Changing the Division Operator -- PEP 238, rev 1.12

Terry Reedy tjreedy at home.com
Fri Jul 27 23:27:41 EDT 2001


>     The current division (/) operator has an ambiguous meaning for
>     numerical arguments: it returns the floor of the mathematical
>     result of division if the arguments are ints or longs, but it
>     returns a reasonable approximation of the division result if the
>     arguments are floats or complex.  This makes expressions
expecting
>     float or complex results error-prone when integers are not
>     expected but possible as inputs.

I previously suggested at this point:
> > and vice versa for integer expressions getting float input
which I should revise to include a key stipulation:
and vice versa for integer expressions getting float integer input

and Guido answered

> That's not the same argument -- expressions expecting ints are
> generally broken when they receive floats; expression expecting
floats
> are *only* broken when they receive ints IF THEY USE DIVISION.  The
> PEP cannot do anything about floats passed in where ints are
expected
> -- such usage is broken.

I disagree; due to my leaving a key word out, you have missed one of
the virtues of the proposal that convinced me to support it.  An
expression that is valid for int integers (ie, 387) is just as valid
for float integers (ie, 387.0), which you claim to be +/- the same
thing (modulo type).  With //, such expressions will give the same
numerical result with either input,  whereas they now, with /,
generally give different numerical results.

Example: 20//6 = 3,   20.0//6.0 = 3.0 (hooray),   20.0/6.0 = 3.5 (ugh,
if I want 3==3.0)

A corollary effect: suppose I publish a discrete algorithm using
Python and someone runs it on a typical hand calculator that converts
int literals to their float equivalent and I explain that 'i//j' means
to enter 'i / j = floor' (with the flooring done by substraction if
necessary) and the person does just that.  The answer will be correct!
(instead of wrong).
------

Me:
> > The only way I can think of to 'globally' turn on new division on
my
> > own Windows computer is to rebind .py and .pyc to python.bat
> > containing '...python.exe -Dnew'.
Guido:
> Do I need to add that to the PEP?

and in response to someone else

> What exactly would you like to see addressed in the PEP?  Should I
> spell out that you have to do a query replace of / to //,approving
the
> replace if the arguments are integers?  That seems pretty trivial.

Right now you are writing to persuade and satisfy people who are
mostly experienced programmers.  Later it will remain to be read by a
wider variety of people, including beginners.  Have to? No.
Advisable? Maybe so.  Even with the rewrite, the proposal may still
have enough shock value to new readers to inhibit some people's normal
thought process.

Terry J. Reedy






More information about the Python-list mailing list