[Python-Dev] PEP 238 - The // operator should truncate instead of floor

dany danzat at gmail.com
Wed Aug 29 17:29:36 CEST 2007


Hi all

First, some background.
I've recently tried to port a certain library that manipulates dates 
from C. Some of the core functions contain heavy integer math (very long 
formulae), and after implementing them I tested them to see the give the 
desired results.
I got very odd results, which surprised me since I copied the formula 
letter to letter.

I decided to investigate further, and after trying to evaluate several 
expressions in the python console, I realized it was the integer 
division's fault.

For some reason, the integer division behaves unexpectedly for negative 
integers.

Looking deeper in the python PEPs, I saw that division on integers is 
defined as: idiv(a, b) = floor(fdiv(a, b)).
This non-quotient division leads to some odd results, e.g. Python seems 
to think -3/2+3/2 = -1. This is clearly, and correct me if I'm mistaken 
- wrong.

Now, seeing as Python 3000 is getting closer, and backwards 
compatibility isn't an issue, I believe it would be a good idea to 
change the // operator (which will be used for integer division) to 
behave as quotient, i.e.: a // b = trunc(a / b)

Dany


More information about the Python-Dev mailing list