[Haskell-beginners] Double's
Thomas Friedrich
info at suud.de
Tue Jul 21 19:10:40 EDT 2009
John Dorsey wrote:
> 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
>
Thanks John! Changing Double to Rational in the type declaration solved
the problem. Also thanks for pointing out that one should never test
flouting point numbers for equality and expect the result to be
trustworthy. I really should have thought about this, but I didn't.
Cheers,
Thomas
More information about the Beginners
mailing list