Message299133
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.) |
|
Date |
User |
Action |
Args |
2017-07-25 19:13:07 | mark.dickinson | set | recipients:
+ mark.dickinson, terry.reedy, docs@python, wohlganger |
2017-07-25 19:13:07 | mark.dickinson | set | messageid: <1501009987.46.0.207110183603.issue31021@psf.upfronthosting.co.za> |
2017-07-25 19:13:07 | mark.dickinson | link | issue31021 messages |
2017-07-25 19:13:06 | mark.dickinson | create | |
|