473,472 Members | 2,155 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1937
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: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.