Mark Dickinson added the comment:

Terry: can you clarify which part you think is potentially confusing? I'm 
having a hard time seeing the text as confusing, but I suspect I'm too close to 
the subject matter to be able to tell.

Charles: I think you're missing the point (which does rather reinforce Terry's 
suggestion that this FAQ could be improved). You say:

> "-190 % 12 == -10 is wrong according to the C definition for computer modulo 
> arithmetic."

But that's the point: for C (specifically C99[*]), -10 is the *correct* result 
from the operation -190 % 12. And that's exactly why this is a FAQ: Python is 
behaving differently from many other mainstream languages (C, Java, C++, C#, 
...) here, so it's useful to understand the justification for this design 
decision.

For C in particular, this behaviour is mandated by section 6.5.5p6 of C99, 
which reads:

> When integers are divided, the result of the / operator is the algebraic
> quotient with any fractional part discarded. If the quotient a/b is
> representable, the expression (a/b)*b + a%b shall equal a.

The first part of this forces -190 / 12 to be -15 (the result of discarding the 
fractional part of the true quotient -15.833....); the second then forces -190 
% 12 to be (-190) - (-15)*12, which is -10.

([*] In C89, the rounding direction of a/b for negative a, and hence the 
behaviour of a%b, was left implementation defined, but same-sign-as-a appears 
to have been the dominant behaviour.)

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue31021>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to