Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the difference between JS Number.MAX_SAFE_INTEGER and MAX_VALUE?

Number.MAX_SAFE_INTEGER 9007199254740991

Number.MAX_VALUE 1.7976931348623157e+308

I understand how MAX_SAFE_INTEGER is computed based on JavaScript's double precision floating point arithmetic, but where does this huge max value come from? Is it the number that comes about if you're using all 63 bits for the exponent instead of the safe 11 bits?

like image 372
Daniel Kobe Avatar asked Jan 14 '16 20:01

Daniel Kobe


People also ask

What is number Max_value in JavaScript?

JavaScript Number MAX_VALUE MAX_VALUE returns the largest number possible in JavaScript. Number. MAX_VALUE has the value of 1.7976931348623157e+308.

Can I use number MAX_SAFE_INTEGER?

Because MAX_SAFE_INTEGER is a static property of Number , you always use it as Number. MAX_SAFE_INTEGER , rather than as a property of a Number object you created.

How many bits is js number?

The JavaScript Number type is a double-precision 64-bit binary format IEEE 754 value, like double in Java or C#.

What is a safe integer in JS?

A safe integer is an integer that. can be exactly represented as an IEEE-754 double precision number, and. whose IEEE-754 representation cannot be the result of rounding any other integer to fit the IEEE-754 representation.


2 Answers

Number.MAX_SAFE_INTEGER is the largest integer which can be used safely in calculations.

For example, Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2 is true — any integer larger than MAX_SAFE_INTEGER cannot always be represented in memory accurately. All bits are used to represent the digits of the number.

Number.MAX_VALUE on the other hand is the largest number possible to represent using a double precision floating point representation. Generally speaking, the larger the number the less accurate it will be.

More information double-precision floating point numbers on Wikipedia

like image 181
iblamefish Avatar answered Oct 12 '22 09:10

iblamefish


JS numbers are internally 64-bits floats (IEEE 754-2008).

MAX_SAFE_INTEGER is the maximum integer that can be safely represented in that format, meaning that all the numbers below that value (and above MIN_SAFE_INTEGER) can be represented as integers.

MAX_VALUE comes from 2^1023 (11 bits mantissa minus mantissa sign), thats about 10^308.

Is it the number that comes about if you're using all 63 bits for the exponent instead of the safe 11 bits?

The mantissa (exponent) is always 11 bits, (not so) surprinsingly that's enough for up to 10^308.

like image 32
Ilya Avatar answered Oct 12 '22 11:10

Ilya