This is an old copy of the Python FAQ. The information here may be outdated.

Why does -22 / 10 return -3?

It’s primarily driven by the desire that i%j have the same sign as j. If you want that, and also want:

i == (i/j)*j + (i%j)

then integer division has to return the floor. C also requires that identity to hold, and then compilers that truncate i/j need to make i%j have the same sign as i.

There are few real use cases for i%j when j is negative. When j is positive, there are many, and in virtually all of them it’s more useful for i%j to be >= 0. If the clock says 10 now, what did it say 200 hours ago? -190 % 12 == 2 is useful; -190 % 12 == -10 is a bug waiting to bite.

CATEGORY: programming

 

A Django site. rendered by a django application. hosted by webfaction.