[Haskell-cafe] Re: Pure Haskell implementation of Float type?

Ertugrul Soeylemez es at ertes.de
Wed Jan 21 06:29:29 EST 2009


"Tim Chevalier" <catamorphism at gmail.com> wrote:

> Is there a pure Haskell implementation of Floats, i.e., one that
> (unlike GHC.Float) doesn't use foreign calls for things like
> isFloatNegativeZero? I don't care about performance; I'm just looking
> for something that doesn't use foreign calls.

You can easily do it yourself:

  data MyFloat m e
    = MyFloat m e | MyInfinity Bool | MyNaN

A number x is represented in floating point as x = m * b^e, where m is
called the mantissa, e the exponent and b the base.  For performance
reasons, usually b = 2 is chosen and both m and e are integers with
fixed size.  You'll find it useful to have a 'normalize' function, which
'moves the point', such that the mantissa isn't divisible by b, if
possible.


Greets,
Ertugrul.


-- 
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://blog.ertes.de/




More information about the Haskell-Cafe mailing list