Arbitrary precision reals?
Fergus Henderson
fjh@cs.mu.OZ.AU
Wed, 26 Mar 2003 19:00:44 +1100
On 25-Mar-2003, Tom Pledger <Tom.Pledger@peace.com> wrote:
>
> The floating point part of the GNU mp library looks difficult to fit
> into Haskell's numeric classes, because the type signatures in class
> Floating don't include a how-much-precision-do-you-want parameter.
How about using a function type which takes a precision and gives
you back an answer to that many digits, and making this function
type an instance of the numeric type classes?
E.g.
data MPF_T -- abstract, corresponds to GNU mp's mpf_t
type PRECISION = Int
type ARBITRARY_PRECISION_REAL = (PRECISION -> MPF_T)
instance Floating ARBITRARY_PRECISION_REAL where
sqrt x = (\prec -> mpf_sqrt_ui (x (prec + sqrt_lossage)))
where sqrt_lossage = 1 -- amount of precision lost by sqrt
...
Here mpf_sqrt_ui would be an interface to GMP's mpf_sqrt_ui() function,
which takes as input an mpf_t and a precision and produces an mpf_t as output.
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
The University of Melbourne | of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.