PEP239 (Rational Numbers) Reference Implementation and new issues

Charles Krug charles at
Thu Oct 3 16:09:29 CEST 2002

On Thu, 3 Oct 2002 08:03:19 -0400, John Roth <johnroth at> wrote:
><python-pep at> wrote in message
> news:mailman.1033596730.13955.python-list at
>> I just uploaded a reference implementation of how rationals might look
>> in Python as patch 617779 [1].  I do have some new issues for
>> discussion that I'd like to get some comments on before I change the
>> PEP.
>> 1) Should future division return rationals rather than floats.  I had
>> sort of assumed this would happen, but I just had a discussion with
>> Kirby Urner and couldn't convince him it was a good idea, so I guess
>> it isn't so clear.
> And I don't think you're ever going to get a concensus, for the
> simple reason that integer division is an oxymoron. The integers
> are not closed under division. The rationals are, and so are the floats
> (well, close enough.)
> In other words, there is no right answer. Treating an integer
> as a degenerate rational makes it look like integer 'division' should
> return a rational, but that's simply semantics. You can just as easily
> treat an integer as a degenerate float.

Not exactly.  Integer division is defined as a theorum, which is called the
Division Algorithm:

Given any two integers a, b with b > 0, there exist unique integers q, r
such that a = bq + r and 0 <= r < b.

It's easy enough to manipulate a and b into the correct form.  It's also
fairly easy to prove.

Rationals are defined in terms of integers like this.  I wish I were pulling
your leg:

Consider a relation between Z and itself such that for i, j, k, n in Z, with 
j != 0 and n != 0:

(i, j) ~ (k, n) if i*n = j*k.

The set of all such ordered pairs is called the Rational Numbers, designated

Luckily, it's a definition, and doesn't require proof beyond showing that the
squiggle in this case defines an equivalence relation (it does).

>From the definition, it's easy to create rationals that are exactly equal to
integer values.  The definition of real numbers is even uglier, but it's
sufficiently broad to include both the rationals and the integers.

If you're writing a rational class, I think it's perfectly reasonable in all
cases to return a rational number, as well as to offer conversion operators
into both float and q, r representations.

I don't think it's a matter of semantics as it is a question of modeling
choice and problem decomposition.

More information about the Python-list mailing list