Why 9 mod -7 = -5? Quotient and remainder with negative integers. Forgive me if this question does not belong on this site for it is simplistic and this is my first post, however I do not seem to understand the modulo function when it comes to negative numbers.
I'd assume the process for calculating modulo would be the same as with positive numbers:


*

*9 % 7 = 2 because 9 - (7 * 9/7) = 9 - (7 * 1) = 2


So wouldn't 9 % (-7) = 9 - (-7 * -9/7) = 9 - (-7 * -1) = 2?

Spelling out a connection between various arithmetical operations not covered in the other answers. It is really relevant when we look at remainders of negative integers modulo a positive integer, so arguably this is not an answer to this title question. But, I sense a push towards linking most related queries to this thread, so here comes.

In elementary number theory (and elementary algebra) we more often than not choose to use $\{0,1,2,\ldots,n-1\}$ as representatives of the equivalence classes of the relation congruent modul $n$. This is fine. It is natural that an algebraically trained person expects the remainder operation in programming languages to also only use these are outputs. I have created interesting bugs in my time because I also had this expectation. Thank fully the bug appears rather soon, and is not exceedingly difficult to diagnose.
The reason to use negative remainders in some situations is the following. To understand it we need to look at the combination of operations of integer arithmetic.
Integer multiplication and addition are unproblematic. The two operations I discuss are integer division and the remainder. I choose to call these $\operatorname{DIV}$
and $\operatorname{MOD}$ respectively.
Think about the familiar step from Euclid's algorithm:
$$a=qb+r.$$
Here $a$ and $b$ are integers, and we are well used to thinking about $q$ as the quotient and $r$ as the remainder. Like
$$q= (a\operatorname{DIV} b)\qquad\text{and}\qquad 
r = (a\operatorname{MOD} b).$$
In Euclid's algorithm, more often than not, $a$ and $b$ are both non-negative ($b>0$ actually). And the following relation always holds
$$a=(a\operatorname{DIV} b) * b + (a\operatorname{MOD} b).\tag{1}$$
This is a good thing to have! We certainly expect computer programs to observe $(1)$ for all $a,b$, $b\neq0$.
The catch is that making $(1)$ true for all inputs ties our hands somewhat. It is not unnatural to also expect that
$$(-a)\operatorname{DIV} b = - (a\operatorname{DIV} b)\tag{2}$$
should also hold. This leads to an explanation of, not really your current "anomaly", but a slightly different one. For example, what should be the result of
$(-9)\operatorname{MOD} 7?$
We most certainly want $9\operatorname{DIV}7$ to be $1$. Therefore rule $(2)$ imposes that $(-9)\operatorname{DIV}7=-1$. But, then the rule $(1)$ forces the result
$$(-9)\operatorname{MOD} 7 = -2$$
upon us.
This has the outcome that when we ask a computer what is the remainder when a negative integer is divided by $b>0$, the answer will be in the set $\{0,-1,-2,\ldots,-(b-1)\}$. Consequently, when $(1)$ and $(2)$ are upheld, we get
$$
(-a)\operatorname{MOD} b = -(a\operatorname{MOD} b).\tag(3)
$$

The above explanation does not really explain the problem in the title query. It might also make sense to have rules similar to $(2)$ and $(3)$ when changing the sign of $b$. For reasons that escape me (anyone???) this is apparently not done. At least your computer (or programming language) apparently uses the floor function when calculating $9\operatorname{DIV} (-7) =-2$, and then uses a matching definition for
$9\operatorname{MOD} (-7) =-5$ so that $(1)$ is still upheld.
