这是由语言定义的吗?有确定的最大值吗?在不同的浏览器中是否有所不同?
当前回答
JavaScript在ECMAScript 2020中收到了一个新的数据类型:BigInt。它引入了带有“n”后缀的数值字面量,并允许任意精度:
var a = 123456789012345678901012345678901n;
当然,当这样大的整数(可能是无意的)被强制转换为数字数据类型时,精度仍然会丢失。
而且,很明显,由于有限的内存,总是会有精度限制,为了分配必要的内存和对如此大的数字执行算术,在时间方面会有成本。
例如,生成一个具有10万个十进制数字的数字,在完成之前会有明显的延迟:
console.log(BigInt("1".padEnd(100000,"0")) + 1n)
...但它确实有效。
其他回答
>= 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
我用一个公式做了一个简单的测试,X-(X+1)=-1,我在Safari、Opera和Firefox(在OS X上测试)上可以得到的X的最大值是9e15。下面是我用于测试的代码:
javascript: alert(9e15-(9e15+1));
让我们来看看源头
描述
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中,有一个数字叫做Infinity。
例子:
(Infinity>100)
=> true
// Also worth noting
Infinity - 1 == Infinity
=> true
Math.pow(2,1024) === Infinity
=> true
对于这个主题的一些问题,这可能已经足够了。
在谷歌Chrome内置javascript,你可以去大约2^1024之前的数字被称为无穷大。
推荐文章
- 在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中根据键值查找和删除数组中的对象