Velocity Reviews > Re: Modul (%) in python not like in C?

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

Arnau Sanchez
Guest
Posts: n/a

 09-09-2007
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/progra...22-10-return-3

Bryan Olson
Guest
Posts: n/a

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

Dotan Cohen
Guest
Posts: n/a

 09-10-2007
On 10/09/2007, Bryan Olson <(E-Mail Removed)> 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

Bryan Olson
Guest
Posts: n/a

 09-10-2007
Dotan Cohen wrote:
> On 10/09/2007, Bryan Olson <(E-Mail Removed)> 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