Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    60
    Thanks
    65
    Thanked 0 Times in 0 Posts

    find largest and smallest number in JavaScript and compare

    Hi

    I want to compare a number to the largest number and smallest number in JavaScript and I was hoping I could do:

    Code:
    
    var MAX = Number.MAX_VALUE;  //1.7976931348623157e+308
    
    var MIN = Number.MIN_VALUE;  //5e-324
    
    var n =111111111111111111111111111111111111111111111111111111111111111111111.111111111111111111111111111111111111111111111111111111111111111111111111111111111;
    
    
    var h =  n > MAX ? n : "error to big";
    
    var g =  n < MIN ? n : "error to small";
    
    alert(h);
    alert(g);

    If the number is out of range then I want to alert the error?

    How do I do this accurately.

    Thanks

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    972
    Thanks
    19
    Thanked 212 Times in 210 Posts
    more like this ...

    var h = n > MAX ? "error to big" : n;

    var g = n < MIN ? "error to small" : n;

  • Users who have thanked DaveyErwin for this post:

    code-in-time (06-22-2012)

  • #3
    New Coder
    Join Date
    Mar 2012
    Posts
    60
    Thanks
    65
    Thanked 0 Times in 0 Posts
    This is not working.


    Clearly "nLarge" is larger than the biggest javascript number and nSmall is not smaller than the Smallest Javascript number.

    How do I check iaccuratly if the number is in range and if it is safe to use?

    thanks


    Code:
            var MAX = Number.MAX_VALUE;  //1.7976931348623157e+308
    
            var MIN = Number.MIN_VALUE;  //5e-324
    
            var nLarge = 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999.99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999;
    
            var nSmall = -9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999.99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999;
    
    
    
            var h = nLarge > MAX ? "error to big" : "in range";
    
            var g = nSmall < MIN ? "error to small" : "in range";
    
            alert(h);
            alert(g);

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,020
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    The largest number Javascipt can handle is 2^53 == 9 007 199 254 740 992. This is because numbers are stored as floating point with a 52 bit mantissa.

    The smallest value is -2^53.


    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    code-in-time (06-22-2012)

  • #5
    Regular Coder
    Join Date
    Aug 2010
    Posts
    972
    Thanks
    19
    Thanked 212 Times in 210 Posts
    var n =999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 99999999999999999999999999999999999.9999999999999999999999999999999999999999999999999999999999999999 9999999999999999999999999999999999999999;

    if(n == "Infinity")alert("TOO BIG")

    var n =.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000001;
    if(n == 0)alert("TOO SMALL")
    Last edited by DaveyErwin; 06-22-2012 at 06:46 PM.

  • #6
    New Coder
    Join Date
    Mar 2012
    Posts
    60
    Thanks
    65
    Thanked 0 Times in 0 Posts
    Hi thanks for the help


    so how do I check if a number is in range?


    do i have to convert to a string and check for + or - or e

  • #7
    New Coder
    Join Date
    Mar 2012
    Posts
    60
    Thanks
    65
    Thanked 0 Times in 0 Posts
    thanks but this does not work

    Quote Originally Posted by DaveyErwin View Post
    var n =999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 99999999999999999999999999999999999.9999999999999999999999999999999999999999999999999999999999999999 9999999999999999999999999999999999999999;

    if(n == "Infinity")alert("TOO BIG")

    var n =.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000001;
    if(n == 0)alert("TOO SMALL")

  • #8
    Regular Coder
    Join Date
    Aug 2010
    Posts
    972
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Quote Originally Posted by code-in-time View Post
    thanks but this does not work
    if you copy and paste there will be
    spaces that you must remove
    anyways …

    Code:
    <!doctype html>
    <head>
    <title>minmax</title>
    </head>
    <body
    <script>
    a = 1;
    while(a != "Infinity"){a = a * 10;document.write(a + "<br>")}
    alert(a)
    </script>
    </body>
    </html>

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,020
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Code:
    <script type="text/javascript">
     
    var x=1.7976931348623157E+10308;
    alert (x);
     
    var y=-1.7976931348623157E+10308;
    alert (y);
    
    
    if (x == Number.POSITIVE_INFINITY || x == Number.NEGATIVE_INFINITY) {
    alert ("Out of range") ;
    } 
    
    var result=2;
    for (i=1; result!=Infinity; i++){
    result=result*result;
    document.write(i+':'+result+'<BR>');
    }
    
    /* Outputs:
    1:4
    2:16
    3:256
    4:65536
    5:4294967296
    6:18446744073709552000
    7:3.402823669209385e+38
    8:1.157920892373162e+77
    9:1.3407807929942597e+154
    10:Infinity
    */
    
    
     
    </script>
    Last edited by Philip M; 06-22-2012 at 07:31 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    code-in-time (06-23-2012)

  • #10
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Depending on what you're actually trying to do with those numbers, comparing to Infinity might not be what you're after, since numbers larger than 2^53 will still be far away from Infinity (Javascript's definition of it, anyway), but they won't be accurate any more (9007199254740992 === 9007199254740993 // true).
    .My new Javascript tutorial site: http://reallifejs.com/
    .Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
    .Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback

  • Users who have thanked venegal for this post:

    code-in-time (06-23-2012)

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,567
    Thanks
    78
    Thanked 4,384 Times in 4,349 Posts
    Quote Originally Posted by Philip M View Post
    The largest number Javascipt can handle is 2^53 == 9 007 199 254 740 992. This is because numbers are stored as floating point with a 52 bit mantissa.

    The smallest value is -2^53.
    Ummm...Philip means that the largest *INTEGER* that JS can represent is that.

    We know, of course, that the largest NUMBER it can represent is far far larger than that.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    code-in-time (06-23-2012)

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,567
    Thanks
    78
    Thanked 4,384 Times in 4,349 Posts
    Ummm...in the original question,

    nLarge is only 1e+155
    nSmall is only -1e+148

    So of course nLarge isn't larger than MAX.
    So of course nSmall isn't smaller than MIN

    But if you instead make nLarge look like this:
    Code:
            var nLarge = 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999;
    Then JavaScript says that it is "Infinity" and, yes, Infinity is greater than MAX.

    I'm truly curious to know what use that is to anyone.
    Last edited by Old Pedant; 06-23-2012 at 04:23 AM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    code-in-time (06-23-2012)

  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,567
    Thanks
    78
    Thanked 4,384 Times in 4,349 Posts
    Sorry...I missed Philip's code that did
    Code:
    if (x == Number.POSITIVE_INFINITY || x == Number.NEGATIVE_INFINITY) {
        alert ("Out of range") ;
    }
    That is, of course, the best way.

    But I still want to know what use it is.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    code-in-time (06-23-2012)

  • #14
    New Coder
    Join Date
    Mar 2012
    Posts
    60
    Thanks
    65
    Thanked 0 Times in 0 Posts
    wow thanks for the help.

    This is what I'm trying to-do


    On my form I have 4 text boxes.
    I only allow 4 decimal places to be typed in after the dot


    number1 * number2 = answerA
    number3 / number4 = answerB

    I'm trying to as accurately as possible calculate what the user types in

    they Might type in
    10000010.002 * 2.0005 =
    //javascript = 20005020.009001
    //calculator = 20005020.009001

    0.002 * 2.0005 =
    //javascript = 0.004001
    //calculator = 0.004001

    100000.9001 * 200.1000 =
    //javascript = 20010180.110009998
    //calculator = 20010180.11001

    1000.002 / 2.0005 =
    //javascript = 499.87603099225186
    //calculator = 499.8760309922519

    100000.9001 / 200.1000 =
    //javascript = 499.7546231884058
    //calculator = 499.7546231884058

    0.002 / 2.0005 =
    //javascript = 0.0009997500624843788
    //calculator = 9.997500624843789e-4

    What is the maximum number of numbers I should allow to be inserted before the dot?

    the last one is very wrong how do I check for this error?


    thanks

  • #15
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,641
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by code-in-time View Post
    0.002 / 2.0005 =
    //javascript = 0.0009997500624843788
    //calculator = 9.997500624843789e-4

    the last one is very wrong how do I check for this error?
    What do you mean by very wrong - they only differ by one in the 19th decimal place - you can't get numbers that are any closer together than that in JavaScript.

    Try applying .toExponential() to the JavaScript result and it will display 0.0009997500624843788 in exponential notation as 9.997500624843788e-4

    Basically the answers should match to 14 digits and only differ by at most one in the fifteenth digit - because that's how many digits JavaScript can handle.
    Last edited by felgall; 06-23-2012 at 05:57 AM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    code-in-time (06-23-2012)


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •