473,320 Members | 2,097 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,320 software developers and data experts.

floating point numbers don't add right

Hi all,

I read a lot of info on this issue, but still don't get it. Why does
this happen?

document.write(".05" + " \+ " + ".05" + " \= " + (.05 + .05) +
"<br>");
document.write("1.05" + " \+ " + ".05" + " \= " + (1.05 + .05) +
"<br>");
document.write("2.05" + " \+ " + ".05" + " \= " + (2.05 + .05) +
"<br>");

Outputs:

..05 + .05 = 0.1
1.05 + .05 = 1.1
2.05 + .05 = 2.0999999999999996
I also tried the code provided in this group's FAQ, and it worked
except for when it reaches an "even" number which should display 6.00
for example, it displays 5.100.

thx!

J.
Jul 23 '05 #1
8 5219
On 23/9/04 9:14 pm, janice wrote:
Hi all,

I read a lot of info on this issue, but still don't get it. Why does
this happen?
(snip) 2.05 + .05 = 2.0999999999999996


In binary, these numbers go on for ever:

2.05 = 10.0000110011001100110011001100...
0.05 = 0.0000110011001100110011001100...
2.10 = 10.0001100110011001100110011001...

With a fixed number of bits it is impossible to represent these numbers
exactly, so they contain errors right from the start. Addition makes the
errors even larger. For example, if I added together the first two binary
numbers shown above, the last digit of the result would be 0, not 1.

It's just like adding 1/3 to 2/3 in base 10:

2/3 = 0.666666666666666
+ 1/3 = 0.333333333333333
= 0.999999999999999 (not 1.000000)

It is just as impossible to write 1/3 in decimal as it is to write 0.05 in
binary.

I hope that makes sense.

--
Philip Ronan
ph***********@virgin.net
(Please remove the "z"s if replying by email)
Jul 23 '05 #2
> I read a lot of info on this issue, but still don't get it. Why does
this happen? .05 + .05 = 0.1
1.05 + .05 = 1.1
2.05 + .05 = 2.0999999999999996


That's really annoying, isn't it? JavaScript uses binary floating point
notation internally to represent numbers. Floating point is very useful
for some scientific applications, but for other applications it has some
severe shortcomings.

In case, you are seeing that it cannot exactly represent decimal
fractions. That would be ok on a planet where people do not commonly use
decimal fractions. It might seem that floating point was a poor choice.

http://www.crockford.com/#javascript
Jul 23 '05 #3
Lee
Douglas Crockford said:
I read a lot of info on this issue, but still don't get it. Why does
this happen?

.05 + .05 = 0.1
1.05 + .05 = 1.1
2.05 + .05 = 2.0999999999999996


That's really annoying, isn't it? JavaScript uses binary floating point
notation internally to represent numbers. Floating point is very useful
for some scientific applications, but for other applications it has some
severe shortcomings.

In case, you are seeing that it cannot exactly represent decimal
fractions. That would be ok on a planet where people do not commonly use
decimal fractions. It might seem that floating point was a poor choice.

The advantages of using standard IEEE floating point representation
far outweigh any disadvantage. The infinitesimal error encountered
in typical web-page arithmetic is easily corrected by rounding.

When representing numbers in binary, each bit represents either
0 or 1 times 2 raised to some positive or negative integer power,
so a binary representation of a value between 0 and 1 is the sum
of the series (b1)/2 + (b2)/4 + (b3)/16 + (b4)/32 + ...
where each (bn) value is either 0 or 1. It's mathematically
impossible to represent some decimal fractions as a finite
series of such terms.

Jul 23 '05 #4
Lee wrote:
Douglas Crockford said:
I read a lot of info on this issue, but still don't get it. Why does
this happen?

.05 + .05 = 0.1
1.05 + .05 = 1.1
2.05 + .05 = 2.0999999999999996


That's really annoying, isn't it? JavaScript uses binary floating point
notation internally to represent numbers. Floating point is very useful
for some scientific applications, but for other applications it has some
severe shortcomings.

In case, you are seeing that it cannot exactly represent decimal
fractions. That would be ok on a planet where people do not commonly use
decimal fractions. It might seem that floating point was a poor choice.


The advantages of using standard IEEE floating point representation
far outweigh any disadvantage. The infinitesimal error encountered
in typical web-page arithmetic is easily corrected by rounding.

When representing numbers in binary, each bit represents either
0 or 1 times 2 raised to some positive or negative integer power,
so a binary representation of a value between 0 and 1 is the sum
of the series (b1)/2 + (b2)/4 + (b3)/16 + (b4)/32 + ...
where each (bn) value is either 0 or 1. It's mathematically
impossible to represent some decimal fractions as a finite
series of such terms.


That is precisely why floating point is poorly suited to most
applications. For applications that require decimal precision (such as
those involving money), there are other representations that are much
better. Scaled integers and BCD, for example.

2.0999999999999996 shows 3.0 to 16 decimal places, nearly all of them
wrong. This result, to most people, is surprising.
Jul 23 '05 #5
Lee
Douglas Crockford said:
That is precisely why floating point is poorly suited to most
applications. For applications that require decimal precision (such as
those involving money), there are other representations that are much
better. Scaled integers and BCD, for example.

2.0999999999999996 shows 3.0 to 16 decimal places, nearly all of them
wrong. This result, to most people, is surprising.

When handling money, all you need to do is round to thousandths
and display to hundredths. It's pretty trivial.

The fact that it's surprising to neophytes isn't much of
a reason to change it.

Jul 23 '05 #6
Lee <RE**************@cox.net> writes:
When handling money, all you need to do is round to thousandths
and display to hundredths. It's pretty trivial.
No code is so trivial that it can't contain an error (except perhaps
the empty statement :).
The fact that it's surprising to neophytes isn't much of
a reason to change it.


It's perhaps too late to change it, although I don't think a change to
BCD-numbers like C#'s "decimal" type would be noticed by most
applications.

However, as a purely theoretical observation, I'd say that for a
script language that is used by so many (until recently)
non-programmers, it would probably be better to use a decimal number
format to avoid exactly these questions. It happens often enough
to be in the FAQ.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #7
JRS: In article <5b**************************@posting.google.com >,
dated Thu, 23 Sep 2004 13:14:59, seen in news:comp.lang.javascript,
janice <go****@janice-eirich.com> posted :

I also tried the code provided in this group's FAQ,


The FAQ says : Operations on integers are exact if the true result and
all intermediates are integers within that range.

If you want results exact to the cent, then work in cents, not in euros.

Integers are exact up to as little over 9,000,000,000,000,000; enough
for most budgets.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #8
JRS: In article <ek**********@hotpop.com>, dated Fri, 24 Sep 2004
19:20:58, seen in news:comp.lang.javascript, Lasse Reichstein Nielsen
<lr*@hotpop.com> posted :

However, as a purely theoretical observation, I'd say that for a
script language that is used by so many (until recently)
non-programmers, it would probably be better to use a decimal number
format to avoid exactly these questions. It happens often enough
to be in the FAQ.


The same applies in Delphi groups of news:borland.*, except that they
have no newsgroup FAQs.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
For news:borland.*, use their server newsgroups.borland.com ; but first read
Guidelines <URL:http://www.borland.com/newsgroups/guide.html> ff. with care.
Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Roger Leigh | last post by:
Hello, I'm writing a fixed-precision floating point class, based on the ideas in the example fixed_pt class in the "Practical C++ Programming" book by Steve Oualline (O' Reilly). This uses a...
4
by: Dave | last post by:
Hi folks, I am trying to develop a routine that will handle sphere-sphere and sphere-triangle collisions and interactions. My aim is to develop a quake style collision engine where a player can...
687
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't...
7
by: Vinoth | last post by:
I'm working in an ARM (ARM9) system which does not have Floating point co-processor or Floating point libraries. But it does support long long int (64 bits). Can you provide some link that would...
15
by: michael.mcgarry | last post by:
Hi, I have a question about floating point precision in C. What is the minimum distinguishable difference between 2 floating point numbers? Does this differ for various computers? Is this...
10
by: Bryan Parkoff | last post by:
The guideline says to use %f in printf() function using the keyword float and double. For example float a = 1.2345; double b = 5.166666667; printf("%.2f\n %f\n", a, b);
23
by: ultimatewarrior | last post by:
Hi all, first of all I beg your pardon if this question has been asked before, but I was unable to find anything in the past posts. I have written a piece of code that was supposed to be quite...
11
by: Peter | last post by:
I have written this small app to explain an issue I'm having with a larger program. In the following code I'm taking 10 ints from the keyboard. In the call to average() these 10 ints are then...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.