Negative division bug?

casevh at comcast.net casevh at comcast.net
Thu Aug 3 14:03:27 EDT 2006


>    I was just screwing around.
> and found:
> >>> -1/100
> -1
>   Shouldn't it be zero?
> 1/100  returns 0
> but -1/ANY_POSITIVE_INTEGER_NUMBER
>   returns -1
>
> >>> -10/3
> -4
>
>    It behaves correct for positive numbers, but for negative
> integers it seems to subtract one from the expected result.
>
That is the intended behavior. Look at the output of divmod().

>>> divmod(-1,100)
(-1,99)

Let (q,r) = divmod(d, v) with v > 0, the behavior is chosen so that 0<=
r < v.

casevh




More information about the Python-list mailing list