Re: Arbitrary precision reals?

2003-03-26 Thread Alastair Reid

Tom Pledger [EMAIL PROTECTED] 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.

Fergus Henderson [EMAIL PROTECTED] writes:
 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?

That's pretty neat.

But it seems that this will suffer from the same loss of sharing problem
as we get with overloaded CAFs.  That is, if I calculate:

   let
 a :: ARBITRARY_PRECISION_REAL
 a = 1
 b = a+a
 c = b+b
 d = c+c
 e = d+d
   in 
   e

then, instead of doing just 4 additions, I will do 15.

So I think we need to add in a memo table.


More seriously though, can we make an Eq instance?  Presumably the GNU
MP equality test doesn't test for equality but equality over the first
'n' bits for some user-specified 'n'?


--
Alastair Reid [EMAIL PROTECTED]  
Reid Consulting (UK) Limited  http://www.reid-consulting-uk.ltd.uk/alastair/

___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell


Re: Arbitrary precision reals?

2003-03-26 Thread Nils Anders Danielsson
On Tue, 25 Mar 2003, Tom Pledger wrote:

 I don't know whether arbitrary precision reals have been done in
 Haskell, but here's one of the issues...

There is a Haskell implementation of exact real arithmetic using Linear
Fractional Transformations, see

  http://www.doc.ic.ac.uk/~ae/exact-computation/

for implementations and a bunch of papers. (The web server does not seem
to respond right now, but the pages are cached by Google.)

/NAD
___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell


Re: Arbitrary precision reals?

2003-03-25 Thread Fergus Henderson
On 25-Mar-2003, Tom Pledger [EMAIL PROTECTED] 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 [EMAIL PROTECTED]  |  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.
___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell


Arbitrary precision reals?

2003-03-24 Thread Tom Pledger
Niall Dalton writes:
 | Hi,
 | 
 | Its been a while since I've been using Haskell seriously, so I might simply
 | have overlooked the answer..
 | 
 | Is there an arbitrary precision real number library available for Haskell?
 | IIRC, atleast GHC uses the GMP library, but only for integers?

Hi.

There's the Ratio library, which can be used to make arbitrary
precision rationals from arbitrary precision integers.  But it doesn't
provide arbitrary *finite* precision implementations of imprecise
functions such as exp, so I suspect it's not quite what you're after.

I don't know whether arbitrary precision reals have been done in
Haskell, but here's one of the issues...

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.

For example, Haskell's

sqrt :: Floating a = a - a

may look like it matches GNU mp's

void mpf_sqrt (mpf_t rop, mpf_t op)

but the result pointer rop also provides the precision argument, which
was prepared earlier by some impure stateful action such as

void mpf_set_default_prec (unsigned long int prec)

I can see three ways around this, but they're all rather fiddly.

  - Write a nonstandard version of Haskell's Floating class,
introducing an explicit how-much-precision-do-you-want parameter.

  - Declare a separate Floating instance for each fixed precision
level you want to use, e.g. instance Floating GNU_mpf_t_with_300bits.

  - Declare a data type which pairs a GNU mpf_t with a number of bits,
and declare a Floating instance for this data type, inferring the
required precision of the result from the precision of the
arguments.  This gets into trouble when there are no arguments: pi
would presumably have to have fixed precision.  Although you could
always ignore it in favour of something like

(4 `withBits` 300) * atan (1 `withBits` 300)

Regards,
Tom



http://haskell.org/onlinereport/ratio.html
http://www.swox.com/gmp/manual/Float-Arithmetic.html#Float%20Arithmetic
http://www.swox.com/gmp/manual/Initializing-Floats.html#Initializing%20Floats




___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell