1

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.

Cary Bondoc
  • 2,923
  • 4
  • 37
  • 60

1 Answers1

2

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.

Community
  • 1
  • 1
Barmar
  • 741,623
  • 53
  • 500
  • 612