View Full Version : find largest and smallest number in JavaScript and compare

code-in-time

06-22-2012, 04:41 PM

Hi

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

var MAX = Number.MAX_VALUE; //1.7976931348623157e+308

var MIN = Number.MIN_VALUE; //5e-324

var n =111111111111111111111111111111111111111111111111111111111111111111111.11111111111111111111111111111 1111111111111111111111111111111111111111111111111111;

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

DaveyErwin

06-22-2012, 05:01 PM

more like this ...

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

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

code-in-time

06-22-2012, 06:28 PM

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

var MAX = Number.MAX_VALUE; //1.7976931348623157e+308

var MIN = Number.MIN_VALUE; //5e-324

var nLarge = 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 9999999999999999999999999999999999999999999999999999999.99999999999999999999999999999999999999999999 999999999999999999999999999999999999999999999999999999999999;

var nSmall = -9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 999999999999999999999999999999999999999999999999.999999999999999999999999999999999999999999999999999 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 9999999;

var h = nLarge > MAX ? "error to big" : "in range";

var g = nSmall < MIN ? "error to small" : "in range";

alert(h);

alert(g);

Philip M

06-22-2012, 06:35 PM

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.

DaveyErwin

06-22-2012, 06:37 PM

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")

code-in-time

06-22-2012, 06:43 PM

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

code-in-time

06-22-2012, 06:58 PM

thanks but this does not work

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")

DaveyErwin

06-22-2012, 07:12 PM

thanks but this does not work

if you copy and paste there will be

spaces that you must remove

anyways …

<!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>

Philip M

06-22-2012, 07:23 PM

<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>

venegal

06-23-2012, 03:57 AM

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).

Old Pedant

06-23-2012, 04:06 AM

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.

Old Pedant

06-23-2012, 04:18 AM

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:

var nLarge = 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 99999999999999999999999999999999999999999999999999999999999999999999999999999;

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.

Old Pedant

06-23-2012, 04:26 AM

Sorry...I missed Philip's code that did

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.

code-in-time

06-23-2012, 05:38 AM

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

felgall

06-23-2012, 05:54 AM

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.

code-in-time

06-23-2012, 06:28 AM

Hi thanks.

so this exponential notation is very new to me.

so what you are telling me is that javascript is correct and the calculator is just showing it in a neatter format with out showing lots of zeros?

How do you convert from exponential notation? to see all the zeros?

thanks

Philip M

06-23-2012, 07:38 AM

How do you convert from exponential notation? to see all the zeros?

var x = 1e40.toFixed();

// note that x is now a string variable

x = Number(x); // 1e40 - a number again

code-in-time

06-23-2012, 02:16 PM

Hi

Is there an easy was to check if a number is in Exponential Notation.

Do I have to convert to a string and see if the string contains "e-" or "e+"

thanks

venegal

06-23-2012, 02:25 PM

As soon as you're talking about a certain kind of "notation", you're talking about strings. Checking whether there's an "e" in a number's string representation will certainly do the trick, and, depending on how you do it, you don't have to convert manually; you can use type coercion instead, for example:

/e/.test(yourNumber)

code-in-time

06-23-2012, 03:01 PM

thanks

code-in-time

06-23-2012, 07:30 PM

Hi , another question please

It appears that if javascript can't display the number properly then when it gets to long it converts it to Exponential Notation.

Is this ok?

var num = 1111111111111111111111.0000 * 1.0001

//returns 1.11122222222222E+21

var num = 11111111111111.0000 * 1.0001

//returns 11112222222222.1

function checkNumber(num.toString()){

if(num.indexOf("E+") || num.indexOf("E-")){

alert("number to large or to small");

}

}

DaveyErwin

06-23-2012, 08:18 PM

num = 1111111111111111111111.0000 * 1.0001

function checkNumber(n){

if(n.toString().indexOf("e")){

alert("number is expressed with exponential notation");

}

}

checkNumber(num)

alert(/e/.test(num))//true the number is expressed with exponential notation

alert(/e/.test(11111111111111.0000 * 1.0001))//false the number is not expressed with exponential notation

Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.