FAQ Topic - Why does 5 * 1.015 != 5.075 or 0.06+0.01 != 0.07?

F

FAQ server

-----------------------------------------------------------------------
FAQ Topic - Why does 5 * 1.015 != 5.075 or 0.06+0.01 !=
0.07?
-----------------------------------------------------------------------

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
generally 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

dd

Why does 5 * 1.015 != 5.075 or 0.06+0.01 != 0.07?

Perhaps this could use a bit more explanation. When
I first looked at this it's not obvious that it's
mostly just a comparison problem. Someone might
think "well if it's not 5.075 then JavaScript math
is crazy". It's not clear that the answer is really
something like 5.0750000001 and just needs a quick
multiply by 100, convert to integer and then divide
by 100 to make it correct.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
Perhaps this could use a bit more explanation. When
I first looked at this it's not obvious that it's
mostly just a comparison problem.

Ideally, that would be because it isn't mostly a comparison problem,
AFAICS; certainly not predominantly so. It's also a visible-rendition
problem.
Someone might
think "well if it's not 5.075 then JavaScript math
is crazy". It's not clear that the answer is really
something like 5.0750000001 and just needs a quick
multiply by 100, convert to integer and then divide
by 100 to make it correct.

That won't make the first example work.

But it would be better to put the Subject in English rather than
geekish, and use the Subject expressions as introductory.


4.7 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 ...

Then change digits; integers to digits. Integers for legibility.

And add the points that it's better to divide by 100 than to multiply by
0.01, since 100 is held exactly and 0.01 cannot be; and that one should
where possible work in integers throughout.
 

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
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top