Floats and Doubles
Tue, 12 Nov 2002 14:35:44 +0100
Yes, they all seem to be right.
You get these funny effects because numbers like 5.2 do not have an
exact representation with floating point numbers in base to (like Float
and Double most likely have on your machine). The number 5.2 is stored
as a slightly different number as a Float, but the toRational function
so it gives you the number corresponding to the internal representation.
Take a course on numerical analysis. :)
Juan Ignacio Garcia Garcia wrote:
> I have been using some of the functions of the classes Real and
> Fractional and I have observed that with the funcion "toRational" we
> can obtain the fraction that represents a given number. For instance:
> *P2> toRational (5.2::Float)
> 5452595 % 1048576
> Why we obtain this numbers instead of "52 % 10" or "26 % 5"?
> I have also obtained the following results with the functions
> "fromRational" and "toRational":
> *P2> (fromRational ((toRational 4) - ( toRational (5.2::Float)
> *P2> (fromRational ((toRational 4) - ( toRational (5.2::Double)
> *P2> (fromRational ((toRational 4) - ( toRational 5.2 )))
> *P2> (fromRational ((toRational 4) - ( toRational (5.2::Float) )))::Float
> Are all these results ok? If this is the case, why?
> Glasgow-haskell-users mailing list