
hello, 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) )))::Double -1.1999998092651367 *P2> (fromRational ((toRational 4) - ( toRational (5.2::Double) )))::Double -1.2000000000000002 *P2> (fromRational ((toRational 4) - ( toRational 5.2 ))) -1.2000000000000002 *P2> (fromRational ((toRational 4) - ( toRational (5.2::Float) )))::Float -1.1999998 Are all these results ok? If this is the case, why? Thanks, Ignacio

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 is exact so it gives you the number corresponding to the internal representation. Take a course on numerical analysis. :) -- Lennart Juan Ignacio Garcia Garcia wrote:
hello, 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) )))::Double -1.1999998092651367 *P2> (fromRational ((toRational 4) - ( toRational (5.2::Double) )))::Double -1.2000000000000002 *P2> (fromRational ((toRational 4) - ( toRational 5.2 ))) -1.2000000000000002 *P2> (fromRational ((toRational 4) - ( toRational (5.2::Float) )))::Float -1.1999998 Are all these results ok? If this is the case, why?
Thanks,
Ignacio
_______________________________________________ Glasgow-haskell-users mailing list Glasgow-haskell-users@haskell.org http://www.haskell.org/mailman/listinfo/glasgow-haskell-users

Lennart Augustsson wrote:
The number 5.2 is stored as a slightly different number as a Float, but the toRational function is exact so it gives you the number corresponding to the internal representation. Take a course on numerical analysis. :)
Nope. Take a course entitled: Why all you zombies must remain forever slaves of IEEE-something (477?), and why you should be happy with this. Jerzy Karczmarczuk

If that is a serious question, then the answer is that if you want to take advantage of floating point hardware you are in general limited to those representations that the hardware understands. Also, most floating point representations have a binary field for what is effectively the significant digits of the number you are representing, and thus there are some conversion and roundoff errors built into the representation. It is of course possible to use something more akin to a BCD representation, and if you are willing to live with the performance impact, then that is fine. That should be, however, another type, because in many cases the performance loss is unacceptable and the roundoff errors are insignificant. On Tuesday 12 November 2002 07:10 am, Jerzy Karczmarczuk wrote:
Lennart Augustsson wrote:
The number 5.2 is stored
as a slightly different number as a Float, but the toRational function is exact so it gives you the number corresponding to the internal representation. Take a course on numerical analysis. :)
Nope. Take a course entitled: Why all you zombies must remain forever slaves of IEEE-something (477?), and why you should be happy with this.
Jerzy Karczmarczuk
_______________________________________________ Glasgow-haskell-users mailing list Glasgow-haskell-users@haskell.org http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
-- Seth Kurtzberg M. I. S. Corp seth@cql.com 1-480-661-1849 (GMT-7)

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
participants (6)
-
Jan Kort
-
Jerzy Karczmarczuk
-
Josef Svenningsson
-
Juan Ignacio Garcia Garcia
-
Lennart Augustsson
-
Seth Kurtzberg