这是由语言定义的吗?有确定的最大值吗?在不同的浏览器中是否有所不同?
当前回答
JavaScript有两种数字类型:number和BigInt。
最常用的数字类型是一个64位浮点IEEE 754数字。
这种类型的最大精确整数值是Number。MAX_SAFE_INTEGER,即:
253 - 1 +/- 9,007,199,254,740,991,或 九千亿亿七千亿九千九百零五十四万四千九百九十一
从这个角度来看:1千万亿字节是1拍字节(或1千兆字节)。
在此上下文中,“安全”指的是准确表示整数并正确比较它们的能力。
来自规范:
注意所有大小为no的正整数和负整数 在Number类型中可以表示大于253的 整数0有两种表示,+0和-0)。
为了安全地使用大于此值的整数,您需要使用BigInt,它没有上界。
注意,位操作符和移位操作符作用于32位整数,因此在这种情况下,最大安全整数是231-1,即2,147,483,647。
Const log = console.log Var x = 9007199254740992 Var y = -x Log (x == x + 1) // true ! Log (y == y - 1) //也成立! //算术运算符有效,但位/移位只对int32运算 Log (x / 2) // 4503599627370496 Log (x >> 1) // 0 Log (x | 1) // 1
关于数字9,007,199,254,740,992的技术注意事项:这个值有一个精确的IEEE-754表示,您可以从变量中赋值和读取这个值,因此对于在小于或等于该值的整数领域中非常仔细选择的应用程序,您可以将其视为最大值。
在一般情况下,必须将此IEEE-754值视为不精确值,因为它是对逻辑值9,007,199,254,740,992还是9,007,199,254,740,993进行编码是不明确的。
其他回答
>= 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
其他人可能已经给出了一般的答案,但我认为给出一个快速确定它的方法会是一个好主意:
for (var x = 2; x + 1 !== x; x *= 2);
console.log(x);
这给了我9007199254740992在Chrome 30不到一毫秒。
它将测试2的幂,以找出当“加”1时,哪一个等于他自己。
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);
让我们来看看源头
描述
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.
浏览器兼容性
JavaScript有两种数字类型:number和BigInt。
最常用的数字类型是一个64位浮点IEEE 754数字。
这种类型的最大精确整数值是Number。MAX_SAFE_INTEGER,即:
253 - 1 +/- 9,007,199,254,740,991,或 九千亿亿七千亿九千九百零五十四万四千九百九十一
从这个角度来看:1千万亿字节是1拍字节(或1千兆字节)。
在此上下文中,“安全”指的是准确表示整数并正确比较它们的能力。
来自规范:
注意所有大小为no的正整数和负整数 在Number类型中可以表示大于253的 整数0有两种表示,+0和-0)。
为了安全地使用大于此值的整数,您需要使用BigInt,它没有上界。
注意,位操作符和移位操作符作用于32位整数,因此在这种情况下,最大安全整数是231-1,即2,147,483,647。
Const log = console.log Var x = 9007199254740992 Var y = -x Log (x == x + 1) // true ! Log (y == y - 1) //也成立! //算术运算符有效,但位/移位只对int32运算 Log (x / 2) // 4503599627370496 Log (x >> 1) // 0 Log (x | 1) // 1
关于数字9,007,199,254,740,992的技术注意事项:这个值有一个精确的IEEE-754表示,您可以从变量中赋值和读取这个值,因此对于在小于或等于该值的整数领域中非常仔细选择的应用程序,您可以将其视为最大值。
在一般情况下,必须将此IEEE-754值视为不精确值,因为它是对逻辑值9,007,199,254,740,992还是9,007,199,254,740,993进行编码是不明确的。
推荐文章
- 如何禁用文本选择使用jQuery?
- 如何停止事件冒泡复选框点击
- 向对象数组添加属性
- 如何在Redux应用程序中动态加载代码分割的减速器?
- Angular 2+和debounce
- 检测涡旋方向
- 在react native中隐藏/显示组件
- 在网页上用鼠标模拟震颤(例如帕金森病)?
- console.log()和console.debug()的区别?
- HyperLogLog算法是如何工作的?
- 如何删除和清除所有的本地存储数据
- 如何从给定的html字符串中删除前导和尾随空白?
- 如何为KnockoutJS调试模板绑定错误?
- 将一个二进制的NodeJS Buffer转换为JavaScript的ArrayBuffer
- 检测浏览器标签是否有焦点