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


当前回答

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

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

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

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

其他回答

在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位整数大得多。

Jimmy的答案正确地表示了连续的JavaScript整数谱,从-9007199254740992到9007199254740992,包括(对不起,9007199254740993,您可能认为您是9007199254740993,但您错了! 下面或jsfiddle中的演示)。

console.log(9007199254740993);

然而,没有答案可以通过编程来发现/证明这一点(除了CoolAJ86在他的回答中提到的将在28.56年完成的答案),所以这里有一个稍微更有效的方法来做到这一点(准确地说,它更有效约28.559999999968312年:),以及一个测试小提琴:

/** * Checks if adding/subtracting one to/from a number yields the correct result. * * @param number The number to test * @return true if you can add/subtract 1, false otherwise. */ var canAddSubtractOneFromNumber = function(number) { var numMinusOne = number - 1; var numPlusOne = number + 1; return ((number - numMinusOne) === 1) && ((number - numPlusOne) === -1); } //Find the highest number var highestNumber = 3; //Start with an integer 1 or higher //Get a number higher than the valid integer range while (canAddSubtractOneFromNumber(highestNumber)) { highestNumber *= 2; } //Find the lowest number you can't add/subtract 1 from var numToSubtract = highestNumber / 4; while (numToSubtract >= 1) { while (!canAddSubtractOneFromNumber(highestNumber - numToSubtract)) { highestNumber = highestNumber - numToSubtract; } numToSubtract /= 2; } //And there was much rejoicing. Yay. console.log('HighestNumber = ' + highestNumber);

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

我这样写:

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;