[Haskell-cafe] Why aren't Float and Double Bounded?

Henning Thielemann lemming at henning-thielemann.de
Tue Nov 27 16:11:14 EST 2007


On Tue, 27 Nov 2007, Bit Connor wrote:

> > Actually, IEEE numbers are designed in such a way, that if you interpret
> > their bits as integer number, then 'succ' leads you to the next larger
> > representable number. Thus you only have to cast from Float or Double to
> > Int32 or Int64 respectively, call enumFromTo, then cast back to Float or
> > Double, respectively. :-]
>
> How do you cast from Float to Int32(or Word32), and back again? I
> couldn't find an appropriate function. Only thing I can see is going
> through the Storable interface, but that seems cumbersome and I
> imagine slow.

As Don pointed out correctly I'm evil. Storable is a fine way, since it
makes a hack look like a hack. Or you might use castSTUArray.

> Also, a related question: How do you convert from Float -> Double, and
> the reverse? Only thing I could find is (fromRational . toRational)
> which I also imagine to be slow, and I also wonder about accuracy.

Rational keeps full precision. Performance - I don't know, maybe it's
fused away? When I started with Haskell I regularly needed conversion
between Float and Double. I don't know why. I haven't needed it for a long
time now.


More information about the Haskell-Cafe mailing list