Modul (%) in python not like in C?

Discussion in 'Python' started by Dotan Cohen, Sep 9, 2007.

  1. Dotan Cohen

    Dotan Cohen Guest

    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

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

    --

    ×-ב-×’-ד-×”-ו-×–-×—-ט
    ×™-ך-×›-ל-×-מ-ן-× -ס
    ע-ף-פ-ץ-צ-ק-ר-ש-ת

    A: Because it messes up the order in which people normally read text.
    Q: Why is top-posting such a bad thing?
    Dotan Cohen, Sep 9, 2007
    #1
    1. Advertising

  2. Dotan Cohen

    Roy Smith Guest

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

    In article‭ <‬mailman.294.1189364475.2658‭.‬‭>,‬
    ‭ "‬Dotan Cohen‭" <‬‭> ‬wrote‭:‬

    ‭> ‬FIrst of all‭, ‬how is the‭ % ‬symbol‭ (‬as in 70%6‭=‬4‭) ‬called in English‭?‬

    It's called‭ "‬modulo‭", ‬but most people pronounce it‭ "‬mod‭".‬

    It's documented at http‭://‬docs.python.org/ref/binary.html
    Roy Smith, Sep 9, 2007
    #2
    1. Advertising

  3. Dotan Cohen

    Dotan Cohen Guest

    =?UTF-8?Q?Re:_Re=C3=A2=C2=AD:_=C3=A2=C2=ACM?==?UTF-8?Q?odul=C3=A2=C2=AD_(%)_=C3=A2=C2=ACi?==?UTF-8?Q?n_python_not_like_in_C=C3=A2=C2=AD=3F=C3=A2=C2=AC?=

    On 09/09/2007, Roy Smith <> wrote:
    > In article <>,
    > "Dotan Cohen" <> wrote:
    >
    > > FIrst of all, how is the % symbol (as in 70%6=4) called in English?

    >
    > It's called "modulo", but most people pronounce it "mod".
    >
    > It's documented at http://docs.python.org/ref/binary.html
    >


    Thanks, Roy. But it's hard to find when one doesn't know it's name!

    Dotan Cohen

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

    --

    ×-ב-×’-ד-×”-ו-×–-×—-ט
    ×™-ך-×›-ל-×-מ-ן-× -ס
    ע-ף-פ-ץ-צ-ק-ר-ש-ת

    A: Because it messes up the order in which people normally read text.
    Q: Why is top-posting such a bad thing?
    Dotan Cohen, Sep 10, 2007
    #3
  4. Dotan Cohen

    Bryan Olson Guest

    Dotan Cohen wrote:
    > FIrst of all, how is the % symbol (as in 70%6=4) called in English?


    The operator is usually called "mod". (The symbol is usually
    called "percent".)

    I reserve "modulo" for its usage in mathematics. 70 modulo 6
    is an equivalence class containing infinitely many integers.
    In math talk, 4 is the least residue of 70 modulo 6.


    > Second, in Turbo C -111%10=-1 however in python -111%10=9. Is one or
    > the other in error?


    Turbo C is correct here with respect to the C standard. Python
    is correct with respect to the Python Reference Manual.

    They also disagree about integer division. C rounds toward zero;
    Python rounds downward.

    In C: -111 / 10 evaluates to -11
    In Python: -111 / 10 evaluates to -12


    > Is this a known gotcha? I tried to google the
    > subject however one cannot google the symbol %.


    I think it's a known gotcha. I cited the refs in another post.


    --
    --Bryan
    Bryan Olson, Sep 10, 2007
    #4
  5. On Mon, 10 Sep 2007 01:06:59 GMT, Bryan Olson <>
    declaimed the following in comp.lang.python:


    > They also disagree about integer division. C rounds toward zero;
    > Python rounds downward.
    >
    > In C: -111 / 10 evaluates to -11
    > In Python: -111 / 10 evaluates to -12
    >
    >
    > > Is this a known gotcha? I tried to google the
    > > subject however one cannot google the symbol %.

    >
    > I think it's a known gotcha. I cited the refs in another post.


    The best answer is probably to be found from the definition of
    divmod()

    >>> divmod(70, 6)

    (11, 4)
    >>> 6*11 + 4

    70
    >>> divmod(-70, 6)

    (-12, 2)
    >>> 6 * -12 + 2

    -70
    >>>


    Or in general terms

    (a, b) = divmod(x, y)
    x = y * a + b

    IOWs, the definition of modulo, and the definition of integer division,
    are linked...

    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
    Dennis Lee Bieber, Sep 10, 2007
    #5
  6. Dotan Cohen

    Guest

    On Sep 9, 11:43?pm, Dennis Lee Bieber <> wrote:
    > On Mon, 10 Sep 2007 01:06:59 GMT, Bryan Olson <>
    > declaimed the following in comp.lang.python:
    >
    > > They also disagree about integer division. C rounds toward zero;
    > > Python rounds downward.

    >
    > > In C: -111 / 10 evaluates to -11
    > > In Python: -111 / 10 evaluates to -12

    >
    > > > Is this a known gotcha? I tried to google the
    > > > subject however one cannot google the symbol %.

    >
    > > I think it's a known gotcha. I cited the refs in another post.

    >
    > The best answer is probably to be found from the definition of
    > divmod()
    >
    >
    >
    > >>> divmod(70, 6)

    > (11, 4)
    > >>> 6*11 + 4

    > 70
    > >>> divmod(-70, 6)

    > (-12, 2)
    > >>> 6 * -12 + 2

    > -70
    >
    > Or in general terms
    >
    > (a, b) = divmod(x, y)
    > x = y * a + b


    But, in general, doesn't (-11,-4) also satisfy
    x = y * a + b
    -70 = 6 * -11 + (-4)?

    Do you overpay your car loans and wait for a refund?

    Or do you make your last payment the residue of the
    debt after making 47 (or whatever) equal payments?


    >
    > IOWs, the definition of modulo, and the definition of integer division,
    > are linked...
    >
    > --
    > Wulfraed Dennis Lee Bieber KD6MOG
    >
    > HTTP://wlfraed.home.netcom.com/
    > (Bestiaria Support Staff: )
    > HTTP://www.bestiaria.com/
    , Sep 10, 2007
    #6
  7. On Sun, 09 Sep 2007 22:57:24 -0700, ""
    <> declaimed the following in comp.lang.python:


    >
    > But, in general, doesn't (-11,-4) also satisfy
    > x = y * a + b
    > -70 = 6 * -11 + (-4)?
    >
    > Do you overpay your car loans and wait for a refund?
    >

    As I stated, integer division and modulo are linked... Which is
    "correct" is probably dependent upon the language specifications. Either
    method allows one to recover the original value from the output of
    divmod.

    Whether that works in the way you intend is a different matter -- my
    car loans usually have interest factors which are applied per payment by
    payment date, so using modulo (or divmod) is not applicable, one must
    actually build the table of payments one at a time (especially since the
    automatic payments taken from paychecks gets "corrupted" during
    holidays, when paychecks are issued a day or two earlier than normal,
    meaning less interest charges during that period and more applied to
    principle; with more on interest the following week, and less on
    interest).
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
    Dennis Lee Bieber, Sep 10, 2007
    #7
  8. Dotan Cohen

    Bryan Olson Guest

    Dennis Lee Bieber wrote:
    > The best answer is probably to be found from the definition of
    > divmod()


    The divmod() function is one of those little delights that reminds
    me why I love Python, but I do not think it answers the question
    here. The definition of divmod() references the '%' operation, and
    not the other way around.

    http://docs.python.org/lib/built-in-funcs.html
    http://docs.python.org/ref/binary.html


    >>>> divmod(70, 6)

    > (11, 4)
    >>>> 6*11 + 4

    > 70
    >>>> divmod(-70, 6)

    > (-12, 2)
    >>>> 6 * -12 + 2

    > -70


    Also:

    >>> divmod(70, -6)

    (-12, -2)
    >>> -6*-12 + -2

    70

    >>> divmod(-70, -6)

    (11, -4)
    >>> -6*11 + -4

    -70

    > Or in general terms
    >
    > (a, b) = divmod(x, y)
    > x = y * a + b


    > IOWs, the definition of modulo, and the definition of integer division,
    > are linked...


    Right. Thus given integers x and y, y!=0, Python and C agree:

    x == (y * (x / y)) + (x % y)

    The disagreement is how integer division rounds. In C, integer
    division rounds toward zero. In Python, integer division rounds
    downward.


    --
    --Bryan
    Bryan Olson, Sep 10, 2007
    #8
  9. Dotan Cohen

    Guest

    On Sep 10, 1:38 am, Dennis Lee Bieber <> wrote:
    > On Sun, 09 Sep 2007 22:57:24 -0700, ""
    > <> declaimed the following in comp.lang.python:
    >
    >
    >
    > > But, in general, doesn't (-11,-4) also satisfy
    > > x = y * a + b
    > > -70 = 6 * -11 + (-4)?

    >
    > > Do you overpay your car loans and wait for a refund?

    >
    > As I stated, integer division and modulo are linked... Which is
    > "correct" is probably dependent upon the language specifications.


    I didn't make an opinion on which was "correct".

    > Either
    > method allows one to recover the original value from the output of
    > divmod.


    My point was the need to satisfy x = y * a + b doesn't
    explain WHY divmod(-70,6) returns (-12,2) instead of (-11,-4).

    >
    > Whether that works in the way you intend is a different matter -- my
    > car loans usually have interest factors which are applied per payment by
    > payment date, so using modulo (or divmod) is not applicable, one must
    > actually build the table of payments one at a time (especially since the
    > automatic payments taken from paychecks gets "corrupted" during
    > holidays, when paychecks are issued a day or two earlier than normal,
    > meaning less interest charges during that period and more applied to
    > principle; with more on interest the following week, and less on
    > interest).


    Ok, poor example. Suppose instead we are slicing a 70 character string
    from the right. We certainly CAN'T have 6 12-letter slices, can we?
    But we can have 6 11-letter slices with 4 characters left over.

    And there are probably examples where (-12,2) would make more sense,
    although I can't think of one at the moment.

    Again, I'm not saying (-12,2) is wrong, just that the explanation
    why it's (-12,2) is wanting since there are cases where (-11,-4)
    makes more sense.

    > --
    > Wulfraed Dennis Lee Bieber KD6MOG
    >
    > HTTP://wlfraed.home.netcom.com/
    > (Bestiaria Support Staff: )
    > HTTP://www.bestiaria.com/
    , Sep 10, 2007
    #9
  10. Bryan Olson wrote:
    > Dotan Cohen wrote:

    ....
    >> Second, in Turbo C -111%10=-1 however in python -111%10=9. Is one or
    >> the other in error?

    >
    > Turbo C is correct here with respect to the C standard. Python
    > is correct with respect to the Python Reference Manual.
    >
    > They also disagree about integer division. C rounds toward zero;
    > Python rounds downward.
    >
    > In C: -111 / 10 evaluates to -11
    > In Python: -111 / 10 evaluates to -12


    C, which was designed as a "high level assembly language," does not
    tightly define the results of / and % for negative numbers. Instead
    it defines the result for positive over positive, and constrains the
    result for the others. The reason is simple: most CPUs available
    at the time provided a single operation that produced both the quotient
    and the remainder, but some produced -1 for -3 / 2, and others produced
    1 for -3 / 2. Either result is justifiable, but the most common use
    of division and remainder is positive / positive, and it would be a
    shame to generate a test for every division and modulus just to cover
    the negative / positive case. You would be slowing down the majority
    of code to satisfy the (usually non-existant) corner case.

    -Scott David Daniels
    Scott David Daniels, Sep 11, 2007
    #10
  11. Dotan Cohen

    Bryan Olson Guest

    Scott David Daniels wrote:
    > C, which was designed as a "high level assembly language," does not
    > tightly define the results of / and % for negative numbers. Instead
    > it defines the result for positive over positive, and constrains the
    > result for the others.


    Not true. Here it is again:

    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]


    --
    --Bryan
    Bryan Olson, Sep 11, 2007
    #11
  12. Dotan Cohen

    Paul Rubin Guest

    Bryan Olson <> writes:
    > > FIrst of all, how is the % symbol (as in 70%6=4) called in English?

    >
    > The operator is usually called "mod". (The symbol is usually
    > called "percent".)


    In Lisp, we'd say Python implements the mod function and C implements
    the rem (remainder) function. See also the functions floor and
    truncate:

    http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node130.html
    Paul Rubin, Sep 11, 2007
    #12
  13. Bryan Olson wrote:
    > Scott David Daniels wrote:
    >
    >> C, which was designed as a "high level assembly language," does not
    >> tightly define the results of / and % for negative numbers. Instead
    >> it defines the result for positive over positive, and constrains the
    >> result for the others.
    >>

    >
    > Not true. Here it is again:
    >
    > 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]
    >
    >
    >

    But C was around for a long time before the 1999 standard. C89,
    commonly called ANSI C, is still very commonly used in compilers, and
    K&R C goes back to 1972. Is truncation toward 0 the standard for K&R C
    as well?

    Cheers,
    Cliff
    J. Cliff Dyer, Sep 11, 2007
    #13
  14. J. Cliff Dyer <> wrote:
    >Bryan Olson wrote:
    >> Not true. Here it is again:
    >>
    >> 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]



    > But C was around for a long time before the 1999 standard. C89,
    > commonly called ANSI C, is still very commonly used in compilers, and
    > K&R C goes back to 1972. Is truncation toward 0 the standard for K&R C
    > as well?


    As I remember, the behaviour for negative 'a' wasn't specified in K&R C
    or in C89; the rule was tightened up for C99.

    -M-
    Matthew Woodcraft, Sep 11, 2007
    #14
  15. On 10 Sep 2007 23:13:57 -0700, Paul Rubin <http://>
    declaimed the following in comp.lang.python:

    >
    > In Lisp, we'd say Python implements the mod function and C implements
    > the rem (remainder) function. See also the functions floor and
    > truncate:
    >

    To add to the fray, Ada (95) defines both mod and rem (from the
    standard):

    a = (a/b) * b + (a rem b) -- rem has sign of a
    -- absolute value less than absolute of b

    a = b * N + (a mod b) -- mod has sign of b
    -- absolute value less then absolute of b
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
    Dennis Lee Bieber, Sep 11, 2007
    #15
  16. Dotan Cohen

    Bryan Olson Guest

    J. Cliff Dyer wrote:
    > Bryan Olson wrote:
    >> Scott David Daniels wrote:
    >>
    >>> C, which was designed as a "high level assembly language," does not
    >>> tightly define the results of / and % for negative numbers. Instead
    >>> it defines the result for positive over positive, and constrains the
    >>> result for the others.
    >>>

    >> Not true. Here it is again:
    >>
    >> 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]
    >>

    > But C was around for a long time before the 1999 standard. C89,
    > commonly called ANSI C, is still very commonly used in compilers, and
    > K&R C goes back to 1972. Is truncation toward 0 the standard for K&R C
    > as well?


    Looks like you have a good point there. I do not have a copy of
    the C89 standard, but do I have editions of Harbison&Steele that
    reflect the 1989 standard, and they confirm your suspicion that
    the results of the '/' and '%' operators on integer operants
    were not uniquely defined in cases where an operand could be
    negative.

    Describing the '/' operator:

    For integral operands,, if the mathematical quotient is
    is not an exact integer, then the result will be one of
    the integers closest to the mathematical quotient of the
    operands. Of those two integers, the one closer to 0 must
    be chosen if both operands are positive

    Furthermore:

    It is always true that (a/b)*b + a%b is equal to a if b
    is not 0

    Quoted from: S. P. Harbison and G. L. Steele. /C: A
    Reference Manual, Third Edition/, Prentice Hall, 1991;
    Section 7.6.1, page 187.

    So Arnau Sanchez and Scott David Daniels were correct with
    respect to C89 and older standards, and many implementations
    have not yet adopted the 1999 standard that strictly defines
    the results of / and %.


    --
    --Bryan
    Bryan Olson, Sep 15, 2007
    #16
    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:
    353
    Tobel
    Jan 27, 2006
  2. Matej Cepl
    Replies:
    2
    Views:
    240
    Matej Cepl
    May 26, 2007
  3. J. Cliff Dyer

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

    J. Cliff Dyer, Sep 9, 2007, in forum: Python
    Replies:
    2
    Views:
    268
  4. Arnau Sanchez

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

    Arnau Sanchez, Sep 9, 2007, in forum: Python
    Replies:
    3
    Views:
    301
    Bryan Olson
    Sep 10, 2007
  5. stef mientki

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

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

Share This Page