Skip to content

Instantly share code, notes, and snippets.

@Daniel-Wiedemann
Created February 7, 2014 16:33
Show Gist options
  • Save Daniel-Wiedemann/f5854772e7d0749c0700 to your computer and use it in GitHub Desktop.
Save Daniel-Wiedemann/f5854772e7d0749c0700 to your computer and use it in GitHub Desktop.
Large numbers erroneously rounded in Javascript // AJAX, JSON response/request
Erklärung zu dem Problem:
http://stackoverflow.com/questions/1379934/large-numbers-erroneously-rounded-in-javascript
http://www.2ality.com/2012/04/number-encoding.html
What you're seeing here is actually the effect of two roundings. Numbers in ECMAScript are internally represented double-precision floating-point. When id is set to 714341252076979033 (0x9e9d9958274c359 in hex), it actually is assigned the nearest representable double-precision value, which is 714341252076979072 (0x9e9d9958274c380). When you print out the value, it is being rounded to 15 significant decimal digits, which gives 14341252076979100.
JavaScript uses double precision floating point values, ie a total precision of 53 bits, but you need
ceil(lb 714341252076979033) = 60
bits to exactly represent the value.
The nearest exactly representable number is 714341252076979072 (write the original number in binary, replace the last 7 digits with 0 and round up because the highest replaced digit was 1).
You'll get 714341252076979100 instead of this number because ToString() as described by ECMA-262, §9.8.1 works with powers of ten and in 53 bit precision all these numbers are equal.
The remaining 9007199254740990 (that is, 253−2) values are denormalized, having the form
s × m × 2e
where s is +1 or −1, m is a positive integer less than 252, and e is −1074.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment