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

Jon Fairbairn jon.fairbairn at cl.cam.ac.uk
Sat Jul 14 04:39:06 EDT 2007

```Jonathan Cast <jcast at ou.edu> writes:

> On Friday 13 July 2007, Jon Fairbairn wrote:
> > Henning Thielemann <lemming at henning-thielemann.de> writes:
> > > On Thu, 12 Jul 2007, Jon Fairbairn wrote:
> > Surely the first few digits can be computed?
>
> That was my first thought, too.
>
> We can't define
>
> data Real = Real{
>   wholePart :: Integer,
>   fractionPart :: [Int]}
>
> because you can't yield e.g. sin pi as an infinite list of
> digits, but you can define

Well, no, but there are much better representations of reals
than that.

> data Real = Real{
>   exponent :: Int,
>   mantissa :: Int -> [Int]}

That's somewhat better, but all that's required is that the
infinite part should be lazy and give successively more
information about the true value of the number.

> where mantissa rounds the number when it's called.  But unless these can be
> memoized fairly well, I would expect performance to be *quite* surprising to
> new users. . .

Hence my wish to use something with an efficient
representation at the head. Here's another attempt:

> data Real = R {easy:: Double,
>                extra_exponent:: Integer,
>                error_value:: ExactReal
>               }

where extra_exponent is going to be zero for "ordinary size"
numbers and error_value is some good exact real
representation (as I said earlier in the thread, I'm not
sufficiently familiar with the area to choose one. There's
some discussion here
<http://www.dcs.ed.ac.uk/home/mhe/plume/node15.html>), and
the number represented is
easy*base^extra_exponent+error_value. The hope would be
that for low precision arithmetic the "easy" part would be
enough, though it's possible that cases where some
inspection of the error_value would be necessary would
turn out to be common.

--
Jón Fairbairn                                 Jon.Fairbairn at cl.cam.ac.uk

```