FAQ Topic - Why does simple decimal arithmetic give strange results?

F

FAQ server

-----------------------------------------------------------------------
FAQ Topic - Why does simple decimal arithmetic give strange
results?
-----------------------------------------------------------------------

For example, 5*1.015 does not give exactly 5.075 and 0.06+0.01 does
not give exactly 0.07 in javascript.

Javascript numbers are represented in binary as IEEE-754 (IEC 559)
Doubles, with a resolution of 53 bits, giving an accuracy of
15-16 decimal digits; integers up to about 9e15 are precise, but
few decimal fractions are. Given this, arithmetic is as exact
as possible, but no more. Operations on integers are exact if
the true result and all intermediates are integers within that
range.

In particular, non-integer results should not normally be
compared for equality; and non-integer computed results
commonly need rounding; see 4.6.

http://msdn.microsoft.com/library/d...html/c7a6bd3a-4b1c-4dbe-8505-106dbf483b41.asp


===
Postings such as this are automatically sent once a day. Their
goal is to answer repeated questions, and to offer the content to
the community for continuous evaluation/improvement. The complete
comp.lang.javascript FAQ is at http://jibbering.com/faq/index.html.
The FAQ workers are a group of volunteers.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected].
For example, 5*1.015 does not give exactly 5.075 and 0.06+0.01 does
not give exactly 0.07 in javascript.

True; but "exactly 0.07" is ambiguous. How best can we state, briefly
BOTH that (0.06+0.01).toString() -> "0.06999999999999999"
AND that (0.06+0.01) - 0.07 != 0.0 // ??

The recent short list of FAQ changes has an error.
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top