What is the justification for the result of x % a
being negative whenever a
is negative, in Python 3? In mathematics, modulo is not typically considered an operator, but rather a modifier on the equality relation, so the justification can't really come from there, as far as I can tell. However, perhaps there is some pragmatic reason why e.g. 5 % -2
is -1
and not 1
(even though -5 % 2
is 1
) in some programming languages, like Python?
Asked
Active
Viewed 65 times
0

Noldorin
- 144,213
- 56
- 264
- 302
-
"In mathematics, modulo is not typically considered an operator" -- why say that? – John Coleman Feb 28 '18 at 00:19
-
2@JohnColeman In mathematics I have rarely seen discussion of the "modulo operator"... but rather modular arithmetic, defining a congruence relation. – miradulo Feb 28 '18 at 00:23
-
I disagree with calling this a duplicate - it is asking about what happens when the *second* argument to `%` is negative. Those other questions are about what happens when the *first* argument is negative. – John Coleman Feb 28 '18 at 00:23
-
@JohnColeman and how is the second duplicate not suitable? – miradulo Feb 28 '18 at 00:24
-
@miradulo Perhaps the *answer* to that question (which was about something like `-5 % 4`) answers this question as well, but the actual question is somewhat different. – John Coleman Feb 28 '18 at 00:27
-
@miradulo In mathematics, you often see things likes `a mod n` (used as an operator) and not only `x = y (mod n)`. – John Coleman Feb 28 '18 at 00:30
-
@JohnColeman Not very often though, let's be honest. I say this from my experience in studying number theory. – Noldorin Feb 28 '18 at 00:36
-
@miradulo Indeed. – Noldorin Feb 28 '18 at 00:37
-
@JohnColeman Then you didn't read the dupe correctly. **The sign matches the denominator**, and the reason has been clearly explained by Guido, no less. – wim Feb 28 '18 at 00:37
-
@wim If you read carefully, you'll notice that I'm talking about a negative *divisor*, not *dividend*! The duplicates you marked are about a negative *dividend*. – Noldorin Feb 28 '18 at 00:38
-
1@JohnColeman I would have to disagree that that is "common". In theoretical discussions what benefit does the use like an operator have over the relational notion? Outside of CS books like Concrete Mathematics I don't recall ever seeing it in books. – miradulo Feb 28 '18 at 00:39
-
2dividend = numerator, divisor = denominator. The answer in the dupe explains that the sign matches the denominator, and why. What part are you missing here? – wim Feb 28 '18 at 00:41
-
@Noldorin I agree that in mathematics it might be less common to think of mod as an operator. On the other hand, almost any book in abstract algebra will denote the canonical ring homomorphism of `Z` onto `Z_n` using notation like `f(x) = x mod n` or perhaps `f(x) = x (mod n)` – John Coleman Feb 28 '18 at 00:48
-
@JohnColeman True, although the latter definition is essentially an implicit definition using the congruence relation form of modulo. In any case, both are useful in different contexts. I certainly see why computer scientists tend to prefer the former. – Noldorin Feb 28 '18 at 00:51
-
2@wim, My bad. Some of the answers do indeed address this point. It's just neither of the questions mention the denominator/divisor (rather the numerator/dividend). – Noldorin Feb 28 '18 at 00:51
-
@wim, Perhaps we should edit one of the existing questions/titles (ideally the second) to make this more clear and explicit? – Noldorin Feb 28 '18 at 00:52
-
@JohnColeman Piggybacking off of Noldorin's comment, I would contest that usage is not using the operator form. Rather, `mod` is really a modifier of the `=` relation. – miradulo Feb 28 '18 at 00:57
-
1@Noldorin Yeah, I've edited the question with my own answer into better shape. I'll leave the other one for someone else. – wim Feb 28 '18 at 01:00
-
@wim Thanks! Sounds fair. I can go ahead and delete this one now, perhaps. Do you think that's best? – Noldorin Feb 28 '18 at 01:05
-
@Noldorin No need to delete. Dupes are good! – wim Feb 28 '18 at 01:14