in reply to adding numbers and floating point errors

Unless your interval is a fraction of a power of two or can be represented by a small number of them added together, your number can't be represented exactly in binary. Think about what you are asking your poor binary computer to do. .05 is 1/20. The first number that isn't bigger than your number is 1/32. Subtract that from 1/20. Now look for 1/2**n which is smaller than what's left. Continue until it comes out even or you run out of precision.

Decimal fractions are rarely representable exactly in computing memory.

To prevent errors from building up, round more often.

As for

3.65 + 0.05 = 3.69999999999999
I wouldn't say the error is in the first place. It's actually in the last place you showed. Any prior rounding would work. If the difference was significant in the first digit, you could round in the second place without getting the correct answer.

Decimals tie people in knots even when computers aren't involved. I remember a heated discussion in a math class I took, where the teacher was making no headway in convincing some student that .3 repeating is 1/3.

Phil

  • Comment on Re: adding numbers and floating point errors

Replies are listed 'Best First'.
Re^2: adding numbers and floating point errors
by smeenz (Sexton) on Nov 22, 2006 at 22:00 UTC
    Thanks to everyone.. I see what the problem is now... I had thought that these sorts of numbers would be represented as whole numbers and then the decimal place added back in for the final result, so 0.0005 + 0.0005 would be the same as adding 5+5 (or 00000101 and 00000101), and then shifting the decimal point appropriately at the output stage, but now that you've explained that the computer is trying to approximate the actual fraction using powers of 2, it makes a lot more sense as to what is happening.