58

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?

djechlin
  • 59,258
  • 35
  • 162
  • 290
Daniel Kobe
  • 9,376
  • 15
  • 62
  • 109
  • 2
    The `MAX_VALUE` is not integral. And to answer your questions check https://en.wikipedia.org/wiki/IEEE_floating_point – zerkms Jan 14 '16 at 20:36

6 Answers6

51

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

clinton3141
  • 4,751
  • 3
  • 33
  • 46
21

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.

Ilya
  • 5,377
  • 2
  • 18
  • 33
10

Basically floating point numbers are represented as:

digits * 2 ** movement

digits (the mantissa) has 52 bits (and 1 "hidden bit"), movement has 11 bits, and both together form a 64bit number (with 1 sign bit). Through that, you can represent all kinds of different numbers as you can store very large numbers (large positive movement), very small numbers (large negative movement), and integers (digits).

What is Number.MAX_SAFE_INTEGER ?

Integers can just be represented with movement being set in a way that the mantissa is actually the number itself, then digits contains the 52 + 1 bit number, and that can hold up to 2 ** 53 - 1 numbers (which is Number.MAX_SAFE_INTEGER).

Now for larger numbers, you have to use movement, which basically means that you move the digits left or right, and therefore you lose accuracy.

(Imagine digits would just take 8 bits)

  number     >     digits | movement > result
  // savely represented
  11111111   >  11111111  | 0        > 11111111
  // lost the last 1
  111111111  >  11111111  | 1        > 111111110
  // lost the two last 1s
  1111111111 >  11111111  | 10       > 1111111100

What is Number.MAX_VALUE ?

If you set all bits of digits and all bits of movement, you get a number (2 ** 53 - 1) that is moved by 2 ** 10 - 1 to the left, and that is the largest number that can be stored in the 64 bit, everything that is larger is Infinity (which is represented as the movement being 2 ** 10 and the mantissa being 0).

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
7

As you know the javascript have type Number, but not integer. Integer appears by ducktyping feature in javascript. So Number.MAX_SAFE_INTEGER < Number.MAX_VALUE.

They together with MIN_VALUE and MIN_SAFE_INTEGER set range of possible Number value, for double and int when you use parseFloat(X) && parseInt(X).

Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
Egor Malkevich
  • 1,516
  • 1
  • 11
  • 24
6

MAX_VALUE is in double (64 bit)
MAX_SAFE_INTEGER can use first 53 bit of a double(64 bit)
basically javascript doesn't support long. so for int number its uses 32 bit integer container. and for a numbers greater then 32 bit its keep the number in a double container which integer part is 53 bit and rest 11 bits are mantissa(keep the information of a floating point number).

Fisherman
  • 5,949
  • 1
  • 29
  • 35
1

MAX_SAFE_INTEGER has a value of 9007199254740991. The reasoning behind that number is that JavaScript uses double-precision floating-point format numbers as specified in IEEE 754. It returns the maximum safe integer value which can be represented as 2^53 - 1.

Safe refers to the ability to represent integers exactly and to correctly compare them.

Eg: Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2 will evaluate to true

MAX_VALUE on the other hand, has a value of approximately 1.79E+308, or 2^1024. Values larger than MAX_VALUE are represented as Infinity.

mrid
  • 5,782
  • 5
  • 28
  • 71