javascript tutorial - [Solved-5 Solutions] What is javascript’s highest integer value that a number can go to without losing precision ? - javascript - java script - javascript array
Problem:
Is this defined by the language? Is there a defined maximum ? Is it different in different browsers ?
Solution 1:
They are 64-bit floating point values, the largest exact integral value is 253-1, or 9007199254740991. In ES6, this is defined as Number MAX_SAFE_INTEGER. Note that the bitwise operators and shift operators operate on 32-bit ints, so in that case, the max safe integer is 231-1, or 2147483647. Test it out!
var x = 9007199254740992;
var y = -x;
x == x + 1; // true !
y == y - 1; // also true !
// Arithmetic operators work, but bitwise/shifts only operate on int32:
x / 2; // 4503599627370496
x >> 1; // 0
x | 1; // 1
click below button to copy the code. By JavaScript tutorial team
Solution 2:
From the reference: alert([Number.MAX_VALUE, Number.MIN_VALUE]);
Solution 3:
It is 253 == 9 007 199 254 740 992. This is because Numbers are stored as floating-point in a 52-bit mantissa. The min value is -253. This makes some fun things happening
Math.pow(2, 53) == Math.pow(2, 53) + 1
>> true
click below button to copy the code. By JavaScript tutorial team
And can also be dangerous :)
var MAX_INT = Math.pow(2, 53); // 9 007 199 254 740 992
for (var we = MAX_INT; we < MAX_INT + 2; ++i) {
// infinite loop
}
click below button to copy the code. By JavaScript tutorial team
Solution 4:
In javascript, there is a number called Infinity examples:
(Infinity>100)
=> true
//also worth noting
Infinity - 1 == Infinity
=> true
Math.pow(2,1024) === Infinity
=> true
click below button to copy the code. By JavaScript tutorial team
Solution 5:
ECMAScript 6:
Number.MAX_SAFE_INTEGER = Math.pow(2, 53)-1;
Number.MIN_SAFE_INTEGER = -Number.MAX_SAFE_INTEGER;