MOO-cows Mailing List Archive


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

>>>>> "Tom" == Tom Ritchford <> writes:

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

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

    Tom>     /t

    Tom> Tom Ritchford,

    Tom> Verge's "Little Idiot" -- Music for the mentally peculiar.
    Tom> 1-800-WEIRDOS

CORRECTION: Knuth vol 1 is "Fundamental Algorithms"; vol 2 is the
            desired reference -- "Seminumerical Algorithms".

-----------  "...  And the men went up and viewed Ai."  [Jos 7:2]  -----------
Robert Jay Brown III  1 847 705-0370
Elijah Laboratories Inc;  759 Independence Drive;  Suite 5;  Palatine IL 60074
-----  M o d e l i n g   t h e   M e t h o d s   o f   t h e   M i n d  ------


Home | Subject Index | Thread Index