Hacker News new | ask | show | jobs
by someweirdperson 1481 days ago
I doubt repeatedly adding floating point numbers is a good idea. With every addition the sum increases further away from the addend, and with their growing relative difference problems grow as well.

Just because the algebra works it doesn't guarantee that the code does, too.

3 comments

Your points are factually correct, but in practice not a big concern, if your floating point value is much more precise than necessary for the numbers used.

E.g. if you use doubles for the range of 32bit integers even adding 1.0 2 billion times to 2 billion still ends up at 4 billion. Even adding 1.0 20 billion times to 20 billion ends up at 40 billion. Now adding 0.1 20 billion times to 2 billion ends up 1908 short on my CPU, i.e. about 19080 absorptions/rounding errors occured. You need some serious differences and amount of operations to actually trigger errors.

You're talking about the best case, but we need to take the reasonable worst cases into account, where x + y nearly cancel etc.
The author of the post is aware of this. They explicitly say that is not the point of the question.
The algebra works for reals, binary floating point is a different beast!