Floats and Doubles
Josef Svenningsson
josefs@cs.chalmers.se
Wed, 13 Nov 2002 14:33:24 +0100 (MET)
On Wed, 13 Nov 2002, Jan Kort wrote:
>
> Juan Ignacio Garcia Garcia wrote:
> > *P2> (fromRational ((toRational 4) - ( toRational 5.2 )))
> > -1.2000000000000002
>
> I can't explain this one, how would fromRational
> know that it has to create a Double ?
>
It's the defaulting mechanism that kicks in. The default default is
(Integer,Double). Since Integer is not an instance of Fractional, Double
is chosen.
But this brings up a strange thing in GHCi. Suppose I load the following
module into GHCi:
\begin{code}
module Foo where
kalle = (fromRational ((toRational 4) - ( toRational 5.2 )))
default (Rational)
\end{code}
What happens is the following:
Prelude> :l Foo.hs
Compiling Foo ( Foo.hs, interpreted )
Ok, modules loaded: Foo.
*Foo> kalle
(-6) % 5
*Foo> (fromRational ((toRational 4) - ( toRational 5.2 )))
-1.2000000000000002
It seems like GHCi doesn't care about the default directive on the promt.
I tried to look in the documentation but I found nothing about defaulting.
I believe it is reasonable to expect that the defaulting of the current
module also affects the promts (I realise however that this can lead to
problems when several modules are in scope..). Other things one might want
to have is to be able to see what the current defaulting is and perhaps
set it manually from the prompt.
At the very least, please document how GHCi handles defaulting. I know
it's not the most frequently used feature but it's there and needs to be
taken care of.
All the best,
/Josef