[Python-ideas] division oddness

Terry Reedy tjreedy at udel.edu
Fri May 7 00:57:52 EDT 2010


> On Thu, May 6, 2010 at 5:43 PM, Mathias Panzenböck

>> Shouldn't by mathematical definition -x // y be the same as -(x// y)?

Tradeoffs, tradeoffs.
Most everyone agrees on this rule for the relation between // and %:

x == y*(x//y) + x%y

If // is defined as above, then, for instance, x%2 has 3 possible values 
(-1, 0, 1) rather than just 2. Not so good. Since y is usually positive 
in practical applications (what does dividing by a negative number 
mean?), having the sign of x%y governed by the sign of y rather than the 
sign of x seems advantageous.

Judging from the chart at
http://en.wikipedia.org/wiki/Modulo_operation
languages are pretty evenly split on which definitions to use.

Terry Jan Reedy






More information about the Python-list mailing list