Stange functionality in javascript numbers

Possible duplicate:
Big numbers mistakenly rounded in Javascript

I am experiencing weird behavior when working with numbers in javascript. When I use the following code:

<a href="javascript:console.log({'id':9200000000032337}.id);"> CLICK HERE </a> 

I get the number 920000000003233 6 in my console. I think this is most likely with rounding or maximum values ​​for numbers, but I don't understand it completely. Is anyone

+4
source share
1 answer

I am not a Javascript expert, but it looks like your number is stored as a 64-bit IEEE-754 floating point number. Of course, what I get from C # code that will display the exact double value:

 double d = 9200000000032337; Console.WriteLine(DoubleConverter.ToExactString(d)); 

(Using my own DoubleConverter class.) My output is the same as yours: 9200000000032336

Floating-point values ​​accurately store a certain number of significant digits exactly - and when numbers get high enough, even integers cannot be stored exactly.

ECMA-262 seems to confirm this:

4.3.19
Numerical value
A primitive value corresponding to a 64-bit binary precision double format. IEEE 754 Value

and from section 7.8.3 (numeric literals):

A numeric literal denotes a value of type Number. This value is determined in two stages: firstly, the mathematical value (MV) is obtained from the literal; secondly, this mathematical value is rounded as described below.

Section 8.5 contains more detailed information.

+11
source

Source: https://habr.com/ru/post/1442923/


All Articles