# [Haskell-cafe] Re: Defaulting to Rational [was: Number overflow]

Jon Fairbairn jon.fairbairn at cl.cam.ac.uk
Thu Jul 12 08:16:00 EDT 2007

```Henning Thielemann <lemming at henning-thielemann.de> writes:

> On Thu, 12 Jul 2007, Jon Fairbairn wrote:
>
> > Now, a proper exact real type is doubtless very inefficient,
> > but wouldn't it be possible to define something that had a
> > fairly efficient head, and a lazy tail? So you'd have, say
> >
> > > data Real = R {big::(Ratio !Int !Int), small:: More_Precision}
>
> Interesting approach.

But flawed as I put it: the big part can't express big
numbers! The big part needs to be either Rational (and the
precision of that part limited during arithmetic) or
BigFloat where

> Data BigFloat = BF {mantissa:: Int, exponent:: Integer}

(ie limited precision, but unbounded magnitude). If we were
to use BigFloat the base would need to be a power of ten to
get the desired results for things like Don's example)

> Somehow similar to making Integer a sum of Int and
> BigInt. Indeed, I have used transcendent arithmetic on
> Doubles to speedup computations for real numbers. However
> real numbers cannot be checked for equality. This can be
> also considered an advantage, because using (==) for
> floating point numbers is most oftenly a bug.

Agreed. That should be part of the change to the numeric
hierarchy.

> I think that this hybrid type is nice and could be used by
> default. But it should not replace native floating point
> types, since they have guaranteed speed in favor of not
> guaranteed precision. And we need a correct
> implementation.

Absolutely!

--
Jón Fairbairn                                 Jon.Fairbairn at cl.cam.ac.uk
http://www.chaos.org.uk/~jf/Stuff-I-dont-want.html  (updated 2007-05-07)

```