Hugs Humor

Andrew J Bromage ajb@spamcop.net
Tue, 8 Jul 2003 12:11:08 +1000


G'day all.

On Mon, Jul 07, 2003 at 12:01:09PM +0200, Jerzy Karczmarczuk wrote:

> I don't understand the remark that the internal arithmetic is
> binary. Sure, it is, so what?

The reason is that you can get the Rational representation even
faster than using continued fractions. :-)

    toFrac :: (RealFloat a) => a -> Rational
    toFrac x
        | m == 0    = 0
        | otherwise = fromInteger m * 2^^(toInteger n)
        where (m,n) = decodeFloat x

    Prelude> toFrac 0.1
    13421773 % 134217728

Cheers,
Andrew Bromage