【问题标题】:Difference Between Real calculation number and the one i calculate with javascript function实际计算数和我用 javascript 函数计算的数之间的区别
【发布时间】:2014-08-17 23:44:22
【问题描述】:

这是我为 exponentiation 编写的函数,类似于 JavaScript 中的 Math.pow 函数。

代码:

function tavan(){
var adad=document.getElementById("pain").value;
var tavan=document.getElementById("bala").value;    
var kol=adad;
while((tavan-1)>0)
{
kol=kol*adad;
--tavan;    

}
document.getElementById("main").innerHTML=kol;              
}

测试此功能后,一切正常。然后我测试这个短语 4^28,结果是 720575940379279**40**。在 windows 计算器 中它是 720575940379279**36** 我的函数号比真实的大 4。

【问题讨论】:

    标签: javascript function while-loop


    【解决方案1】:

    Javascript 中使用的浮点格式有 56 位尾数。准确存储4^28 需要 57 位。所以去掉最低位,增加指数,得到一个近似的结果。

    Windows 计算器可能使用任意精度的整数数学运算,这允许它精确地显示更大的数字。见:

    What is the standard solution in Javascript for handling big numbers (BigNum)?

    用于 Javascript 的类似库。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 2020-11-22
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    相关资源
    最近更新 更多