6

The following code

console.log(Math.pow(2, 53)); 
console.log(Math.pow(2, 53) + 1);

produces exactly same output for both calculations:

9007199254740992

Why?

Art
  • 23,747
  • 29
  • 89
  • 101
  • Possible duplicate: http://stackoverflow.com/questions/307179/what-is-javascripts-max-int-whats-the-highest-integer-value-a-number-can-go-to – Ivo Wetzel Jan 30 '11 at 01:03

2 Answers2

11

The result you see occurs because Math.Pow() is working with floating point numbers, and when you reach the 16th decimal digit, you can't necessarily add one to the least significant decimal digit of the value and expect the result to change.

There are typically, in a 64-bit (8-byte) IEEE 754 floating-point binary value, 53 bits for the mantissa (including the implied 1-bit). Your calculation Math.Pow(2, 53) requires 54 bits in the mantissa to be guaranteed of a change. If you add 2, you should see the change.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
-3

All Javascript numbers are double.

ponce
  • 919
  • 5
  • 15