programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Double arithmetic

Janeice DelVecchio
Bartender
Posts: 1812
12
Okay so if you add double value 0.1 to 0 a thousand times, you don't get 100. But a double with value (0.1*1000) equals 100.

Why?

David Newton
Author
Rancher
Posts: 12617
Thus is the nature of floating point math.

Henry Wong
author
Sheriff
Posts: 23295
125

One easy way to think about it. Floating point operations are not 100% accurate. Each operations has some inaccuracies. And 1000 operations will introduce 1000 inaccuracies.

Henry

Janeice DelVecchio
Bartender
Posts: 1812
12
Thanks so much!

Rob Spoor
Sheriff
Posts: 21135
87
Floating point numbers are stored in binary as well. For instance 0.1 binary is 0.5 decimal, 0.01 binary is 0.25 decimal, etc. This means that 0.1 decimal is 0.000110011001100110011... binary. Just like 1/3 is 0.3333333... decimal this will never end. At some time the rest is just chopped off, and this causes the inaccuracy.

Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Floating-point numbers in Java (as well in many other programming languages) are represented in IEEE-754 format, as binary fractions.

Just like in decimal you can't represent some fractional numbers with a finite number of digits, for example 1/3 = 0.33333..., you can't represent some fractional numbers in binary with a finite number of digits.

A simple example of a binary fraction: 10110.011 = 2^4 + 2^2 + 2^1 + 2^-2 + 2^-3 = 22.375

The number 1/10 can be represented in decimal with a single digit after the decimal point: 0.1

But in binary, you can't represent it with a finite number of digits; 1/10 in binary is approximately: 0.00011001100110011... (the digits repeat infinitely).

Because you can't represent 1/10 with exact precision, doing maths with this number in a float or double will lead to rounding errors. And as Henry says, when you do 1000 calculations, you're piling 1000 rounding errors on top of each other.

Handy tool: http://www.digitconvert.com/

Janeice DelVecchio
Bartender
Posts: 1812
12
Incredibly thorough.... but too many numbers for before coffee.... thanks! I will wake up a bit and read this again.

Campbell Ritchie
Marshal
Posts: 56570
172
. . . and when you have had your coffee, you will be able to read this

Campbell Ritchie
Marshal
Posts: 56570
172
If you want a precise answer, you ought to use the BigDecimal class.

John Godfrey
Greenhorn
Posts: 18
1
Janeice posted the original question after I raised it while being nitpicked for Java-7 Sum in the Cattle Drive - thanks Janeice and all who answered. But can anyone explain why multiplying a double with a value of 0.1 by 1000 does return 100, but adding 0.1 to itself 1000 times doesn't?

Janeice DelVecchio
Bartender
Posts: 1812
12
See henry's answer above. This is most likely due to the volume of calculations.

David Newton
Author
Rancher
Posts: 12617
Precisely (if you'll excuse the pun): cumulative errors.

Henry Wong
author
Sheriff
Posts: 23295
125
Janeice DelVecchio wrote:See henry's answer above. This is most likely due to the volume of calculations.

Its based on the operation and the number of operations. Certain operations have more inaccuracy than others -- and doing multiple operations will compound the inaccuracies.

One of my favorite example, of large inaccuracy with a few operations, is...

Obviously, the answer should be zero...

Henry

John Godfrey
Greenhorn
Posts: 18
1
Ahhh, got it. Thanks. 0.1 * 1000 is only one operation . . . seems obvious now. (Janeice can tell you I've been saying that quite a lot recently!)

Campbell Ritchie
Marshal
Posts: 56570
172
By inaccuracy, Henry, surely you mean imprecision. The output from your formula, which is of course 3456789 ( ), is accurate but imprecise.

fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Campbell Ritchie wrote:By inaccuracy, Henry, surely you mean imprecision. The output from your formula, which is of course 3456789 ( ), is accurate but imprecise.

I think 3456789 is VERY precise. If it came back with "oh...i don't know...around 3,000,000, give or take a couple hundred thousand..." that would be imprecise.

David Newton
Author
Rancher
Posts: 12617
It's precise, not accurate. I always use the thermometer analogy--temp is 72.0, reading is 72.0, accurate and precise. temp is 103.0, reading is 90.0. precise but inaccurate.

Precision is "constantly reproducible" and number of decimal points. Accuracy is "reflection of reality".

http://en.wikipedia.org/wiki/Accuracy_and_precision

Campbell Ritchie
Marshal
Posts: 56570
172
It's not reading 93°, but 72.1° or 71.9° when the temperature is 72.0°. Accurate in that your 99.9999.... almost reflects the "real world" value of 100, but slightly imprecise.

Campbell Ritchie
Marshal
Posts: 56570
172
Having looked in Wikipedia for Accuracy_and_precision, I think I may be mistaken about accurate and precise.

Hunter McMillen
Ranch Hand
Posts: 492
But how accurate is that article?

Hunter

David Newton
Author
Rancher
Posts: 12617
Precisely.

Campbell Ritchie
Marshal
Posts: 56570
172
It's both accurately precise and precisely accurate.

Janeice DelVecchio
Bartender
Posts: 1812
12
Did you boys hijack my thread?

The whole precise and accurate debacle brings pictures in my mind of an archer. This is how I remembered in Chemistry class -- back in the day.

If the arrow always hits the same Point, it's Precise.
If the arrow always hits Around the bulls eye, it's Accurate.
To win the prize, the archer needs to be both, like Robin Hood.

 It is sorta covered in the JavaRanch Style Guide.