1
var num = 9094504735860991;
console.log(num); // 9094504735860992

Now I set a variable named num equals to 9094504735860991,but when logged, it changed to 9094504735860992.

Number.MAX_VALUE => 2^53 - 1 === 9007199254740000, however 9094504735860991 < 9007199254740000.

王吉志
  • 77
  • 3
  • 1
    you mean **9094504735860991 > 9007199254740000** right? – Matteo Tassinari Apr 11 '17 at 14:50
  • 1
    We'd need to see some code for more context into why this is happening. – Stese Apr 11 '17 at 14:51
  • Are you sure that's Number.MAX_VALUE? – Oliver Charlesworth Apr 11 '17 at 14:52
  • 2
    Short answer: Because beyond `Number.MAX_SAFE_INTEGER + 1` (`9007199254740991`), the IEEE-754 floating-point format can no longer represent every consecutive integer. `9007199254740991 + 1` is `9007199254740992`, but `9007199254740992 + 1` is also ``9007199254740992` because `9007199254740993` cannot be represented. The next that can be is `9007199254740994`. Long answer: See the linked question's answers. – T.J. Crowder Apr 11 '17 at 14:56
  • @T.J.Crowder - Please post this as an answer. Would help others!! – Pugazh Apr 11 '17 at 14:59
  • @Pugazh: Thanks. :-) Maybe I'll update [my answer to the linked question](http://stackoverflow.com/a/1379973/157247), which looks like it could be clearer via incorporating some of my comment above. – T.J. Crowder Apr 11 '17 at 15:04
  • Ugh, I wish I could fix the error in my comment above. `Number.MAX_SAFE_INTEGER + 1` is `9007199254740992`, not `9007199254740991`, `900719925474099` **is** `Number.MAX_SAFE_INTEGER`. – T.J. Crowder Apr 11 '17 at 15:48

0 Answers0