Double -> CDouble, realToFrac doesn't work

Sven Panne Sven.Panne at
Sun Aug 8 12:02:19 EDT 2004

Hal Daume III wrote:
> [...]
> Prelude Foreign.C> (0 :: CDouble) / 0
> NaN
> Prelude Foreign.C> (0 :: Double) / 0
> NaN
> Prelude Foreign.C> realToFrac ((0 :: Double) / 0) :: CDouble
> -Infinity
> yikes!  the NaN got turned into a -Infinity!!!
> aside from manually checking for 'strange' Double/CDouble values and 
> wrapping realToFrac, is there a better way?  also, does this count as a 
> bug?

Hmmm, this is a little bit of a dark corner in the H98 report, but there are
probably other people on this list who know better than me. :-] The problem
is that 'realToFrac' is defined as 'fromRational . toRational', so the first
question is: How is 'toRational' supposed to handle NaN? One thing coming to
my mind is '0 :% 0', but this is normally a value which can't be constructed.
So the next question is: Would this be allowed? +Infinity and -Infinity could
be represented similarly then ('1 :% 0' and '(-1) :% 0'), and 'fromRational'
could handle these values specially. But I can't believe that this has been
discussed for the first time. SPJ? Malcolm?

When you compile the stuff above with optimizations on, you get what you've
expected, thanks to RULES which shortcut the route via Rational completely.


More information about the Glasgow-haskell-users mailing list