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

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

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

    More info:

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

    http://groups.google.com/group/comp.lang.python/search?group=comp.lang.python&q=modulo negative C
    Arnau Sanchez, Sep 9, 2007
    #1
    1. Advertising

  2. Arnau Sanchez

    Bryan Olson Guest

    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
    #2
    1. Advertising

  3. Arnau Sanchez

    Dotan Cohen Guest

    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
    #3
  4. Arnau Sanchez

    Bryan Olson Guest

    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
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tobel
    Replies:
    0
    Views:
    352
    Tobel
    Jan 27, 2006
  2. Matej Cepl
    Replies:
    2
    Views:
    240
    Matej Cepl
    May 26, 2007
  3. Dotan Cohen

    Modul (%) in python not like in C?

    Dotan Cohen, Sep 9, 2007, in forum: Python
    Replies:
    15
    Views:
    476
    Bryan Olson
    Sep 15, 2007
  4. J. Cliff Dyer

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

    J. Cliff Dyer, Sep 9, 2007, in forum: Python
    Replies:
    2
    Views:
    268
  5. stef mientki

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

    stef mientki, Sep 9, 2007, in forum: Python
    Replies:
    5
    Views:
    260
    John Machin
    Sep 10, 2007
Loading...

Share This Page