views:

39

answers:

1

As programmers, most (if not all of us) know that floating point numbers have a tendency to not be very accurate. I know that this problem can't be avoided entirely, but I am wondering if there are any certain practices, patterns, etc. that can be used to at least reduce floating point errors.

Thanks in advance.

+2  A: 

Use fixed-point mathematics where you can deal with a known limited precision.

As an example, the Rockbox music player firmware uses almost entirely fixed-point media codecs.

If you must be perfectly accurate, use an infinite-length storage type like those provided by the GMP library.

If you're just trying to cut down on your errors, try to work as close to zero as possible, where the IEEE FP numbers are more precise. Reorder your operations to avoid letting your absolute values get too large.

Borealid
I imagine that Rockbox uses fixed-point codecs by preference because some of its targets don't have FPUs so performance would be inadequate. I don't think accuracy is a major concern - if it were, we'd be using Tremor on desktops instead of libvorbis.
Peter
Example: summing a vector, sort by absolute value first. This way, you don't suffer from loss of precision as the size of your sum grows. However, this adds some overhead, so if speed is more important than precision, don't even worry about it.
Ian