integer division rounding

Michael Lunnay mlunnay at ihug.com.au
Tue Jul 17 10:17:38 CEST 2001


Which is 'mathematically' correct then? I am doing some graphics coding and
while my maths background is shakey (re-learning it all as I go along) I
would like to match the work I am learning from.
Should I do all floating point division and then use __builtin__.int() so as
to match C code?

Michael

> > 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