Read more

Heads up: JavaScript does not like big numbers

Tobias Kraze
November 10, 2014Software engineer at makandra GmbH

In a JavaScript console, type this:

> 9112347935156469760
9112347935156470000
Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

Ooops. And that's not a float!

This occurs because JavaScript uses double precision floats to store numbers.

So according to IEEE floating point definition Show archive.org snapshot only numbers between -(2^53 - 1) (-9007199254740991) and 2^53 - 1 (9007199254740991) can safely be represented in JavaScript.

Note that ECMAScript 6 will probably also offer Number.MAX_SAFE_INTEGER Show archive.org snapshot (and Number.MAX_SAFE_INTEGER) that point to those numbers, but browser support for that is still scarce.

For arbitrary large numbers (even >= 2^53), BigInt objects Show archive.org snapshot can be used starting with ECMAScript 2020. Recent versions of Chrome/FF/Opera already support the new class, but it won't work on other Browsers like IE11/Edge/Safari. (Last checked on January 2020)

Posted by Tobias Kraze to makandra dev (2014-11-10 18:09)