Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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.

More info:

http://www.python.org/doc/faq/progra...22-10-return-3

http://groups.google.com/group/comp....ulo+negative+C

 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Modul (%) in python not like in C? Dotan Cohen Python 15 09-15-2007 11:56 AM
Re: Modul (%) in python not like in C? stef mientki Python 5 09-10-2007 10:37 AM
Re: Modul (%) in python not like in C? J. Cliff Dyer Python 2 09-09-2007 08:12 PM
email modul with writing to mboxes (and locking) for python 2.4.*? Matej Cepl Python 2 05-26-2007 07:54 AM
Searching a performant Newsletter Modul Tobel Java 0 01-27-2006 01:12 PM



Advertisments