[Haskell-beginners] Double's

John Dorsey haskell at colquitt.org
Tue Jul 21 18:58:29 EDT 2009


Thomas,

The strangeness you're experiencing is normal behavior for floating-point
(FP) math, which by definition doesn't obey the usual algebraic laws you'd
like it to.

FP is inexact.  FP operations silently round at every turn.  Comparison of
two FP values for equality is usually a programming error, with a few
notable exceptions.  Usually what you want is either to compare the
difference between two FP values to see if they're within some appropriate
tolerance, or to avoid FP altogether.  In the latter case, rational numbers
may be what you need; Haskell has native types that support exact rational
numbers directly, and you can convert to floating point if needed for
approximate trig functions or whatever.

Regards,
John



More information about the Beginners mailing list