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


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

string comparison of big integers

Newsgroups comp.lang.java.programmer
Date 2015-02-26 21:43 -0800
Message-ID <fbfdf843-2a45-4e59-a0c4-70020b6982cf@googlegroups.com> (permalink)
Subject string comparison of big integers
From Vlastimil Burian <burian.vlastimil2@gmail.com>

Show all headers | View raw


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! 

Back to comp.lang.java.programmer | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

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

csiph-web