posted 7 years ago

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.

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions

posted 7 years ago

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/

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/

posted 7 years ago

Incredibly thorough.... but too many numbers for before coffee.... thanks! I will wake up a bit and read this again.

When you do things right, people won't be sure you've done anything at all.

Campbell Ritchie

Marshal

Posts: 56570

172

Campbell Ritchie

Marshal

Posts: 56570

172

John Godfrey

Greenhorn

Posts: 18

1

posted 7 years ago

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?
posted 7 years ago

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

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

Campbell Ritchie

Marshal

Posts: 56570

172

posted 7 years ago

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.

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.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

posted 7 years ago

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

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

Campbell Ritchie

Marshal

Posts: 56570

172

posted 7 years ago

Having looked in Wikipedia for Accuracy_and_precision, I think I may be mistaken about accurate and precise.

Campbell Ritchie

Marshal

Posts: 56570

172

posted 7 years ago

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

If the arrow always hits

To win the prize, the archer needs to be both, like Robin Hood.

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.

When you do things right, people won't be sure you've done anything at all.

It is sorta covered in the JavaRanch Style Guide. |