
21 Jul
2009
21 Jul
'09
6:58 p.m.
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