[Haskell-cafe] Newbie: Haskell Sine Oddities

David Roundy droundy at abridgegame.org
Sun Apr 30 07:19:02 EDT 2006

On Sat, Apr 29, 2006 at 04:51:40PM -0400, Cale Gibbard wrote:
> Floating point computations are always approximate and have some level
> of error associated with them. If you want proper real numbers, things
> like equality testing become impossible in general. If you look
> around, I think there are a couple of libraries in Haskell which let
> you work with arbitrary precision reals though.

That's not really true.  The exact cases of floating point arithmetic can
be important, and it's really annoying when compilers break them.

For small integers, floating point arithmetic *is* exact, for example, and
also for arithmetic (not division) involving integers divided by powers of
two, provided there's no overflow or underflow.  These exact properties
allow the moderately careful programmer to do exact calculations that could
have done using clever integer arithmetic while reusing code that works
with floating point numbers.  It can be handy, for example, when computing
the symmetries of a basis set, since you don't need a separate integer
3-vector class (in C++, for example).  This isn't a big deal, and it's much
less of a deal in Haskell, where you can profitably use typeclasses to make
the integer 3-vectors relatively easy to work with, but on the other hand,
why bother with an integer class that will behave identically to the
floating-point one whenever it's used? (Yes, the answer is the safety of
*knowing* that you made no approximation, but for such a small piece of
easily audited code, that's not likely to be worth the effort.)
David Roundy

More information about the Haskell-Cafe mailing list