这是由语言定义的吗?有确定的最大值吗?在不同的浏览器中是否有所不同?
当前回答
在谷歌Chrome内置javascript,你可以去大约2^1024之前的数字被称为无穷大。
其他回答
在JavaScript中,有一个数字叫做Infinity。
例子:
(Infinity>100)
=> true
// Also worth noting
Infinity - 1 == Infinity
=> true
Math.pow(2,1024) === Infinity
=> true
对于这个主题的一些问题,这可能已经足够了。
为了安全
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);
}());
253 == 9 007 199 254 740 992。这是因为数字存储为52位尾数中的浮点数。
最小值为-253。
这使得一些有趣的事情发生了
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
让我们来看看源头
描述
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.
浏览器兼容性
在谷歌Chrome内置javascript,你可以去大约2^1024之前的数字被称为无穷大。
推荐文章
- 我如何检查如果一个变量是JavaScript字符串?
- 如何检测如果多个键被按下一次使用JavaScript?
- 如何通过history. pushstate获得历史变化的通知?
- 使用jQuery改变输入字段的类型
- 在JavaScript中,什么相当于Java的Thread.sleep() ?
- 使用jQuery以像素为整数填充或边距值
- 检查是否选择了jQuery选项,如果没有选择默认值
- Next.js React应用中没有定义Window
- 如何重置笑话模拟函数调用计数之前,每次测试
- 如何强制一个功能React组件渲染?
- 在javascript中从平面数组构建树数组
- 将Dropzone.js与其他字段集成到现有的HTML表单中
- 如何在AngularJS中观察路由变化?
- JavaScript DOM删除元素
- 将dd-mm-yyyy字符串转换为日期