这是由语言定义的吗?有确定的最大值吗?在不同的浏览器中是否有所不同?
当前回答
在JavaScript中,数字的表示是2^53 - 1。
然而,按位操作是在32位(4个字节)上计算的,这意味着如果你超过32位移位,你将开始丢失位。
其他回答
在JavaScript中,有一个数字叫做Infinity。
例子:
(Infinity>100)
=> true
// Also worth noting
Infinity - 1 == Infinity
=> true
Math.pow(2,1024) === Infinity
=> true
对于这个主题的一些问题,这可能已经足够了。
在谷歌Chrome内置javascript,你可以去大约2^1024之前的数字被称为无穷大。
的wrotes:
任何你想用于位操作的东西都必须在 0x80000000(-2147483648或-2^31)和0x7fffffff(2147483647或2^31 - 1)。 控制台会告诉您0x80000000等于+2147483648,但是 0x80000000 & 0x80000000 = -2147483648
十六进制小数是无符号的正数值,因此0x80000000 = 2147483648 -这在数学上是正确的。如果你想让它成为一个带符号的值,你必须右移:0x80000000 >> 0 = -2147483648。你也可以写成1 << 31。
为了安全
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);
}());
让我们来看看源头
描述
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.
浏览器兼容性
推荐文章
- 如何禁用文本选择使用jQuery?
- 如何停止事件冒泡复选框点击
- 向对象数组添加属性
- 如何在Redux应用程序中动态加载代码分割的减速器?
- Angular 2+和debounce
- 检测涡旋方向
- 在react native中隐藏/显示组件
- 在网页上用鼠标模拟震颤(例如帕金森病)?
- console.log()和console.debug()的区别?
- HyperLogLog算法是如何工作的?
- 如何删除和清除所有的本地存储数据
- 如何从给定的html字符串中删除前导和尾随空白?
- 如何为KnockoutJS调试模板绑定错误?
- 将一个二进制的NodeJS Buffer转换为JavaScript的ArrayBuffer
- 检测浏览器标签是否有焦点