toRational problem

Dylan Thurston dpt@math.harvard.edu
Tue, 17 Apr 2001 12:05:06 -0400


On Tue, Apr 17, 2001 at 08:53:08AM +0200, Lennart Augustsson wrote:
> Ashley Yakeley wrote:
> > At 2001-04-16 18:35, pablocardenal@laplatavive.com wrote:
> > >> Why (toRational 0.9) == (9 % 10) is False?
> > ...
> > This is probably because Hugs attempts a floating-point approximation of
> > the string "0.9" before passing it to a 'from' function (fromSingle,
> > fromDouble) to cast it to 'Fractional a => a'.
> 
> Even though Hugs has this bug, you get the same behaviour even in
> proper Haskell.  The reason is that the type of 0.9 gets defaulted to
> Double (unless you've changed the default default).

Good point.  From ghci:

Prelude> 0.9 :: Rational
9 % 10
Prelude> toRational 0.9
8106479329266893 % 9007199254740992

(But hugs does have this bug: 0.9 :: Rational is not 9 % 10.)

--Dylan Thurston