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?
 A: $\, a\bmod n\ $ and $\,a\, \%\, n\,$ are operations that denote the remainder $\,r\,$ left when dividing $\, a\,$ by $\,n\,$ using the (Euclidean) integer division algorithm:  $\ a = q\, n + r\,$ with quotient $\,q\,$ and remainder $\,r.\,$ For naturals $\,a,n\,$ the remainder is usually normalized so  $\,0\le r< n,\,$ but it can instead be normalized into any complete set of residues $\bmod n,\,$ e.g. the least magnitude reps $\,0,\pm1, \pm2\,\pmod{\!5}$
Such normal form reps allow us to test congruence $\,\bmod n\,$ by comparing  normal forms (analogous to testing fraction equivalence by comparing their normalized least terms), i.e.
$$ a\equiv b\!\!\!\pmod{\!n}\iff a\bmod n = b\bmod n\qquad$$
Recall the congruence relation $\,a\equiv b\pmod{\!n}\,$ means $\,n\mid a-b,\,$ i.e. $\,n\,$ divides $\,a-b.\,$ Be careful not to confuse this relation $\!\bmod\!$ with the above operation (see here for more on this distinction).
There are various conventions in use that specify how to define the quotient and remainder for the division algorithm when extending the above  from naturals to integers, i.e. allowing negative integers. The remainder is uniquely determined once one defines the quotient, so usually the conventions say which way to round the quotient, e.g. towards $\,0\,$ or towards the nearest integer or towards $\pm\infty.$ Some programming languages provide all of the possibilities, e.g. see the  floor, ceiling, round, truncate functions in Common Lisp.
The conventions always satisfy  $\ a\bmod n \equiv a\,\pmod{\!n},\ $ i.e. the normal form of $\,a\,$ is an element of the congruence equivalence class $\, a+n\Bbb Z\,$ of all integers congruent to $\,a,\,$ so normal form reduction is compatible with modular arithmetic $\bmod n.\,$ Generally we can replace an argument of a sum or product (but not exponent) by any congruent integer and we obtain a congruent result, by the Congruence Sum  & Product Rules, or their inductive extension: the Congruence Polynomial Rule. When performing modular computation this property allows us to use our ingenuity to choose (non-normal) reps that simplify the calculation, e.g. using $\,-1\,$ vs. $\,9\,$ when computing $\,9^{2n}\bmod 10.\,$ But machines generally lack such ingenuity - they are programmed using deterministic algorithms so they must use a modular reduction operation $\,a\,\mapsto\,  a\bmod n.$
A web search will turn up much further discussion of the various conventions in use  e.g. see Wikipedia and see also D. Leijen, Division and Modulus for Computer Scientists.
A: Note that $-5 = -7 +2$ and $9 = -(-7)+2$. When you do these calculations, your remainder is always nonnegative by definition (just to make things easily stated) but your coefficient $q$ (in $p = qd+r$) can be any number in $\Bbb Z$. This is why $-1$ is admissible as a coefficient in front of $-7$ when decomposing $9$.
A: Recall that $a \equiv b \bmod n$ if and only if $n$ divides $a - b$. This is the definition.
So is it true that $-5 \equiv 9 \bmod -7$?   
A: It's absolutely true that $9\equiv 2\bmod (-7)$. It's also true that $9\equiv -5\bmod (-7)$, and also $9\equiv 9\bmod (-7)$, $9\equiv 16\bmod (-7)$,  etc.
I suspect you're being confused by the output from a computer. The output that a computer makes when the symbols 9 % -7 are entered into it might reflect whatever conventions a programmer decided on (e.g., this StackOverflow post).
A: 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.
