Modul (%) in python not like in C?

D

Dotan Cohen

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?
 
R

Roy Smith

‭> ‬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
 
D

Dotan Cohen

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?
 
B

Bryan Olson

Dotan said:
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.
 
D

Dennis Lee Bieber

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



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()

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
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
M

mensanator

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



-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?
 
D

Dennis Lee Bieber

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
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
B

Bryan Olson

Dennis said:
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

Also:
70
-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.
 
M

mensanator

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

Scott David Daniels

Bryan said:
Dotan Cohen wrote: ....

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
(e-mail address removed)
 
B

Bryan Olson

Scott said:
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]
 
J

J. Cliff Dyer

Bryan said:
Scott said:
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
 
M

Matthew Woodcraft

J. Cliff Dyer said:
Bryan said:
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-
 
D

Dennis Lee Bieber

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
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
B

Bryan Olson

J. Cliff Dyer said:
Bryan said:
Scott said:
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 %.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top