Modul (%) in python not like in C?
Bryan Olson
fakeaddress at nowhere.org
Sun Sep 9 20:08:28 EDT 2007
Arnau Sanchez wrote:
>> Dotan Cohen wrote:
>>> Second, in Turbo C -111%10=-1 however in python -111%10=9. Is one or
>>> the other in error? Is this a known gotcha? I tried to google the
>>> subject however one cannot google the symbol %. Thanks in advance.
[...]
> In fact, what you get in C depends on the compiler and architecture,
Not according to the C standard:
When integers are divided, the result of the / operator is
the algebraic quotient with any fractional part discarded.(87)
If the quotient a/b is representable, the expression
(a/b)*b + a%b shall equal a.
[...]
87) This is often called ‘‘truncation toward zero’’.
[International Standard ISO/IEC 9899:1999, Section 6.5.5
Multiplicative operators, Paragraph 6 and footnote 87]
> while Python is always consistent and returns positive remainders.
Technically:
The modulo operator always yields a result with the same sign
as its second operand (or zero)
[http://docs.python.org/ref/binary.html]
--
--Bryan
More information about the Python-list
mailing list