【问题标题】:Incrementing MAX_SAFE_INTEGER in Javascript [duplicate]在 Javascript 中增加 MAX_SAFE_INTEGER [重复]
【发布时间】:2021-06-10 12:54:32
【问题描述】:

如何解释我得到的结果在前两次计算中以 92 结尾,在第三次计算中以 94 结尾?

    console.log(Number.MAX_SAFE_INTEGER + 1); //...92
    console.log(Number.MAX_SAFE_INTEGER + 2); //...92
    console.log(Number.MAX_SAFE_INTEGER + 3); //...94

【问题讨论】:

标签: javascript data-structures


【解决方案1】:

基于this Number.MAX_SAFE_INTEGER 是您安全计算中的最大整数,任何大于此的数字都是不安全的。

编辑

对于大于Number.MAX_SAFE_INTEGER 使用BigInt 如:

BigInt(Number.MAX_SAFE_INTEGER) + BigInt(2)

 alert(BigInt(Number.MAX_SAFE_INTEGER) + BigInt(1)); //9007199254740992n
 alert(BigInt(Number.MAX_SAFE_INTEGER) + BigInt(2)); //9007199254740993n
 alert(BigInt(Number.MAX_SAFE_INTEGER) + BigInt(3)); //9007199254740994n

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-26
    • 2021-07-27
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    • 2013-02-26
    • 2012-12-28
    • 2016-07-31
    相关资源
    最近更新 更多