integer division rounding

Terry Reedy tjreedy at home.com
Tue Jul 17 03:14:19 EDT 2001


> Why does an integer division resulting in a negative round down ,e.g -0.5
> => -1, while C and  __builtin__.int() rounds up ,e.g -0.5 => 0. Is this a
> purposful error(?), or an oversight?

Purposeful.  This way, 0 <= m%n < n and (m/n)*n + m%n = m.
The other choice (round toward 0) maintains the invarient -(m/n) = (-m/n).
Much as we would like, we cannot make all three invarients true at once for
negative quotients.

Terry J. Reedy






More information about the Python-list mailing list