[Python-ideas] division oddness

MRAB python at mrabarnett.plus.com
Fri May 7 03:09:22 CEST 2010


Mathias Panzenböck wrote:
> Shouldn't by by mathematical definition -x // y be the same as -(x // 
> y)? But when assign x=2, y=3 you get:
> 
>  >>> -2 // 3
> -1
>  >>> -(2 // 3)
> 0
> 
> And also:
>  >>> 2 // -3
> -1
>  >>> -2 // -3
> 0
> 
> And even more:
>  >>> int(-2 / 3)
> 0
>  >>> int(2 / -3)
> 0
> 
> I think this rather odd. Is there any deeper reason to this behaviour? I 
> guess changing this will break a lot of code, but why does it behave 
> like this?
> 
It's so that:

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

is always True (assuming x != 0).



More information about the Python-ideas mailing list