0
9007199254740992 + 1 = 9007199254740992 

But

9007199254740992 + 2 = 9007199254740994

Can anyone explain here hows this working under the hood? And how Javascript is able to store bigger Number than its max limit ?

intekhab
  • 1,566
  • 1
  • 13
  • 19
  • Read => http://stackoverflow.com/questions/307179/what-is-javascripts-highest-integer-value-that-a-number-can-go-to-without-losin – i.krivosheev Jun 22 '15 at 07:07
  • floating-point numbers have limited precision. There's probably a duplicate somewhere but I can't find it easily. edit: dammit – o11c Jun 22 '15 at 07:07
  • I have seen the ur link already that didnt give me what i want to know. – intekhab Jun 22 '15 at 07:10
  • Lots of duplicates. The marked duplicate has links to others… – RobG Jun 22 '15 at 07:12
  • 1
    Have a look at https://en.wikipedia.org/wiki/Double-precision_floating-point_format. *"Between 2^52=4,503,599,627,370,496 and 2^53=9,007,199,254,740,992 the representable numbers are exactly the integers. For the next range, from 2^53 to 2^54, everything is multiplied by 2, so the representable numbers are the even ones, etc."* Basically, if you have numbers >= 2^53, you are loosing the least significant bit, and hence only even numbers can be represented. – Felix Kling Jun 22 '15 at 07:46
  • 1
    Here is a simplified example: `1000` this is `2^8` in binary. Now imagine this to be equivalent to `2^53`, so we loose the least significant bit (indicated by the `|` (not to be confused with bitwise OR!!)): `100|0`. I.e. we are only storing 3 bits. If you `+ 1`, you get `100|1`. But because we are only storing 3 bits (left of the `|`), the value does not change. If `+ 2` we get `101|0`. This changed one of the bits we store, hence the value changes. `2^53` is *not* JavaScript's limit. All values are 64bit, but they are *doubles* `2^53` is the max integer that can be represented precisely. – Felix Kling Jun 22 '15 at 07:51

0 Answers0