0

In JavaScript, the Number data type is 64 bits long.

How then is Number.MAX_VALUE === 1.7976931348623157e+308 and Math.pow(2, 64) === 18446744073709552000? How can the Number data type only be 64 bits, but have a higher value than 2^64?

Also I noticed that Math.pow(2, 64) + 1 === Math.pow(2, 64) but Math.pow(2, 64) + 10000 !== Math.pow(2, 64) Why would 2^64 + 1 be equal to 2^64 and 2^64 + 10000 not?

Nick Gallimore
  • 1,222
  • 13
  • 31
  • 64-bit *floating point* numbers. Re-read both the question and the answer, and see the result of `Math.pow(2, 64) + 10000`. It has a lot of 0s at the end, which the actual result of `2^64 + 10000` doesn't have. – Federico klez Culloca Jan 15 '20 at 15:13
  • 1
    You can try this to see that the calculation is not exact: `Math.pow(2, 64) + 10000 - 10000 == Math.pow(2,64)`, it will return `false`. – Federico klez Culloca Jan 15 '20 at 15:14
  • Because `Number` in JavaScript are floating-point numbers. See https://stackoverflow.com/a/3439981/661843 how to avoid this problem. – verybadbug Jan 15 '20 at 15:15
  • In floating point numbers, 11 bits are reserved for the exponent, 52 bits for the mantissa. Making the number larger than what can be represented by 52 bits means a loss of precision. –  Jan 15 '20 at 15:24
  • How come the MAX_VALUE is much much larger than 2^64? – Nick Gallimore Jan 15 '20 at 15:55
  • 1
    Because the 11-bit exponent can go 10^(+/- 308). The mantissa is still restricted to 52 bits, though, so the end of the number will start filling with zeroes. –  Jan 15 '20 at 16:33
  • @Amy Thank you and yes it does. – Nick Gallimore Jan 15 '20 at 20:17

0 Answers0