什么是一个数字可以达到的JavaScript的最高整数值,而不会失去精度?

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

+/- 9007199254740991

ECMA第8.5节 – 数字

请注意,所有大小不超过2 53的正整数和负整数可用Numbertypes表示(实际上,整数0有两个表示,+0和-0)。

它们是64位浮点值,最大精确积分值是2 53 -1或9007199254740991 。 在ES6中,这被定义为Number.MAX_SAFE_INTEGER 。

请注意,按位运算符和移位运算符以32位整数运算,所以在这种情况下,最大安全整数是2 31 -1或2147483647。


testing一下!

 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 

从参考 :

 alert([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 

它是2 53 == 9 007 199 254 740 992.这是因为Number s被作为浮点存储在52位尾数中。

最小值是-2 53

这使得一些有趣的事情发生

 Math.pow(2, 53) == Math.pow(2, 53) + 1 >> true 

而且也可能是危险的:)

 var MAX_INT = Math.pow(2, 53); // 9 007 199 254 740 992 for (var i = MAX_INT; i < MAX_INT + 2; ++i) { // infinite loop } 

进一步阅读: http : //blog.vjeux.com/2010/javascript/javascript-max_int-number-limits.html

在JavaScript中有一个叫做Infinity的数字。

例子:

 (Infinity>100) => true // Also worth noting Infinity - 1 == Infinity => true Math.pow(2,1024) === Infinity => true 

对于有关这个主题的一些问题,这可能就足够了。

吉米的答案正确地表示连续的JavaScript整数频谱为-90071992547409929007199254740992 (包括9007199254740993,可能你认为你是9007199254740993,但你错了! )。

然而,没有任何答案能够以编程的方式发现/certificate(除了在28.56年之后的答案中提到的CoolAJ86之外),所以这里有一个更有效率的方法来做到这一点(更确切地说,约28.559999999968312年:),随着testing小提琴 :

 /** * 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); 

为了安全起见

 var MAX_INT = 4294967295; 

推理

我以为我会很聪明,find一个更实用的方法x + 1 === x的价值。

我的机器每秒钟只能计算1000万…所以我会在28.56年回复最后的答案。

如果你不能等那么久,我敢打赌

  • 大多数循环不运行28.56年
  • 9007199254740992 === Math.pow(2, 53) + 1足够certificate
  • 你应该坚持4294967295这是Math.pow(2,32) - 1 ,以避免预期的位移

寻找x + 1 === x

 (function () { "use strict"; var x = 0 , start = new Date().valueOf() ; while (x + 1 != x) { if (!(x % 10000000)) { console.log(x); } x += 1 } console.log(x, new Date().valueOf() - start); }()); 

ECMAScript 6:

 Number.MAX_SAFE_INTEGER = Math.pow(2, 53)-1; Number.MIN_SAFE_INTEGER = -Number.MAX_SAFE_INTEGER; 

简短的答案是“这取决于”。

如果您在任何位置使用按位运算符(或者如果您指的是数组的长度),则范围为:

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

(-(-1>>>1)-1)…(-1>>>1)

(恰巧,按位运算符和数组的最大长度限制为32位整数。)

如果您不使用按位运算符或使用数组长度:

签名: (-Math.pow(2,53))…(+Math.pow(2,53))

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

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

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

在Chrome 30中,在不到一毫秒的时间内,我就可以得到9007199254740992。

它将testing2的权力,以找出哪一个“加”1等于他自己。

您要用于按位操作的任何内容必须介于0x80000000(-2147483648或-2 ^ 31)和0x7fffffff(2147483647或2 ^ 31 – 1)之间。

控制台会告诉你,0x80000000等于+2147483648,但是0x80000000和0x80000000等于-2147483648。

我用一个公式X – (X + 1)= – 1做了一个简单的testing,XI的最大值可以在Safari,Opera和Firefox上testing(在OS X上testing)是9e15。 这是我用来testing的代码:

 javascript: alert(9e15-(9e15+1)); 

我这样写:

 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; 

尝试:

 maxInt = -1 >>> 1 

在Firefox 3.6中是2 ^ 31 – 1。

在谷歌浏览器内置的JavaScript中,你可以在数字被称为无穷大之前去约2 ^ 1024。

Scato wrotes:

任何你想用于按位操作的东西必须在0x80000000(-2147483648或-2 ^ 31)和0x7fffffff(2147483647或2 ^ 31 – 1)之间。

控制台会告诉你0x80000000等于+2147483648,但是0x80000000和0x80000000等于-2147483648

hex小数是无符号正值,所以0x80000000 = 2147483648 – 这在math上是正确的。 如果你想使它成为一个有符号的值,你必须右移:0x80000000 >> 0 = -2147483648。 你也可以写1 << 31。

Number.MAX_VALUE表示JavaScript中可表示的最大数值。

由于似乎没有人这样说,所以在v8引擎中, 31 bits数字和数字在行为上存在差异。

如果你有32 bits你可以使用第一位来告诉JavaScript引擎该数据是什么types,剩下的位包含实际的数据。 这就是V8对于31 bis numbers一个小的优化(或者是用来做,我的资料来源相当过时)。 最后31 bits是数值,然后是第一位,告诉引擎是数字还是对象引用。

但是,如果你使用31 bits以上的数字,那么数据将不适合,数字将64位加框,优化不会在那里。

在下面的video中,底线是:

喜欢可以表示为31位有符号整数的数值。

  • 有趣的video
  • 其他来源

Firefox 3似乎没有大数目的问题。

1e + 200 * 1e + 100会计算罚款1e + 300。

Safari似乎也没有问题。 (如果有其他人决定对此进行testing,请在Mac上注明。)

除非我在这个时间的这个时候失去了我的大脑,这是比64位整数大。

Node.js和Google Chrome似乎都使用了1024位浮点值,所以:

 Number.MAX_VALUE = 1.7976931348623157e+308