Why is a negative modulo a negative, a negative? I have understood that $-340$ mod $60$ = $20$ because $-6 \times 60$ = $-360$ is smaller than $-340$.
Can someone explain why $-340$ mod $-60$ = -$40$?
 A: All programming languages I'm aware of maintain the following identity
$$ n = d \cdot (n \mathbin{\text{div}} d) + (n \bmod  d) $$
where $\mathbin{\text{div}}$ is some sort of integer-valued division of integers. The two most common choices are "floor division" and "trucation".
Floor division means that you round the quotient $n/d$ down to the next integer, so you'd have
$$3 \mathbin{\text{div}} 2 = 1 \qquad 4 \mathbin{\text{div}} 2 = 2 \qquad 3 \mathbin{\text{div}} (-2) = -2 $$
Truncation means that you always round towards zero, or that you always round the magnitude down to the next integer:
$$3 \mathbin{\text{div}} 2 = 1 \qquad 4 \mathbin{\text{div}} 2 = 2 \qquad 3 \mathbin{\text{div}} (-2) = -1 $$
Either way, the language defines the remainder operator to be compatible with their choice of integer division operator. Both ways, we have $(-3) \mathbin{\text{div}} (-2) = 1$, so we'd have
$$ -3 = (-2) \cdot ((-3) \mathbin{\text{div}} (-2)) + ((-3) \bmod (-2)) $$
$$ -3 = (-2) \cdot 1 + ((-3) \bmod (-2)) $$
$$ -1 = (-3) \bmod (-2) $$
A: We write $a \equiv b \pmod{m}$ or $a \equiv_{\pmod{m}} b$ if and only if you mean $m \mid a-b$. If we want the binary operator we write $a \bmod m$, but different people may define the binary modulo operator differently. One way is $a \bmod m = a-\lfloor \frac{a}{m} \rfloor m$, which gives meaningful results even for real $a,m$ as long as $m > 0$. In Excel "mod" uses exactly this binary operator that I've just defined, even for negative $m$. That produces a non-positive result when $m < 0$.
A: I think I now might be able to answer your question. (Housekeeping details first: in the equation $n = qm + r$, $q$ is a quotient, $m$ is a modulus and $r$ is a remainder, and we write $n \equiv r \pmod m$).
Because out of infinitely many valid answers, the computer has been programmed to give you just one answer. The criterion $|r| < |m|$ which I alluded to earlier narrows down the possible answers to just two, but the computer has to choose just one to present to you. So then, if $m > 1$, it chooses $0 \leq r < m$, and if $m < -1$, it chooses $m < r \leq 0$.
So, in your example, by the $|r| < |m|$ criterion, either 20 or $-40$ is valid. But if $m = -60$, the computer has been programmed to give you $r = -40$ as the answer, because $-60 < -40 < 0$. If $m = 60$, then it chooses $r = 20$ as the answer, because $0 < 20 < 60$.
But if you're still confused about both $-40$ and 20 being valid answers, a geometrical illustration might help. I want you to draw a horizontal line segment 3 inches long (or 3 kilometers long, or whichever unit of length measurement you like). Label the left endpoint "$-360$". Label the right endpoint "$-300$". Then, 1 inch from the left endpoint, mark a dot and label it $-340$. If $m = -60$, the left endpoint corresponds to $qm$ with $q = 6$. Then $r = 20$. The right endpoint corresponds to $qm$ with $q = 5$, and then $r = -40$. Both pairs of $\{q, r\}$ values "address" the same point on the number line. It just happens that $q = 6, r = 20$ gives us the shortest distance.
Changing to $m = 60$ but keeping $n = -340$ hardly changes things. The point $-360$ still corresponds to a $qm$, but now $q = -6$, though $r = 20$ still. And $qm = -300$ with $q = -5$, but $r = -40$ just like before. I personally prefer $r = 20$, but the way the computers have been programmed, it gives you $r = -40$.
Modular arithmetic isn't the only case in which the computer chooses one answer out of several. For example, if you ask your computer to answer $\sqrt[12]{2}$, you're essentially asking it to solve $x^{12} = 2$. There are a dozen valid answers to this equation. But the computer, perhaps arbitrarily, some might say, gives you only the positive real answer. (There is also the issue of numeric vs. symbolic computations, but that's a whole other can of worms).
A: It's because that's the way the computer was programmed. So mod(-340, -60) leads to return -40. But the more "valid" response would be return 20.
Too many of you humans think computers are magical. A computer subroutine is only as good as the human who programmed it. The Tooth Fairy is magical, Siri is not. The Tooth Fairy can tell you the GCDs of any randomly chosen numbers in $\mathbb{Z}[\sqrt{-5}]$, while Siri would either complain she can't apply the Euclidean algorithm in that domain, or, more likely, change the subject.
Of course at this point you don't care about $\mathbb{Z}[\sqrt{-5}]$, you care about good ol' $\mathbb{Z}$ because that's the domain you're doing most of your arithmetic in. But there is a reason I'm bringing up the GCD function. You see, the greatest common divisor of two negative integers in $\mathbb{Z}$ is a positive integer, for even if they're coprime, they nevertheless share $1$ as a trivial divisor. For example, $\gcd(-340, -60) = 20$. A computer, in all its glorious stupidity, could be delayed or prevented from arriving at the right answer for gcd if mod is not defined to always give a positive or $0$ result.
So how does Microsoft Excel define mod? I don't want to tell you, I've got my reasons, but I will tell you that, in very broad strokes, it goes something like this:
function integer mod(integer n, integer modulus) {
    var real intermediate1 := n / modulus;
    var integer intermediate2 := floor(intermediate1);
    return n - (intermediate2 * modulus)
}

You can't check every possible input, but you can check a few positive inputs to see whether or not it gives the correct results. And you can check that this will give $-40$ in your main test case with the negative inputs. But if you want it to give $20$ instead that's accomplished easily enough:
function integer mod(integer n, integer modulus) {
    var integer adjustedModulus := abs(modulus);
    var real intermediate1 := n / adjustedModulus;
    var integer intermediate2 := floor(intermediate1);
    return n - (intermediate2 * adjustedModulus)
}

Then, when you send it mod(-340, -60), it will change the -60 to 60 in its special local variable adjustedModulus. intermediate2 will be -6 rather than 5 and the final result will be the same as if you called the previous function with -340, 60.
But when a human concerns himself too much with programming a silly piece of machinery, he dumbs down his own knowledge. It would be much better for you to think of $-40$ and $20$ as belonging to the same coset of the principal ideal $60\mathbb{Z}$, which you can notate as $-40 + \langle 60 \rangle$ or $20 + (60)$, to name just two of the many possible notations you could use.
A: You could also say that $-340 \equiv -40 \pmod{60}$. It is quite common to see in number theory books things like "$-1 \pmod p$", where $p$ is a positive prime.
So really, $-340 \equiv 20 \pmod{-60}$ is as acceptable as $-340 \equiv -40 \pmod{-60}$.
Here's the way I like to think about it: the expression $\pmod c$ tells us we're thinking about the multiples of $c$, which includes numbers like $-c$ and $-488c$ (so in the case of $-60$, we're also thinking about numbers like 60 and $-300$). And in $a \equiv b \pmod c$, the $b$ tells us that $a$ is $b$ integer notches away from a multiple of $c$. The sign of $b$ tells us in what direction we have to go to hit a multiple of $c$.
Thus, in your example, $b = -40$ means that if we go $-40$ "back" from $-340$, we hit $-300$, and if we go 20 "forward", we hit $-360$. (Don't get too hung up on which is forward and which is back, as long as you don't switch them in the middle of a single operation).
A: $$-340=(-300-40)=(-5)\cdot60-40\equiv-40\equiv-360+20\equiv 20\pmod{60}$$
Or by definition $$a\equiv b\pmod{60}\implies 60\mid a-b\\60\mid -340-(-40)\\60\mid -300$$
