Coder Perfect

What is the greatest integer value a number can reach in JavaScript without losing precision?

Problem

Is the language defining this? Is there a certain upper limit? Is it different depending on the browser?

Asked by TALlama

Solution #1

Number and BigInt are the two number types in JavaScript.

Number is a 64-bit floating point IEEE 754 number, which is the most commonly used number type.

Number.MAX SAFE INTEGER has the maximum precise integral value of this type, which is:

To put this in perspective: one quadrillion bytes is a petabyte (or one thousand terabytes).

In this context, “safe” refers to the capacity to accurately represent and compare integers.

From the spec:

You must use BigInt, which has no upper bound, to safely use numbers larger than this.

Note that the bitwise operators and shift operators operate on 32-bit integers, so in that case, the max safe integer is 231-1, or 2,147,483,647.

A technical comment on the number 9,007,199,254,740,992 is as follows: This value has an accurate IEEE-754 representation, and you can assign and read it from a variable, so you may treat it as a maximum value for very carefully chosen applications in the domain of integers less than or equal to this value.

Because it’s unclear if this IEEE-754 value is encoding the logical value 9,007,199,254,740,992 or 9,007,199,254,740,993, you should treat it as inexact in most cases.

Answered by Jimmy

Solution #2

>= ES6:

Number.MIN_SAFE_INTEGER;
Number.MAX_SAFE_INTEGER;

<= ES5

From the reference:

Number.MAX_VALUE;
Number.MIN_VALUE;

Answered by Peter Bailey

Solution #3

253 == 9 007 199 254 740 992 is the number. Because numbers are stored as floating-point in a 52-bit mantissa, this is the case.

-253 is the minimum value.

As a result, some interesting things occur.

Math.pow(2, 53) == Math.pow(2, 53) + 1
>> true

And it’s also potentially dangerous:)

var MAX_INT = Math.pow(2, 53); // 9 007 199 254 740 992
for (var i = MAX_INT; i < MAX_INT + 2; ++i) {
    // infinite loop
}

Further reading: http://blog.vjeux.com/2010/javascript/javascript-max_int-number-limits.html

Answered by Vjeux

Solution #4

There is a number named Infinity in JavaScript.

Examples:

(Infinity>100)
=> true

// Also worth noting
Infinity - 1 == Infinity
=> true

Math.pow(2,1024) === Infinity
=> true

For certain questions on this subject, this may be adequate.

Answered by BananaNeil

Solution #5

Jimmy’s solution accurately depicts the continuous JavaScript integer spectrum as -9007199254740992 to 9007199254740992 inclusive (sorry, 9007199254740993, you believe you’re 9007199254740993, but you’re incorrect!). Demonstration (in jsfiddle or below).

However, no response (other than the one CoolAJ86 pointed to in his answer that would finish in 28.56 years 😉 finds/proves this programatically, so here’s a somewhat more efficient way to do it (to be precise, it’s more efficient by around 28.559999999968312 years:), as well as a test fiddle:

Answered by Briguy37

Post is based on https://stackoverflow.com/questions/307179/what-is-javascripts-highest-integer-value-that-a-number-can-go-to-without-losin