-3

Type this in the console of your browser:

9999999999999999==10000000000000000

It says they are equal, why?

Shai UI
  • 50,568
  • 73
  • 204
  • 309
  • It has to do with this: http://stackoverflow.com/questions/307179/what-is-javascripts-highest-integer-value-that-a-number-can-go-to-without-losin – David Sherret Nov 18 '14 at 18:38
  • Rounding. The number `9999999999999999` is too large to represent accurately (in Javascript). Try this: `alert(9999999999999999);` See what it alerts. – Matt Burland Nov 18 '14 at 18:38
  • 1
    Also see `Number.MAX_SAFE_INTEGER`. It's `9007199254740991` which is less than `9999999999999999` – Matt Burland Nov 18 '14 at 18:42
  • And finally, if you need to represent larger integers accurately, there are libraries available that will do that. – Matt Burland Nov 18 '14 at 18:43
  • A [Google search](https://www.google.com/search?q=9999999999999999%3D%3D10000000000000000) for that exact string returns multiple SO results. – admdrew Nov 18 '14 at 18:47
  • Downvoting this is just rude. It is not really a duplicate IMHO, the OP might not have even heard of precisions before, and it's okay, everyone is learning, or at least should be. – Zsolt Szatmari Oct 20 '15 at 13:28

1 Answers1

3

JavaScript only supports 53 bit integers

All numbers in JavaScript are floating point which means that integers are always represented as

sign × mantissa × 2exponent

The mantissa has 53 bits. You can use the exponent to get higher integers, but then they won’t be contiguous, any more. For example, you generally need to multiply the mantissa by two (exponent 1) in order to reach the 54th bit. However, if you multiply by two, you will only be able to represent every second integer:

David Chan
  • 7,347
  • 1
  • 28
  • 49