views:

81

answers:

3

Seeing this post about floating point errors on slashdot, I got curious what other kind of solutions exist to deal with such kind of floating point rounding errors. So what was the floating point bug you learned most from, and what did you learn from it?

EDIT: I am working within a project where we have to deal a lot with floating point calculations, so I hope to get some answers that might help me to avoid some things before they are becoming problems. I will accept the answer that gives me the most new insights beyond "compare everything with an epsilon".

A: 

I don't think you really have to worry about it unless you're dealing with very small numbers and/or very large numbers... if it's a problem, either use some sort of 'decimal' class, or choose a more stable algorithm.

I don't think this has ever actually bitten me in the past, but I was quite excited when it happened to one of my peers and I knew exactly what was wrong :D

Mark
Another problem, numbers which are of completely different orders of magnitude. A small number (let's call it dx) added to a much bigger number (let's call it x) may completely underflow. So you can have situations where x=x+dx can run a million times, with x staying the same.
wisty
-1: There are many cases where you need to be aware of limited precision in floating point operations - taking a sloppy approach such as *"I don't think you really have to worry about it unless you're dealing with very small numbers and/or very large numbers"* is just asking for trouble.
Paul R
@wisty: That's what I meant by "small and large numbers". @Paul: Mm... so, what, every time you use a float you should scrutinize over every single calculation? I'm saying if you're doing running some complicated algo over it where precision is important, then sure, put some thought into it, but for the most part, you can fix it when it becomes a problem. Unless it's mission-critical software.
Mark
@Mark, yes, but take note that multiplying and dividing small and big numbers is usually OK (unless you hit zero or infinity), but adding and subtracting is almost always dangerous.
wisty
+1  A: 

I learned to never ever compare two floats for equality. Instead I always try to phrase my logic so that the comparisons are always a less-than or greater-than condition. Comparing a float with zero is particularly nasty.

John Källén
+2  A: 

Try to avoid adding numbers of dissimilar magnitudes. For example, 10^8+1==10^8 in single-precision arithmetic. You can fix this by moving to double precision, but then 10^8+1.00000001==10^8+1… the fraction gets lost.

If all the numbers going into some linear algebra are biased, remove the bias first. So, given 1000001, 1000003.9, 1000002.5, …, subtract a million before doing anything, and add it back at the end.

To sum a very large sequence of small numbers, sum smaller subsequences first so the numbers at the end don't get unduly rounded.

To multiply a very large sequence of numbers, add their logarithms to avoid overflow or underflow.

Potatoswatter