Enjoy an ad free experience by logging in. Not a member yet? Register.


Results 1 to 15 of 22

06222012, 04:41 PM #1
 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; //5e324 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
06222012, 05:01 PM
#2
 Join Date
 Aug 2010
 Posts
 945
 Thanks
 19
 Thanked 205 Times in 203 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:
codeintime (06222012)
06222012, 06:28 PM
#3
 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; //5e324 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);
06222012, 06:35 PM
#4
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 17,731
 Thanks
 202
 Thanked 2,508 Times in 2,486 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:
codeintime (06222012)
06222012, 06:37 PM
#5
 Join Date
 Aug 2010
 Posts
 945
 Thanks
 19
 Thanked 205 Times in 203 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; 06222012 at 06:46 PM.
06222012, 06:43 PM
#6
 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
06222012, 06:58 PM
#7
 Join Date
 Mar 2012
 Posts
 60
 Thanks
 65
 Thanked 0 Times in 0 Posts
06222012, 07:12 PM
#8
 Join Date
 Aug 2010
 Posts
 945
 Thanks
 19
 Thanked 205 Times in 203 Posts
06222012, 07:23 PM
#9
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 17,731
 Thanks
 202
 Thanked 2,508 Times in 2,486 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; 06222012 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:
codeintime (06232012)
06232012, 03:57 AM
#10
 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, Fullfledged OOP calculator, Big number calculator
.Latest quickbit: Including jQuery — Environmentaware minification and CDNs with local fallback
Users who have thanked venegal for this post:
codeintime (06232012)
06232012, 04:06 AM
#11
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:
codeintime (06232012)
06232012, 04:18 AM
#12
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:
Then JavaScript says that it is "Infinity" and, yes, Infinity is greater than MAX.Code:var nLarge = 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999;
I'm truly curious to know what use that is to anyone.
Last edited by Old Pedant; 06232012 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:
codeintime (06232012)
06232012, 04:26 AM
#13
Sorry...I missed Philip's code that did
That is, of course, the best way.Code:if (x == Number.POSITIVE_INFINITY  x == Number.NEGATIVE_INFINITY) { alert ("Out of range") ; }
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:
codeintime (06232012)
06232012, 05:38 AM
#14
 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 todo
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.997500624843789e4
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
06232012, 05:54 AM
#15
 Join Date
 Sep 2005
 Location
 Sydney, Australia
 Posts
 6,471
 Thanks
 0
 Thanked 634 Times in 624 Posts
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.997500624843788e4
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; 06232012 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:
codeintime (06232012)