[Haskell-beginners] High precision doubles

Andrew Hunter ahunter at cs.hmc.edu
Wed Jun 24 21:18:19 EDT 2009


On Wed, Jun 24, 2009 at 08:46:04PM -0300, Aaron MacDonald wrote:
> Is there anything in Haskell that can represent doubles with higher 
>                                                 ^^^^^^^

No, I'm pretty sure "double precision" will be double precision
everywhere (systems not supporting IEEE-754 notwithstanding.)  Want to
represent floating point numbers more precisely?  That's feasible.

> precision than the regular kind? I'm working with formulas that generate 
> sets of values, and values that I know should be zero are ending up as 
> things like -7.105427357601002e-15 or -1.7763568394002505e-14, which is 
> causing all kinds of butterfly effects.
>
> I've heard of Rational, but I need to also work with irrational numbers 
> (namely, pi and anything that comes from cos and sin). What other options 
> do I have?
>

Some systems have support for "long doubles", typically an 80-bit type
on x86; I don't know if Haskell provides any access to this.  I doubt
it.  If you want a more sledgehammerish approach, packages like HMPFR:

http://hackage.haskell.org/package/hmpfr

can provide arbitrary precision calculations.  They're slow, though.  

More to the point, however: you don't want more precision.  Welcome to
the world of numerical algorithms; floating point arithmetic is
inherently inexact.  Get used to it.  For example, I'll bet your
errors are caused by testing for equality against zero, and if I had
to guess, you're probably trying to terminate a procedure when some
value hits zero?  It's not going to; you need to introduce the concept
of tolerances, and accept if |val| < tol.  This is a simplistic
solution and not really right in most cases, but might help.  If you
want more advice about how to handle floating-point inaccuracy, could
you provide a program and what's going wrong?

For way more information, consult "What Every Computer Scientist
Should Know About Floating-Point Arithmetic."

http://docs.sun.com/source/806-3568/ncg_goldberg.html

HTH,
AHH


More information about the Beginners mailing list