ECMAScript 6's Number.MAX_SAFE_INTEGER
supposedly represents the maximum numerical value JavaScript can store before issues arise with floating point precision. However it's a requirement that the number 1 added to this value must also be representable as a Number
.
Number.MAX_SAFE_INTEGER
NOTE The value of
Number.MAX_SAFE_INTEGER
is the largest integern
such thatn
andn + 1
are both exactly representable as aNumber
value.The value of
Number.MAX_SAFE_INTEGER
is9007199254740991 (2^53−1)
.– ECMAScript Language Specification
The JavaScript consoles of Chrome, Firefox, Opera and IE11 can all safely perform calculations with the number 9,007,199,254,740,992. Some tests:
// Valid Math.pow(2, 53) // 9007199254740992 9007199254740991 + 1 // 9007199254740992 9007199254740992 - 1 // 9007199254740991 9007199254740992 / 2 // 4503599627370496 4503599627370496 * 2 // 9007199254740992 parseInt('20000000000000', 16) // 9007199254740992 parseInt('80000000000', 32) // 9007199254740992 9007199254740992 - 9007199254740992 // 0 9007199254740992 == 9007199254740991 // false 9007199254740992 == 9007199254740992 // true // Erroneous 9007199254740992 + 1 // 9007199254740992 9007199254740993 + "" // "9007199254740992" 9007199254740992 == 9007199254740993 // true
Why is it a requirement that n + 1
must also be representable as a Number
? Why does failing this make the value unsafe?
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.
The number 2,147,483,647 (or hexadecimal 7FFFFFFF16) is the maximum positive value for a 32-bit signed binary integer in computing.
JavaScript Number MAX_VALUE MAX_VALUE returns the largest number possible in JavaScript. Number. MAX_VALUE has the value of 1.7976931348623157e+308.
The Number. MIN_SAFE_INTEGER constant represents the minimum safe integer in JavaScript, or -(253 - 1). To represent integers smaller than this, consider using BigInt .
I would say its because while Math.pow(2, 53)
is the largest directly representable integer, its unsafe in that its also the first value who's representation is also an approximation of another value:
9007199254740992 == 9007199254740993 // true
In contrast to Math.pow(2, 53) - 1
:
9007199254740991 == 9007199254740993 // false
the only number which is in close proximity of another when compared with its next number shows true
9007199254740992 == 9007199254740993 true 9007199254740993 == 9007199254740994 false 9007199254740991 == 9007199254740992 false 9007199254740997 == 9007199254740998 false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With