2

When I execute

parseInt(8558968890839370929)

in Chrome, it returns 8558968890839371000. Why does it not return 9,007,199,254,740,992 (253) instead?

Markate Su
  • 33
  • 1
  • 7

1 Answers1

5

Because the range of int is crossed. The maximum value of int can be +/- 9007199254740992

From the ECMA

Note that all the positive and negative integers whose magnitude is no greater than 253 are representable in the Number type (indeed, the integer 0 has two representations, +0 and −0).

6502
  • 112,025
  • 15
  • 165
  • 265
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • Yes,in javascript,the type of number is crossed when it comes 2^53,so i think it must return me Math.pow(2,53),but it 's not the result – Markate Su Apr 22 '14 at 05:42
  • 1
    @user3559031 I suspect that it ended up over-flowing and wrapping back around to `8558968890839371000`. –  Apr 22 '14 at 05:44
  • 1
    i don't think it.you can try in chrome that parseInt(8558968890839371001) turn out to be 8558968890839371000, and parseInt(8558968890839371002) turn out to be 8558968890839371000, too ,if it wrapping back,it should not be the same result with parseInt(8558968890839371001)and parseInt(8558968890839371002).It seems that the number come accross the max value. – Markate Su Apr 22 '14 at 05:49
  • @user3559031 alright, you've convinced me that I actually have no idea what I'm talking about :/ –  Apr 22 '14 at 05:52
  • This question has been asked hundreds of times. Duplicate of many. – jfriend00 Apr 22 '14 at 05:57
  • I want to know how it runs. @jfriend00,can you show me the answer or the link ? – Markate Su Apr 22 '14 at 06:01
  • 1
    @user3559031 - What do you mean "how it runs"? If you want to see the JS code that does what you see, you can go look at the webkit or Firefox code (they are open source). You're exceeding the max range of integers so you lose precision. Not much more to say than that. I linked to one of the dup questions above and voted to close as a dup. – jfriend00 Apr 22 '14 at 06:03
  • @jfriend00 Sorry, i know that it lose precision,but why it happens that 8558968890839370929 becomeing 8558968890839371000. – Markate Su Apr 22 '14 at 06:13
  • 2
    @user3559031 - because it lost precision. Limits on precision means that is can't keep track of that many digits so the last digits are no longer accurate. Don't exceed the max integer that javascript supports if you want precision. – jfriend00 Apr 22 '14 at 06:20
  • @markate, @jfriend00 , Actually, I think that this has more to do with floating point math. All numbers in javascript are floats. Although `parseInt` parses strings for integers, it outputs them as floats. In JS, all floats have 16 digits of precision. When you enter `8558968890839370929`, a number with 19 digits, JS rounds it so that it has only 16 significant digits. ie, it makes the last three zero. – mindoftea Apr 23 '14 at 20:32