Difference Between Real calculation number and the one i calculate with javascript function

北城余情 提交于 2019-12-14 02:33:44

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/24442089/difference-between-real-calculation-number-and-the-one-i-calculate-with-javascri

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!