这是由语言定义的吗?有确定的最大值吗?在不同的浏览器中是否有所不同?
当前回答
在JavaScript中,有一个数字叫做Infinity。
例子:
(Infinity>100)
=> true
// Also worth noting
Infinity - 1 == Infinity
=> true
Math.pow(2,1024) === Infinity
=> true
对于这个主题的一些问题,这可能已经足够了。
其他回答
火狐3似乎不存在庞大数字的问题。
1e+200 * 1e+100将计算到1e+300。
Safari似乎也没有问题。(顺便说一句,如果有人想测试的话,这是在Mac上进行的。)
除非我脑子进水了,否则这比64位整数大得多。
>= 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
我这样写:
var max_int = 0x20000000000000;
var min_int = -0x20000000000000;
(max_int + 1) === 0x20000000000000; //true
(max_int - 1) < 0x20000000000000; //true
int32也是一样
var max_int32 = 0x80000000;
var min_int32 = -0x80000000;
我用一个公式做了一个简单的测试,X-(X+1)=-1,我在Safari、Opera和Firefox(在OS X上测试)上可以得到的X的最大值是9e15。下面是我用于测试的代码:
javascript: alert(9e15-(9e15+1));
让我们来看看源头
描述
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.
浏览器兼容性