我在处理 JavaScript 数字时发现了以下奇怪的行为。
var baseNum = Math.pow(2, 53);
console.log(baseNum); //prints 9007199254740992
console.log(baseNum + 1); //prints 9007199254740992 again!
console.log(baseNum + 2); //prints 9007199254740994, 2 more than +1
console.log(baseNum + 3) // prints 9007199254740996, 2 more than +2
console.log(baseNum + 4) // prints 9007199254740996, same as +3
这里发生了什么?我知道 JavaScript 最多只能表示 2^53
的数字(它们在内部是“ double ”?),但为什么会出现这种行为?
如果 2^53
是实际最大值,那为什么我们有 Number.MAX_VALUE
(1.7976931348623157e+308
)?
最佳答案
这个数字真的是双倍的。尾数有 52 位 ( source and extra information on doubles )。因此,存储 2^53 会砍掉一位。
数字使用 3 个符号位(非常简单)和另外两个部分(尾数 M 和指数 E)存储。数字计算如下:
(1 + M/2^53) * 2^(E-1023)
我可能在某些细节上有一些偏差,但基本思想就在那里。所以当数字为2^53时,2^(E-1023) = 2^53,由于M只有52位,所以不能再表示最低位。
关于javascript - 奇怪的 JavaScript 数字行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12595232/