这是由语言定义的吗?有确定的最大值吗?在不同的浏览器中是否有所不同?


当前回答

火狐3似乎不存在庞大数字的问题。

1e+200 * 1e+100将计算到1e+300。

Safari似乎也没有问题。(顺便说一句,如果有人想测试的话,这是在Mac上进行的。)

除非我脑子进水了,否则这比64位整数大得多。

其他回答

简短的回答是“视情况而定。”

如果你在任何地方使用位操作符(或者如果你引用数组的长度),范围是:

无符号:0…(1 > > > 0)

签名:(-(1 > > > 1)1)……(1 > > > 1)

(恰巧按位操作符和数组的最大长度被限制为32位整数。)

如果你不使用位操作符或使用数组长度:

签名:(-Math pow (2.53)) ... + Math . pow (2.53))

这些限制是由“Number”类型的内部表示施加的,通常对应于IEEE 754双精度浮点表示。(请注意,与典型的有符号整数不同,由于内部表示的特征,负极限的大小与正极限的大小相同,它实际上包括一个负0!)

在谷歌Chrome内置javascript,你可以去大约2^1024之前的数字被称为无穷大。

其他人可能已经给出了一般的答案,但我认为给出一个快速确定它的方法会是一个好主意:

for (var x = 2; x + 1 !== x; x *= 2);
console.log(x);

这给了我9007199254740992在Chrome 30不到一毫秒。

它将测试2的幂,以找出当“加”1时,哪一个等于他自己。

Node.js和谷歌Chrome似乎都使用1024位浮点值,因此:

Number.MAX_VALUE = 1.7976931348623157e+308

让我们来看看源头

描述

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.

浏览器兼容性