problem with large numbers and javascript

D

david

Hi all,

I write in javascript
a= 17770000000000019
and the value in a gets actually 17770000000000020.

Why is it ?

Thanks,
David
 
D

David Mark

david said:
Hi all,

I write in javascript
a= 17770000000000019
and the value in a gets actually 17770000000000020.

Why is it ?

Because of the way Javascript (and many other languages) store numbers
behind the scenes. They don't store each digit as that would be very
inefficient, but a sign, mantissa and exponent. Google "floating point"
and "storage". The gist is that very large (or small), as well as
irrational numbers, may not render as you expect. They just don't have
the precision (nothing does for irrationals of course--you'd need
infinite memory and display capabiities).

As an aside, for completely different reasons (related to time zones),
Date objects can exhibit a similar identity crisis, so don't be like
Dojo - for (a bad) example - and use Date objects for canonical storage
(i.e. the day you think you store may end up being the previous when
rendered or read back through the object's properties).
 
R

Richard Cornford

I write in javascript
a= 17770000000000019
and the value in a gets actually 17770000000000020.

Why is it ?

Javascript's (single) numeric primitive type is stored as an IEEE 754
64 bit double precision floating point number. Such a value is
versatile but still limited in what can be accommodated in its 64
bits. For example, the _continuous_ range of integer values that it
can represent goes from -9007199254740991 to +9007199254740992 (2 to
the power of 53). Not all integers outside of that range can be
represented, and those that cannot will be approximated to the nearest
value that can be represented. Your number is outside of that range
and so is being represented by the nearest available value.

Note that sequences of characters entered as source text for numeric
literals will result in numeric values that are approximations of the
number they represent if they cannot be precisely represented by an
IEEE 754 64 bit floating point number, and that performing
mathematical operations on javascript number values that do precisely
represent some number value may result in values that could not be
represented and so will be represented as approximations. E.G.:-

alert(9007199254740992); // 9007199254740992 (precise)
alert(9007199254740992 + 1); // 9007199254740992 (approximated
result)
alert(9007199254740992 + 2); // 9007199254740994 (precise)
alert(9007199254740992 + 3); // 9007199254740996 (approximated
result)
alert(9007199254740992 + 4); // 9007199254740996 (precise)
alert(9007199254740992 + 5); // 9007199254740996 (approximated
result)
alert(9007199254740992 + 6); // 9007199254740998 (precise)

Richard.
 
D

david

Javascript's (single) numeric primitive type is stored as an IEEE 754
64 bit double precision floating point number. Such a value is
versatile but still limited in what can be accommodated in its 64
bits. For example, the _continuous_ range of integer values that it
can represent goes from -9007199254740991 to +9007199254740992 (2 to
the power of 53). Not all integers outside of that range can be
represented, and those that cannot will be approximated to the nearest
value that can be represented. Your number is outside of that range
and so is being represented by the nearest available value.

Note that sequences of characters entered as source text for numeric
literals will result in numeric values that are approximations of the
number they represent if they cannot be precisely represented by an
IEEE 754 64 bit floating point number, and that performing
mathematical operations on javascript number values that do precisely
represent some number value may result in values that could not be
represented and so will be represented as approximations. E.G.:-

alert(9007199254740992);      // 9007199254740992  (precise)
alert(9007199254740992 + 1);  // 9007199254740992  (approximated
result)
alert(9007199254740992 + 2);  // 9007199254740994  (precise)
alert(9007199254740992 + 3);  // 9007199254740996  (approximated
result)
alert(9007199254740992 + 4);  // 9007199254740996  (precise)
alert(9007199254740992 + 5);  // 9007199254740996  (approximated
result)
alert(9007199254740992 + 6);  // 9007199254740998  (precise)

Richard.

Thank you all for the great explanations
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,574
Members
47,207
Latest member
HelenaCani

Latest Threads

Top