[Python-Dev] Adding a Rational Type to Python

Michael Hudson mwh21@cam.ac.uk
12 Mar 2001 08:24:03 +0000


Guido van Rossum <guido@digicool.com> writes:

> Here's the third installment -- my response to Moshe's rational
> numbers PEP.

I'm replying to Guido mainly through laziness.

> > PEP: XXX
> > Title: Adding a Rational Type to Python
> > Version: $Revision$
> > Author: pep@zadka.site.co.il (Moshe Zadka)
> > Status: Draft
> > Python-Version: 2.2
> > Type: Standards Track
> > Created: 11-Mar-2001
> > Post-History:
> > 
> > 
> > Abstract
> > 
> >     Python has no number type whose semantics are that of a
> >     unboundedly precise rational number.
> 
> But one could easily be added to the standard library, and several
> implementations exist, including one in the standard distribution:
> Demo/classes/Rat.py.
> 
> >     This proposal explains the
> >     semantics of such a type, and suggests builtin functions and
> >     literals to support such a type. In addition, if division of
> >     integers would return a non-integer, it could also return a
> >     rational type.
> 
> It's kind of sneaky not to mention in the abstract that this should be
> the default representation for numbers containing a decimal point,
> replacing most use of floats!

If "/" on integers returns a rational (as I presume it will if
rationals get in as it's the only sane return type), then can we
please have the default way of writing rationals as "p/q"?  OK, so it
might be inefficient (a la complex numbers), but it should be trivial
to optimize if required.

Having ddd.ddd be a rational bothers me.  *No* langauge does that at
present, do they?  Also, writing rational numbers as decimal floats
strikes me s a bit loopy.  Is 

  0.33333333

1/3 or 3333333/10000000?

Certainly, if it's to go in, I'd like to see

> > Literals
> > 
> >     Literals conforming to the RE '\d*.\d*' will be rational numbers.

in the PEP as justification.

Cheers,
M.

-- 
  MAN:  How can I tell that the past isn't a fiction designed to
        account for the discrepancy between my immediate physical
        sensations and my state of mind?
                   -- The Hitch-Hikers Guide to the Galaxy, Episode 12