实际计算数与我用javascript函数计算的数之间的差异 [英] Difference Between Real calculation number and the one i calculate with javascript function

查看:153
本文介绍了实际计算数与我用javascript函数计算的数之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我为指数为JavaScript中的 Math.pow 函数编写的函数。



代码:

 函数tavan(){
var adad = document.getElementById( 。疼痛)值;
var tavan = document.getElementById(bala).value;
var kol = adad; $((tavan-1)> 0)

{
kol = kol * adad;
--tavan;

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

测试完这个函数后,一切似乎都正确。然后我测试这个短语4 ^ 28,结果是720575940379279 ** 40 **。在 windows计算器中,它是720575940379279 ** 36 **
我的函数数量比实数大4。 / div>

Javascript中使用的浮点格式有56位尾数。要准确存储 4 ^ 28 需要57位。因此,最低有效位将被丢弃,并且指数会增加,并且您会得到一个近似结果。

Windows计算器可能使用任意精度整数数学运算,更精确的数字。请参阅:

什么是Javascript中用于处理大数字的标准解决方案(BigNum)?



用于Javascript的类似库。


This is the function that I write for exponentiation like the Math.pow function in JavaScript.

Code:

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;              
}

After testing this function, everything seems right. Then I test this Phrase 4^28 and the result is 720575940379279**40**. In windows calculator it was 720575940379279**36** my function number was 4 bigger than real.

解决方案

The floating point format used in Javascript has 56 bits of mantissa. To store 4^28 accurately requires 57 bits. So the least significant bit is dropped and the exponent is increased, and you get an approximate result.

The Windows calculator probably uses arbitrary precision integer math, which allows it to show larger numbers precisely. See:

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

for similar libraries for Javascript.

这篇关于实际计算数与我用javascript函数计算的数之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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