# Re: Modul (%) in python not like in C?

Discussion in 'Python' started by Arnau Sanchez, Sep 9, 2007.

1. ### Arnau SanchezGuest

J. Cliff Dyer escribió:

> Dotan Cohen wrote:

>> FIrst of all, how is the % symbol (as in 70%6=4) called in English?
>>
>> 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.
>>
>> Dotan Cohen
>>

>
> The % operator is called "modulo" in English. I don't think the
> difference in implementation is an error. It's just a difference of
> calculation method.
>
> Python will always yield a number x = m%n such that 0 <= x < n, but
> Turbo C will always yield a number such that if x = m%n -x = -m%n. That
> is, since 111 % 10 = 1, -111 % 10 = -1. The two values will always
> differ by n (as used above).

In fact, what you get in C depends on the compiler and architecture, while
Python is always consistent and returns positive remainders.

http://www.python.org/doc/faq/programming/#why-does-22-10-return-3

Arnau Sanchez, Sep 9, 2007

2. ### Bryan OlsonGuest

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

Bryan Olson, Sep 10, 2007

3. ### Dotan CohenGuest

On 10/09/2007, Bryan Olson <> wrote:
> 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]

This seems most logical to me. Turbo C is against the standard, then.

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

Again, logical.

Dotan Cohen

http://what-is-what.com
http://gibberish.co.il

Dotan Cohen, Sep 10, 2007
4. ### Bryan OlsonGuest

Dotan Cohen wrote:
> On 10/09/2007, Bryan Olson <> wrote:
>> 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]

>
> This seems most logical to me. Turbo C is against the standard, then.

No, Turbo C is yielding the one-and-only correct result with respect
to the C standard:

(a/b)*b + a%b shall equal a

(-111/10)*10 + -111%10 shall equal -111

(-111/10) evaluates to -11 by the truncate-toward-zero rule

-11*10 + -111%10 shall equal -111

-110 + -111%10 shall equal -111

-111%10 shall equal -1

--
--Bryan

Bryan Olson, Sep 10, 2007