[Edu-sig] RE: Rationals

Guido van Rossum guido@python.org
Fri, 11 Oct 2002 12:38:30 -0400


We've gone through all this recently on python-dev, and also to some
extent here I believe.  I hope we won't have to go though all of it
again here; the python-dev archives are public.

The name choices are between rat() and rational().  Tim noted that
every rational type that was every used for real was called rat, so
rat it is.

> >     int   / int   --> float
> 
> Why not this?
> 
>       int   / int   --> rat
> 
> After all, the definition of "rational" is "int divided by int".

This is even what PEP 238 suggests.  But I'm not comfortable right now
with it for two reasons:

- People are expecting this to return a float now.

- I don't know how well rationals will work in "common" computations.
  I was bitten badly by this in ABC (read the python-dev archives) and
  while we can avoid the mistakes made there, I'm still not
  comfortable.

I think the following would be fair:

  If fixed-point decimal constants like 2.5 are rationals, 5/2 should
  also be a rational.  If 2.5 is a float, 5/2 should return a float --
  meaning that the only way to get a rational is to use an explicit
  rat() constructor or a literal with an 'r' suffix.  (I think 2.5r
  would make a nice way to input a rational too, in addition to 5r/2
  and 5/2r.)

For extra credit:

- What should "print 1/3" print?  I really don't want this to print
  the string "1/3" or some other way showing the components of the
  rational number.

--Guido van Rossum (home page: http://www.python.org/~guido/)