MOO-cows Mailing List Archive


Floating point is like that... (was Re: Floating point error)

    If programmers are assuming that they are going to get
"exact" numbers with their floating point calculations,
I fear that they will be sadly disappointed.  It's a really
Bad Idea to test floating point numbers for equality -- you should
always build in a little shrinkage into the tests, or else you
will get a nasty surprise one day.  (ie, write abs( a-b ) < epsilon,
rather than a == b, where epsilon is a small number.  If a and b
can vary greatly in magnitude, try abs( a-b )/b < epsilon ((when
b isn't zero of course!)))

    Donald Knuth discusses this at great length in the Art of Computer
Programming, vol 1.


Tom Ritchford,

Verge's "Little Idiot" -- Music for the mentally peculiar.

Follow-Ups: References:

Home | Subject Index | Thread Index