How to compare two 20 digit numbers in javascript

Discussion in 'Javascript' started by balakrishnan.dinesh@gmail.com, Jul 28, 2006.

  1. Guest

    hi frnds,
    Im having two 20digit numbers, But while comparing those it
    is giiving wrong ouput in javascript.

    for example here is my code,
    my secanrio is ,

    ~ If first 20 digit number is greater number than second 20 digit
    number ,then it should return.

    * And another important thing is, the two 20digit number will be same
    but last one digit only change

    Code:

    <HTML>
    <script language='javascript'>
    function numberCheck()
    {
    var val1=document.getElementById('txt1').value;
    var val2=document.getElementById('txt2').value;
    if(parseInt(val1) > parseInt(val2))
    alert("Greater")
    else
    alert("Correct");

    }
    </script>
    <body>
    <input type=text id="txt1" value="18446744073709551617" size=25>
    <input type=text id="txt2" value="18446744073709551616" size=25>
    <input type=button onclick="numberCheck();" value="check">

    </body>
    </html>

    So here txt1 value is greater than txt2 value, but it returning Correct
    alert.
    Please tel me reason why its returning like this,
    Is there any restriction for comparing more than 20 digits
     
    , Jul 28, 2006
    #1
    1. Advertising

  2. Randy Webb Guest

    said the following on 7/28/2006 1:44 AM:
    > hi frnds,
    > Im having two 20digit numbers, But while comparing those it
    > is giiving wrong ouput in javascript.
    >
    > for example here is my code,
    > my secanrio is ,
    >
    > ~ If first 20 digit number is greater number than second 20 digit
    > number ,then it should return.
    >
    > * And another important thing is, the two 20digit number will be same
    > but last one digit only change


    If the only thing that can change is the last digit then simply compare
    the last character and it doesn't matter how long the "number" is if it
    is stored in a string.

    > <HTML>
    > <script language='javascript'>
    > function numberCheck()
    > {
    > var val1=document.getElementById('txt1').value;


    var lastChar1 = val1.charAt(val1.length-1);

    > var val2=document.getElementById('txt2').value;


    var lastChar2 = val2.charAt(val2.length-1);


    Now, simply compare the last 2 characters.

    > if(parseInt(val1) > parseInt(val2))


    Don't use parseInt without the second parameter or you may eventually
    get unexpected results.

    if (+lastChar1>+lastChar2)

    <snip>

    >
    > So here txt1 value is greater than txt2 value, but it returning Correct
    > alert.
    > Please tel me reason why its returning like this,


    The incorrectness introduced by floating point math.

    > Is there any restriction for comparing more than 20 digits


    Probably.

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, Jul 28, 2006
    #2
    1. Advertising

  3. RobG Guest

    wrote:
    [...]
    > So here txt1 value is greater than txt2 value, but it returning Correct
    > alert.
    > Please tel me reason why its returning like this,
    > Is there any restriction for comparing more than 20 digits


    Yes, absolutely.

    If only the last digit needs to be compared, then Randy has answered
    your question. However, if you want to compare large numbers as
    numbers, read the following thread:

    <URL:
    http://groups.google.com/group/comp...gest positive integer&rnum=3#8c73213f5f2e91c5
    >


    >From that thread:


    "The representable range is +/-1.7976931348623157×10^308 (with
    numbers as small as +/-5×10^-324). The range of precise integer
    values is +/-9007199254740992 (+/-2^53)."

    Note that there are many integers in the representable range that can't
    be represented exactly, starting with integers of 16 digits. That is
    well short of your requirement for 20 digits.


    --
    Rob
     
    RobG, Jul 28, 2006
    #3
  4. Guest

    Hi Randy,

    For a example only ,i told u the last character, It can be any digit in
    the whole number, So i want to compare the whole 20 digit number with
    other 20 digit number.

    I want know ,why is it not comparing correctly. Is there anyother way
    to compare
     
    , Jul 28, 2006
    #4
  5. Guest

    Hi Randy,

    For a example only ,i told u the last character, It can be any digit in
    the whole number, So i want to compare the whole 20 digit number with
    other 20 digit number.

    I want know ,why is it not comparing correctly. Is there anyother way
    to compare
     
    , Jul 28, 2006
    #5
  6. Guest

    HI Rob,

    Thanks for your information.

    Then there no way to compare this. My senario is like that,
    what can i do to solve this problem.
    Can u give me a suggestion.
     
    , Jul 28, 2006
    #6
  7. Randy Webb Guest

    said the following on 7/28/2006 2:51 AM:
    > Hi Randy,
    >
    > For a example only ,i told u the last character, It can be any digit in
    > the whole number, So i want to compare the whole 20 digit number with
    > other 20 digit number.


    The value of a text input is a string. Do a string comparison and you
    don't run into the 16 digit limit.

    > I want know ,why is it not comparing correctly. Is there anyother way
    > to compare


    Compare them as strings.

    if (string1 == string2)
    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, Jul 28, 2006
    #7
  8. RobG Guest

    wrote:
    > HI Rob,
    >
    > Thanks for your information.
    >
    > Then there no way to compare this. My senario is like that,
    > what can i do to solve this problem.
    > Can u give me a suggestion.


    An algorithm is:

    - get the values as strings (input elements will return strings)
    - check they are both integers
    - remove leading zeros
    - the longest one is bigger
    - if they are the same length, check digit by digit
    - if the numbers are really big, chec, blocks of say
    10 digits at a time.

    e.g. the following example returns true only if a and b are integers
    and a is bigger than b. If a or b aren't integers, 'undefined' is
    returned. Otherwise, if b is bigger or equal to a, false is returned.

    function isBigger(a, b)
    {
    // Make sure they are integers
    var re = new RegExp('\\D','g');
    if (re.test(a) || re.test(b)){
    return undefined;
    }

    // Trim leading zeros
    a = a.replace(/^0+/g,'');
    b = b.replace(/^0+/g,'');

    // Check lengths
    if (a.length != b.length){
    return (a.length > b.length);
    }

    // Compare digits
    var a = a.split('');
    var b = b.split('');
    for (var i=0, j=a.length; i<j; i++){
    if (+a > b) return true;
    }
    return false;
    }
     
    RobG, Jul 28, 2006
    #8
  9. Randy Webb Guest

    RobG said the following on 7/28/2006 5:07 AM:
    > wrote:
    >> HI Rob,
    >>
    >> Thanks for your information.
    >>
    >> Then there no way to compare this. My senario is like that,
    >> what can i do to solve this problem.
    >> Can u give me a suggestion.

    >
    > An algorithm is:
    >
    > - get the values as strings (input elements will return strings)
    > - check they are both integers
    > - remove leading zeros


    If they are defined in the HTML then you can be pretty assured whether
    they are numbers or not, but even then, comparing them as strings is
    simpler and easier. After removing the leading zeros, if they are equal
    as strings they must be equal as numbers. If they aren't equal as
    strings then they aren't equal as numbers.

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, Jul 28, 2006
    #9
  10. Paul Guest

    RobG wrote:
    > wrote:
    > > HI Rob,
    > >
    > > Thanks for your information.
    > >
    > > Then there no way to compare this. My senario is like that,
    > > what can i do to solve this problem.
    > > Can u give me a suggestion.

    >
    > An algorithm is:
    >
    > - get the values as strings (input elements will return strings)
    > - check they are both integers
    > - remove leading zeros
    > - the longest one is bigger
    > - if they are the same length, check digit by digit
    > - if the numbers are really big, chec, blocks of say
    > 10 digits at a time.
    >
    > e.g. the following example returns true only if a and b are integers
    > and a is bigger than b. If a or b aren't integers, 'undefined' is
    > returned. Otherwise, if b is bigger or equal to a, false is returned.
    >
    > function isBigger(a, b)
    > {
    > // Make sure they are integers
    > var re = new RegExp('\\D','g');
    > if (re.test(a) || re.test(b)){
    > return undefined;
    > }
    >
    > // Trim leading zeros
    > a = a.replace(/^0+/g,'');
    > b = b.replace(/^0+/g,'');
    >
    > // Check lengths
    > if (a.length != b.length){
    > return (a.length > b.length);
    > }
    >
    > // Compare digits
    > var a = a.split('');
    > var b = b.split('');
    > for (var i=0, j=a.length; i<j; i++){
    > if (+a > b) return true;
    > }
    > return false;
    > }


    Rob, I think you've got a great solution for the problem.
    But, I'm going to offer up something a little more general purpose.
    Inspired by Java's BigDecimal object. I'm going to propose a BigInteger
    for java that will allow for comparison of really large numbers.
    /*
    * Representation of a big integer.
    * the compare method returns a value greater than zero if the
    comparing
    * object is greater, a negative number if the object compared is
    greater,
    * or zero if the objects are equal.
    */
    function BigInteger(strVal) {
    this.parts = new Array();
    for(var i=0; i<strVal.length; i++){
    this.parts = +strVal.charAt(i);
    }
    this.compare = function(num){
    var thisLength = this.parts.length;
    var numLength = num.parts.length;
    var greater = 0;
    if(thisLength == numLength && thisLength>0){
    var cnt = thisLength-1;
    while( greater==0 && cnt >= 0 ){
    greater = this.parts[cnt] - num.parts[cnt];
    cnt--;
    }
    } else {
    greater = (thisLength > numLength)?(1):(-1);
    }
    return greater;
    }
    }
    Granted, creating an array of numbers is probably overkill for simple
    comparison it does simplify extending to allow mathematical operations
    pretty easily.

    The original number check function could be modified to use this like:
    function numberCheck()
    {
    var val1=document.getElementById('txt1').value;
    var val2=document.getElementById('txt2').value;
    var val1Bigint = new BigInteger(val1);
    var val2Bigint = new BigInteger(val2);

    var comp = val1Bigint.compare(val2Bigint);
    if(comp==0) {
    alert("Equal");
    } else if(comp>0){
    alert("Greater")
    } else {
    alert("Less");
    }
    }
     
    Paul, Jul 28, 2006
    #10
  11. JRS: In article <>
    , dated Fri, 28 Jul 2006 02:07:30 remote, seen in
    news:comp.lang.javascript, RobG <> posted :
    >
    > wrote:


    >> Can u give me a suggestion.


    That must seem very polite, to a Burmese.

    >An algorithm is:
    >
    > - get the values as strings (input elements will return strings)
    > - check they are both integers


    But -79, +3, 123e45, 123e4567 are all integers. *Probably* he means
    all-digit strings.

    > - remove leading zeros


    Probably. But 077 = 0x3F = 63 = 0.63e2, maybe.

    >function isBigger(a, b)
    >{
    > // Make sure they are integers
    > var re = new RegExp('\\D','g');


    I don't see that 'g' is needed, any non-digit fails an all-digit format.

    > if (re.test(a) || re.test(b)){
    > return undefined;
    > }
    >
    > // Trim leading zeros
    > a = a.replace(/^0+/g,'');
    > b = b.replace(/^0+/g,'');


    Ditto.

    > // Check lengths
    > if (a.length != b.length){
    > return (a.length > b.length);
    > }
    >
    > // Compare digits
    > var a = a.split('');
    > var b = b.split('');
    > for (var i=0, j=a.length; i<j; i++){
    > if (+a > b) return true;
    > }
    > return false;
    >}


    It could be better to compare a.charAt(i) with b.charAt(i); it saves
    generating length+length objects. Perhaps I mean charCodeAt throughout.
    I suspect you've not handled b<a, though he seems not to need that.


    I think I'd go lower-level.

    Take strings A & B.

    From the Subject line, we can take it that the numbers consist only of
    decimal digits, to be interpreted as decimal (if doubtful, use a
    preliminary RegExp [*]).

    We can also take it that they are of equal length; and, if not, it's
    easy to prepend a string of zeroes to the shorter. At this stage they
    can in fact be compared as strings.

    Otherwise, loop through both strings, subtracting A.charAt and
    B.charAt. At the first difference, return that difference. Finally
    return 0 (the final difference; so it'd be nice to use
    Dif = cA-cB ; if (!Dif) break ; ... return Dif ; ).


    ISTM that charAt should be swift given what ECMA seems to say about
    all characters using 16 bits. If charAt[50] requires 100 or more bytes
    to be considered, I'd not want to use it here.

    ISTM that the strings could perhaps be broken up into ten-digit units
    with a cunning RegExp (actually, cunning not needed where the length is
    a multiple of 10) match, and the resulting strings compared numerically
    in turn with unary +.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, Jul 28, 2006
    #11
  12. Guest

    Hi john,

    Thanks for suggestion, Its working fine.
     
    , Aug 3, 2006
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fangs
    Replies:
    3
    Views:
    9,801
    darshana
    Oct 26, 2008
  2. GenxLogic
    Replies:
    3
    Views:
    1,289
    andrewmcdonagh
    Dec 6, 2006
  3. Steven
    Replies:
    8
    Views:
    471
    Arndt Jonasson
    Feb 3, 2006
  4. puneet vyas

    Addition of two 100 digit numbers

    puneet vyas, Oct 23, 2008, in forum: C++
    Replies:
    1
    Views:
    375
    Bernd Strieder
    Oct 23, 2008
  5. Alessio
    Replies:
    10
    Views:
    705
    Alessio
    Oct 5, 2009
Loading...

Share This Page