为什么JavaScript认为354224848179262000000和354224848179261915075相等? [英] Why does JavaScript think 354224848179262000000 and 354224848179261915075 are equal?

查看:129
本文介绍了为什么JavaScript认为354224848179262000000和354224848179261915075相等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我开始尝试使用递归函数找到第100个Fibonacci数,并使用以下代码记住函数。

So, I started out by trying to find the 100th Fibonacci number using a recursive function and by memoizing the function using the following code.

Function.prototype.memoize = function () {
    var originalFunction = this,
        slice = Array.prototype.slice;
        cache = {};
    return function () {
        var key = slice.call(arguments);
        if (key in cache) {
            return cache[key];
        } else {
            return cache[key] = originalFunction.apply(this, key);
        }
    };
};

var fibonacci = function (n) {
    return n === 0 || n === 1 ? n : fibonacci(n - 1) + fibonacci(n - 2);
}.memoize();

console.log(fibonacci(100));

现在,你可以在这个小提琴,JavaScript记录 354224848179262000000 作为结果。根据 WolframAlpha <,第百个斐波纳契数实际上是 354224848179261915075 / a>这是正确的。

Now, as you can see in this fiddle, JavaScript logs 354224848179262000000 as the result. The hundredth Fibonacci number is actually 354224848179261915075 according to WolframAlpha which is correct.

现在,我的问题是这个。 为什么数字计算错误,即使算法完全正确?我的想法指向JavaScript,因为根据 Google的计算器 1 ,两个数字相等。

Now, my question is this. Why does the number compute incorrectly, even though the algorithm is completely sane? My thoughts point to JavaScript because according to Google's calculator1, the two numbers are equal.

导致此类错误的JavaScript是什么?该数字安全地在IEEE 754号码的最大值范围内, 1.7976931348623157e + 308

What is it about JavaScript that causes such an error? The number is safely within limits of the maximum value of an IEEE 754 number, which is 1.7976931348623157e+308.

1 如果这可能是我平台上的错误,我已经在Ubuntu上的Chromium和Firefox上进行了测试。

1In case this could be a bug on my platform, I have tested this on both Chromium and Firefox on Ubuntu.

推荐答案

随着你的号码越来越大,你的丢失精度,JavaScript中的最大安全数实际上是 Number.MAX_SAFE_INTEGER === 9007199254740991

As your number gets bigger, your lose precision, the maximum safe number in JavaScript is actually Number.MAX_SAFE_INTEGER === 9007199254740991

对于每个需要的额外位,你会失去1位精度,因为最后一位被假定为零。

For every extra bit that is required you lose 1 bit of precision because the last bit is assumed to be zero.

根据至IEEE754 354224848179262000000等于二进制:

according to IEEE754 354224848179262000000 equals in binary to:

0 10001000011 0011001100111101101101110110101001111100010110010110

指数10001000011是1091,如果你减去1023,结果为68.

这意味着你使用68位代表你的重要因素,因为他们只有52位可用对于有效值,假设最后16位为零。
任何落在这16位的计算都没有效果。

The exponent, 10001000011 is 1091, which results to 68 if you subtract 1023.
This means you are using 68 bits to represent your significant, since their are only 52 bits available for the significant, the last 16 bits are assumed to be zero. Any calculations that fall in those 16 bits will have no effect.

这篇关于为什么JavaScript认为354224848179262000000和354224848179261915075相等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆