这是由语言定义的吗?有确定的最大值吗?在不同的浏览器中是否有所不同?
当前回答
让我们来看看源头
描述
The MAX_SAFE_INTEGER constant has a value of 9007199254740991 (9,007,199,254,740,991 or ~9 quadrillion). The reasoning behind that number is that JavaScript uses double-precision floating-point format numbers as specified in IEEE 754 and can only safely represent numbers between -(2^53 - 1) and 2^53 - 1. Safe in this context refers to the ability to represent integers exactly and to correctly compare them. For example, Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2 will evaluate to true, which is mathematically incorrect. See Number.isSafeInteger() for more information. 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.
浏览器兼容性
其他回答
>= ES6:
Number.MIN_SAFE_INTEGER;
Number.MAX_SAFE_INTEGER;
< = ES5
参考文献:
Number.MAX_VALUE;
Number.MIN_VALUE;
console.log (MIN_VALUE, Number.MIN_VALUE); console.log (MAX_VALUE, Number.MAX_VALUE); console.log (MIN_SAFE_INTEGER, Number.MIN_SAFE_INTEGER);/ / ES6 console.log (MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);/ / ES6
的wrotes:
任何你想用于位操作的东西都必须在 0x80000000(-2147483648或-2^31)和0x7fffffff(2147483647或2^31 - 1)。 控制台会告诉您0x80000000等于+2147483648,但是 0x80000000 & 0x80000000 = -2147483648
十六进制小数是无符号的正数值,因此0x80000000 = 2147483648 -这在数学上是正确的。如果你想让它成为一个带符号的值,你必须右移:0x80000000 >> 0 = -2147483648。你也可以写成1 << 31。
火狐3似乎不存在庞大数字的问题。
1e+200 * 1e+100将计算到1e+300。
Safari似乎也没有问题。(顺便说一句,如果有人想测试的话,这是在Mac上进行的。)
除非我脑子进水了,否则这比64位整数大得多。
Node.js和谷歌Chrome似乎都使用1024位浮点值,因此:
Number.MAX_VALUE = 1.7976931348623157e+308
要用于位操作的任何值必须在0x80000000(-2147483648或-2^31)和0x7fffffff(2147483647或2^31 - 1)之间。
控制台会告诉您0x80000000等于+2147483648,但是0x80000000 & 0x80000000等于-2147483648。