这是由语言定义的吗?有确定的最大值吗?在不同的浏览器中是否有所不同?
当前回答
让我们来看看源头
描述
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.
浏览器兼容性
其他回答
Node.js和谷歌Chrome似乎都使用1024位浮点值,因此:
Number.MAX_VALUE = 1.7976931348623157e+308
为了安全
var MAX_INT = 4294967295;
推理
我认为我应该聪明一点,用更实用的方法找到x + 1 === x的值。
我的机器每秒只能计算1000万次左右……所以我会在28.56年后给出确切的答案。
如果你等不了那么久,我敢打赌
大多数循环都不会持续28.56年 9007199254740992 ===数学。Pow(2,53) + 1是足够的证明 您应该坚持使用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);
}());
我这样写:
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;
在谷歌Chrome内置javascript,你可以去大约2^1024之前的数字被称为无穷大。
简短的回答是“视情况而定。”
如果你在任何地方使用位操作符(或者如果你引用数组的长度),范围是:
无符号:0…(1 > > > 0)
签名:(-(1 > > > 1)1)……(1 > > > 1)
(恰巧按位操作符和数组的最大长度被限制为32位整数。)
如果你不使用位操作符或使用数组长度:
签名:(-Math pow (2.53)) ... + Math . pow (2.53))
这些限制是由“Number”类型的内部表示施加的,通常对应于IEEE 754双精度浮点表示。(请注意,与典型的有符号整数不同,由于内部表示的特征,负极限的大小与正极限的大小相同,它实际上包括一个负0!)
推荐文章
- 在React Native中使用Fetch授权头
- 为什么我的球(物体)没有缩小/消失?
- 如何使用jQuery检测页面的滚动位置
- if(key in object)或者if(object. hasownproperty (key)
- 一元加/数字(x)和parseFloat(x)之间的区别是什么?
- angularjs中的compile函数和link函数有什么区别
- 删除绑定中添加的事件监听器
- 很好的初学者教程socket.io?
- HtmlSpecialChars在JavaScript中等价于什么?
- React: 'Redirect'没有从' React -router-dom'中导出
- 如何在React中使用钩子强制组件重新渲染?
- 我如何使用Jest模拟JavaScript的“窗口”对象?
- Numpy Max vs amax vs maximum
- 我如何等待一个承诺完成之前返回一个函数的变量?
- 在JavaScript中根据键值查找和删除数组中的对象