0

number 10205558742352809 gets reduced to 10205558742352808. What?

  • 1
    exactly how are you using this number? and note that in JS, all numbers are fundamentally floats, and your number exceeds the 2**53 range that ints can be represented in exactly. – Marc B Dec 21 '15 at 16:10
  • 1
    Take a look at [this post](http://stackoverflow.com/questions/307179/what-is-javascripts-highest-integer-value-that-a-number-can-go-to-without-losin). It should explain everything. The problem is that you exceeded the maximum integer value that can be used safely. – spac3nerd Dec 21 '15 at 16:18

1 Answers1

1

TL;DR: Floating-point numbers can be screwy.

Long Answer

Javascript uses 64-bit floating point numbers (IEEE-754). The binary that makes up a floating point number is composed of 3 parts: sign bit (1 bit), exponent field (11 bits), and the significand field (52 bits). There are plenty of sources online that go into further detail about how this works.

If we convert 10205558742352809 to it's binary format then interpret that as hexadecimal for readability we get:

434220F367C941D4

Which is the same thing you'd get for 102...09. Compare that with 102...10:

434220F367C941D5

So 102...09 is not directly representable in IEEE-754 and in fact lies on the border between 102...08 and 102...10.

Mike Cluck
  • 31,869
  • 13
  • 80
  • 91