By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,250 Members | 1,631 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,250 IT Pros & Developers. It's quick & easy.

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

P: n/a
-----------------------------------------------------------------------
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/de...6dbf483b41.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.

Jun 14 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
dd
On Jun 15, 1:00 am, "FAQ server" <javascr...@dotinternet.bewrote:
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.

Jun 15 '07 #2

P: n/a
In comp.lang.javascript message <11*********************@g4g2000hsf.goog
legroups.com>, Fri, 15 Jun 2007 02:31:26, dd <dd****@gmail.composted:
>On Jun 15, 1:00 am, "FAQ server" <javascr...@dotinternet.bewrote:
>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.
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.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Jun 15 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.