超大整数的加法运算疑似存在bug
发布于 6 年前 作者 mingshen 6995 次浏览 来自 官方Issues

函数:

//======================testbug============================

function testlint() {

    var lint=1;

    for(var i=1;i<=22;i++)

    {

        lint=lint*10+1;

        console.log(“round”+i+",lint="+lint);

    }

}

我实验超大整数在乘法过程中并无问题,每次乘以10尾部加一个0.

但是位数较高的整数加法时好像有BUG,类似于加法竖式没有从个位对齐。

运行结果:注意,第16轮开始异常。

round1,lint=11

round2,lint=111

round3,lint=1111

round4,lint=11111

round5,lint=111111

round6,lint=1111111

round7,lint=11111111

round8,lint=111111111

round9,lint=1111111111

round10,lint=11111111111

round11,lint=111111111111

round12,lint=1111111111111

round13,lint=11111111111111

round14,lint=111111111111111

round15,lint=1111111111111111

round16,lint=11111111111111112

round17,lint=111111111111111120

round18,lint=1111111111111111200

round19,lint=11111111111111110000

round20,lint=111111111111111110000

round21,lint=1.1111111111111111e+21

round22,lint=1.1111111111111111e+22

1 回复

javascript 中最大安全整数是2^53-1(-9007199254740991~9007199254740991)

可以通过 Number.MAX_SAFE_INTEGER Number.MIN_SAFE_INTEGER 获取

超过这个值可以计算但不精确

回到顶部