integer division rounding
Marcin 'Qrczak' Kowalczyk
qrczak at knm.org.pl
Wed Jul 18 13:45:17 EDT 2001
18 Jul 2001 16:46:49 +0200, Ralf Muschall <ralf.muschall at alphasat.de> pisze:
> What does this mean? I always understood C's way of dividing
> negative ints a "do what is the simplest thing for the machine"
It used to be. This is what C++ says:
4 The binary / operator yields the quotient, and the binary % operator
yields the remainder from the division of the first expression by the
second. If the second operand of / or % is zero the behavior is unde-
fined; otherwise (a/b)*b + a%b is equal to a. If both operands are
nonnegative then the remainder is nonnegative; if not, the sign of the
remainder is implementation-defined70).
_________________________
70) According to work underway toward the revision of ISO C, the pre-
ferred algorithm for integer division follows the rules defined in the
ISO Fortran standard, ISO/IEC 1539:1991, in which the quotient is al-
ways rounded toward zero.
and C indeed doesn't provide the choice now:
[#6] When integers are divided, the result of the / operator
is the algebraic quotient with any fractional part
discarded.78) If the quotient a/b is representable, the
expression (a/b)*b + a%b shall equal a.
____________________
78)This is often called ``truncation toward zero''.
A consequence of the C definition is that the compiler can't implement
i/2 as i>>1 unless it can be sure that i is nonnegative.
--
__("< Marcin Kowalczyk * qrczak at knm.org.pl http://qrczak.ids.net.pl/
\__/
^^ SYGNATURA ZASTĘPCZA
QRCZAK
More information about the Python-list
mailing list