473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

-----------------------------------------------------------------------
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
2 1934
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: Chris Croughton | last post by:
I notice the real reason for not posting "off-topic" here is that the result will be a long thread on whether the matter is off-topic or not -- caused by the very people who claim that they don't...
28
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
3
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does simple decimal arithmetic give strange results?...
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does simple decimal arithmetic give strange results?...
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does simple decimal arithmetic give strange results?...
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does simple decimal arithmetic give strange results?...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.