4

So in JavaScript, 111111111111111111111 == 111111111111111110000. Just type any long number – at least about 17 digits – to see it in action ;-)

That is because JavaScript uses double-precision floating-point numbers, and certain very long numeric literals can not be expressed accurately. Instead, those numbers get rounded to the nearest representable number possible. See e.g. What is JavaScript's highest integer value that a Number can go to without losing precision?

However, doing the math according to IEEE-754, I found out that every number inside [111111111111111106560, 111111111111111122944] will be replaced by 111111111111111114752 internally. But instead of showing this ...4752 number, it gets displayed as 111111111111111110000. So JavaScript is showing those trailing zeros which obfuscates the real behavior. This is especially annoying because even numbers like 263, which are accurately representable, get "rounded" as described.

So my question is: Why does JavaScript behave like this when displaying the number?

Community
  • 1
  • 1
purefanatic
  • 933
  • 2
  • 8
  • 23
  • Since all the integers in that range are treated equivalently, one of them is chosen arbitrarily for display. It makes sense to show zeroes for all the digits outside the range of precision. – Barmar Aug 21 '14 at 00:44
  • I'll bet there's something in the ECMAScript specification that mandates using zeroes for this, but I'm not interested enough to go look it up. – Barmar Aug 21 '14 at 00:45
  • Yes, that will probably be the case. I just thought maybe someone knew the exact reasoning behind that, because in the C programming language for example, I have never seen such strange zeros (using `printf("%lf", n);`). – purefanatic Aug 21 '14 at 00:50
  • Thank you for improving my question Jan Doggen and Paul. I need to work on my wording a bit. – purefanatic Aug 21 '14 at 12:51

1 Answers1

4

JavaScript integers can only be +/- 253, which is:

9007199254740992

One of your numbers is

111111111111111106560

which is considerably outside of the range of numbers that can accurately represented as an integer.

This follows the IEEE 754:

  • Sign bit: 1 bit
  • Exponent width: 11 bits
  • Significand precision: 53 bits (52 explicitly stored)

EDIT

The Display of numbers is sometimes rounded by the JavaScript engine, yes. However, that can be over-ridden using the toFixed method. (Warning, toFixed is known to be broken under some versions of IE).

In your console, type:

111111111111111122944..toFixed(0)

"111111111111111114752"

Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74
  • There are many more integers, which can be represented accurately using IEEE-754 double precision. They just don't necessarily have neighbors ;-) Those Integers are the subject of my question - I think they should be displayed accurately. – purefanatic Aug 21 '14 at 00:43
  • Now that's good news, being able to at least display them somehow right! Didn't know that! – purefanatic Aug 21 '14 at 00:56