Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #31227 > unrolled thread

string comparison of big integers

Started byVlastimil Burian <burian.vlastimil2@gmail.com>
First post2015-02-26 21:43 -0800
Last post2015-02-27 11:08 +0000
Articles 10 — 6 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  string comparison of big integers Vlastimil Burian <burian.vlastimil2@gmail.com> - 2015-02-26 21:43 -0800
    Re: string comparison of big integers Joerg Meier <joergmmeier@arcor.de> - 2015-02-27 08:21 +0100
      Re: string comparison of big integers Vlastimil Burian <burian.vlastimil2@gmail.com> - 2015-02-26 23:28 -0800
        Re: string comparison of big integers Vlastimil Burian <burian.vlastimil2@gmail.com> - 2015-02-26 23:53 -0800
        Re: string comparison of big integers Joerg Meier <joergmmeier@arcor.de> - 2015-02-27 09:12 +0100
        Re: string comparison of big integers Arne Vajhøj <arne@vajhoej.dk> - 2015-02-27 07:04 -0500
    Re: string comparison of big integers Andreas Leitgeb <avl@auth.logic.tuwien.ac.at> - 2015-02-27 10:57 +0000
      Re: string comparison of big integers burian.vlastimil2@gmail.com - 2015-02-27 03:04 -0800
        Re: string comparison of big integers Arne Vajhøj <arne@vajhoej.dk> - 2015-02-27 11:23 -0500
    Re: string comparison of big integers Martin Gregorie <martin@address-in-sig.invalid> - 2015-02-27 11:08 +0000

#31227 — string comparison of big integers

FromVlastimil Burian <burian.vlastimil2@gmail.com>
Date2015-02-26 21:43 -0800
Subjectstring comparison of big integers
Message-ID<fbfdf843-2a45-4e59-a0c4-70020b6982cf@googlegroups.com>
The code is in C#, but it should be understandable to you all.



I need to compare big numbers in all number systems, but first step is to be able to compare them in decimal number system, which is not working properly, 



e.g. 

           int Comparison = CompareNumbers("510", "228"); 


            if (Comparison == 0) 

                MessageBox.Show("N1 = N2"); 

            else if (Comparison == 1)                 

                MessageBox.Show("N1 > N2"); 

            else 

                MessageBox.Show("N1 < N2"); 

shows "N1 < N2" which is obviously WRONG 


please don't care much about GetDigitValue function and the number systems as I am pretty sure this is OK 




string Numbers = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 


private Int16 GetDigitValue(char Digit) 
{ 

Int16 Value = Convert.ToInt16(Numbers.IndexOf(Char.ToUpper(Digit))); 

if (Value == -1) 
   throw new InvalidNumberSystem(); 
   else return Value; 

} 


private int CompareNumbers(string Number1, string Number2) 
{ 

int I = -1; 

Number1 = Number1.TrimStart('0'); 
Number2 = Number2.TrimStart('0'); 

if (Number1 == Number2)              return  0; else 
if (Number1.Length < Number2.Length) return -1; else 
if (Number1.Length > Number2.Length) return  1; else 
{ 
     do { 
         I++; 
     } while ((I < Number1.Length) && (GetDigitValue(Number1[I]) >= GetDigitValue(Number2[I]))); 
            } 

if (I < Number1.Length - 1) 
     return -1; 
else 
     return 1; 

} 



Whatt is wrong?, please help! 

[toc] | [next] | [standalone]


#31228

FromJoerg Meier <joergmmeier@arcor.de>
Date2015-02-27 08:21 +0100
Message-ID<ymdzcix9sz2l$.1ejyrjg120tvo.dlg@40tude.net>
In reply to#31227
On Thu, 26 Feb 2015 21:43:06 -0800 (PST), Vlastimil Burian wrote:

> I need to compare big numbers in all number systems, but first step is to be able to compare them in decimal number system, which is not working properly, 

Simply use BigInteger or BigDecimal.

BigInteger number1 = new BigInteger("994237846283");
BigInteger number2 = new BigInteger("287391283712");

if (number1.compareTo(number2) > 0) System.out.println("number1 is bigger
than number2");

Liebe Gruesse,
		Joerg

-- 
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.

[toc] | [prev] | [next] | [standalone]


#31229

FromVlastimil Burian <burian.vlastimil2@gmail.com>
Date2015-02-26 23:28 -0800
Message-ID<6e87c06e-7e35-45b1-a76e-766befb279da@googlegroups.com>
In reply to#31228
On Friday, February 27, 2015 at 8:21:46 AM UTC+1, Joerg Meier wrote:
> On Thu, 26 Feb 2015 21:43:06 -0800 (PST), Vlastimil Burian wrote:
> 
> > I need to compare big numbers in all number systems, but first step is to be able to compare them in decimal number system, which is not working properly, 
> 
> Simply use BigInteger or BigDecimal.
> 
> BigInteger number1 = new BigInteger("994237846283");
> BigInteger number2 = new BigInteger("287391283712");
> 
> if (number1.compareTo(number2) > 0) System.out.println("number1 is bigger
> than number2");
> 
> Liebe Gruesse,
> 		Joerg
> 
> -- 
> Ich lese meine Emails nicht, replies to Email bleiben also leider
> ungelesen.

Error 3  The type or namespace name 'Numeric' does not exist in the namespace 'System' (are you missing an assembly reference?)

in FW 4.5.1

[toc] | [prev] | [next] | [standalone]


#31230

FromVlastimil Burian <burian.vlastimil2@gmail.com>
Date2015-02-26 23:53 -0800
Message-ID<50244d61-e987-4d04-86a7-85ce4005bcdc@googlegroups.com>
In reply to#31229
On Friday, February 27, 2015 at 8:28:46 AM UTC+1, Vlastimil Burian wrote:
> On Friday, February 27, 2015 at 8:21:46 AM UTC+1, Joerg Meier wrote:
> > On Thu, 26 Feb 2015 21:43:06 -0800 (PST), Vlastimil Burian wrote:
> > 
> > > I need to compare big numbers in all number systems, but first step is to be able to compare them in decimal number system, which is not working properly, 
> > 
> > Simply use BigInteger or BigDecimal.
> > 
> > BigInteger number1 = new BigInteger("994237846283");
> > BigInteger number2 = new BigInteger("287391283712");
> > 
> > if (number1.compareTo(number2) > 0) System.out.println("number1 is bigger
> > than number2");
> > 
> > Liebe Gruesse,
> > 		Joerg
> > 
> > -- 
> > Ich lese meine Emails nicht, replies to Email bleiben also leider
> > ungelesen.
> 
> Error 3  The type or namespace name 'Numeric' does not exist in the namespace 'System' (are you missing an assembly reference?)
> 
> in FW 4.5.1

I'm solving it now on MSDN forum. Thanks anyway. Goodbye.

[toc] | [prev] | [next] | [standalone]


#31231

FromJoerg Meier <joergmmeier@arcor.de>
Date2015-02-27 09:12 +0100
Message-ID<vu7aee8fu412.1gzwmu5g7ox4s.dlg@40tude.net>
In reply to#31229
On Thu, 26 Feb 2015 23:28:35 -0800 (PST), Vlastimil Burian wrote:

> On Friday, February 27, 2015 at 8:21:46 AM UTC+1, Joerg Meier wrote:
>> On Thu, 26 Feb 2015 21:43:06 -0800 (PST), Vlastimil Burian wrote:
>>> I need to compare big numbers in all number systems, but first step is to be able to compare them in decimal number system, which is not working properly, 
>> Simply use BigInteger or BigDecimal.

>> BigInteger number1 = new BigInteger("994237846283");
>> BigInteger number2 = new BigInteger("287391283712");

>> if (number1.compareTo(number2) > 0) System.out.println("number1 is bigger
>> than number2");
> Error 3  The type or namespace name 'Numeric' does not exist in the namespace 'System' (are you missing an assembly reference?)

> in FW 4.5.1

Java has neither namespaces nor assembly references.

Liebe Gruesse,
		Joerg

-- 
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.

[toc] | [prev] | [next] | [standalone]


#31236

FromArne Vajhøj <arne@vajhoej.dk>
Date2015-02-27 07:04 -0500
Message-ID<54f05d33$0$280$14726298@news.sunsite.dk>
In reply to#31229
On 2/27/2015 2:28 AM, Vlastimil Burian wrote:
> On Friday, February 27, 2015 at 8:21:46 AM UTC+1, Joerg Meier wrote:
>> On Thu, 26 Feb 2015 21:43:06 -0800 (PST), Vlastimil Burian wrote:
>>> I need to compare big numbers in all number systems, but first step is to be able to compare them in decimal number system, which is not working properly,
>> Simply use BigInteger or BigDecimal.
>>
>> BigInteger number1 = new BigInteger("994237846283");
>> BigInteger number2 = new BigInteger("287391283712");
>>
>> if (number1.compareTo(number2) > 0) System.out.println("number1 is bigger
>> than number2");
>>
>> Liebe Gruesse,
>> 		Joerg
>>
>> --
>> Ich lese meine Emails nicht, replies to Email bleiben also leider
>> ungelesen.
>
> Error 3  The type or namespace name 'Numeric' does not exist in the namespace 'System' (are you missing an assembly reference?)
>
> in FW 4.5.1

.NET != Java

Arne

[toc] | [prev] | [next] | [standalone]


#31232

FromAndreas Leitgeb <avl@auth.logic.tuwien.ac.at>
Date2015-02-27 10:57 +0000
Message-ID<slrnmf0jej.uoi.avl@login.logic.tuwien.ac.at>
In reply to#31227
Vlastimil Burian <burian.vlastimil2@gmail.com> wrote:
> The code is in C#, but it should be understandable to you all.

>      } while ((I < Number1.Length) && (GetDigitValue(Number1[I]) >= GetDigitValue(Number2[I]))); 

I think the mistake is here.

[toc] | [prev] | [next] | [standalone]


#31234

Fromburian.vlastimil2@gmail.com
Date2015-02-27 03:04 -0800
Message-ID<deeace69-ed4b-4411-9b27-9bc0207c474d@googlegroups.com>
In reply to#31232
On Friday, February 27, 2015 at 11:58:38 AM UTC+1, Andreas Leitgeb wrote:
> Vlastimil Burian <burian.vlastimil2@gmail.com> wrote:
> > The code is in C#, but it should be understandable to you all.
> 
> >      } while ((I < Number1.Length) && (GetDigitValue(Number1[I]) >= GetDigitValue(Number2[I]))); 
> 
> I think the mistake is here.

Yes. It was there.

Working code:

        private int CompareNumbers(string Number1, string Number2, Int16 NumSystem)
        {

            if ((NumSystem < 2) || (NumSystem > 36))
                throw new InvalidNumberSystem();

            if (!NumberValid(Number1, NumSystem))
                throw new InvalidNumberSystem();

            if (!NumberValid(Number2, NumSystem))
                throw new InvalidNumberSystem();

            int I = -1;

            Number1 = Number1.TrimStart('0');
            Number2 = Number2.TrimStart('0');

            if (Number1 == Number2)              return  0; else
            if (Number1.Length < Number2.Length) return -1; else
            if (Number1.Length > Number2.Length) return  1; else
            {
                do {
                    I++;
                } while ((I < Number1.Length) && (GetDigitValue(Number1[I]) == GetDigitValue(Number2[I])));
            }

            if ((I < Number1.Length) && (GetDigitValue(Number1[I]) <= GetDigitValue(Number2[I])))
                return -1;
            else
                return 1;

        }

[toc] | [prev] | [next] | [standalone]


#31237

FromArne Vajhøj <arne@vajhoej.dk>
Date2015-02-27 11:23 -0500
Message-ID<54f09a01$0$285$14726298@news.sunsite.dk>
In reply to#31234
On 2/27/2015 6:04 AM, burian.vlastimil2@gmail.com wrote:
> On Friday, February 27, 2015 at 11:58:38 AM UTC+1, Andreas Leitgeb wrote:
>> Vlastimil Burian <burian.vlastimil2@gmail.com> wrote:
>>> The code is in C#, but it should be understandable to you all.
>>
>>>       } while ((I < Number1.Length) && (GetDigitValue(Number1[I]) >= GetDigitValue(Number2[I])));
>>
>> I think the mistake is here.
>
> Yes. It was there.
>
> Working code:
>
>          private int CompareNumbers(string Number1, string Number2, Int16 NumSystem)
>          {
>
>              if ((NumSystem < 2) || (NumSystem > 36))
>                  throw new InvalidNumberSystem();
>
>              if (!NumberValid(Number1, NumSystem))
>                  throw new InvalidNumberSystem();
>
>              if (!NumberValid(Number2, NumSystem))
>                  throw new InvalidNumberSystem();
>
>              int I = -1;
>
>              Number1 = Number1.TrimStart('0');
>              Number2 = Number2.TrimStart('0');
>
>              if (Number1 == Number2)              return  0; else
>              if (Number1.Length < Number2.Length) return -1; else
>              if (Number1.Length > Number2.Length) return  1; else
>              {
>                  do {
>                      I++;
>                  } while ((I < Number1.Length) && (GetDigitValue(Number1[I]) == GetDigitValue(Number2[I])));
>              }
>
>              if ((I < Number1.Length) && (GetDigitValue(Number1[I]) <= GetDigitValue(Number2[I])))
>                  return -1;
>              else
>                  return 1;
>
>          }

C# code is really off-topic here.

But I don't like the code above.

Here are two alternative ideas to work with:

         public static int CompareNumbers2(string number1, string 
number2, short radix)
         {
             BigInteger n1 = number1.ToCharArray().Aggregate(new 
BigInteger(0), (val,dig) => radix * val + GetDigitValue(dig));
             BigInteger n2 = number2.ToCharArray().Aggregate(new 
BigInteger(0), (val,dig) => radix * val + GetDigitValue(dig));
             return number1.CompareTo(number2);
         }

         public static int CompareNumbers3(string number1, string 
number2, short radix)
         {
             string n1 = number1.TrimStart('0');
             string n2 = number2.TrimStart('0');
             if(n1 == n2) return 0;
             if(n1.Length < n2.Length) return -1;
             if(n1.Length > n2.Length) return 1;
             return 
((IStructuralComparable)(n1.ToCharArray())).CompareTo(n2.ToCharArray(), 
Comparer<char>.Create((c1,c2) => 
GetDigitValue(c1).CompareTo(GetDigitValue(c2))));
         }

Arne

[toc] | [prev] | [next] | [standalone]


#31235

FromMartin Gregorie <martin@address-in-sig.invalid>
Date2015-02-27 11:08 +0000
Message-ID<mcpj7p$d60$1@dont-email.me>
In reply to#31227
On Thu, 26 Feb 2015 21:43:06 -0800, Vlastimil Burian wrote:

> Whatt is wrong?, please help!

Obvious: use "while (condition test) { i++ }"
and not      "{ i++ } while (condition test)
so you start the comparison at the first digit, not the second.


-- 
martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web