5

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$?

DDC
  • 169
  • By definition $a = b, mod(c) \iff c|(a-b)$ – Bman72 Dec 28 '14 at 15:39
  • 2
    There are various conventions for extending mod and division (with remainder) to negative integers, e.g. see this answer for references. – Bill Dubuque Dec 28 '14 at 15:46
  • I could not understand it. Basically I want someone to explain me this: -340 mod -60 = -40, 340 mod 60 = 40, -340 mod 60 = 20, 340 mod -60 = -20 – DDC Dec 28 '14 at 15:59
  • @DDC Use the definition i gave you. I can do the first example you listed for you, and if you have troubles with the other ones just ask :). You want to understand why $-340 \equiv -40 \text{mod} 60$. This is true by definition iff $60 , | , (-340-(-40)) \iff 60 , | , -300$, but this is true since $-300 = 60 \cdot (-5) \implies 60 , | , -300$. The definition can intuitively been understood as $a\equiv b \quad \text{mod}(c)$ if $c$ divides the difference of the two numbers $a,b$. – Bman72 Dec 28 '14 at 16:16
  • Or if you want to get algebraic number theoretic about it, you can say that a congruence measures a coset's distance from the nearest or second nearest representative of a principal ideal. Since $\langle 60 \rangle$ and $\langle -60 \rangle$ are the same thing, the same congruences apply to $-60$ that apply to 60. – Robert Soupe Dec 28 '14 at 16:22

6 Answers6

5

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) $$

  • Yes this is the other part of the implementation design answer, and rearranging your equation and using floor division gives the formula I gave in my answer. – user21820 Dec 29 '14 at 01:21
  • Yes, but this is much clearer, and gets my unhesitating upvote. Whether it is or isn't the answer that clears up the asker's confusion, I don't know. – Robert Soupe Dec 29 '14 at 01:55
4

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$.

user21820
  • 57,693
  • 9
  • 98
  • 256
  • I wrote for binary only. I think it was edited otherwise! – DDC Dec 28 '14 at 16:23
  • @DDC: Ah I see. You can click on "Edit" in my post to see the LaTeX commands needed to display the various modulo operators. Does what I say answer your question too? – user21820 Dec 28 '14 at 16:24
  • Thanks, I did it. But I don't get the answer. – DDC Dec 28 '14 at 16:28
  • @DDC: Do you know the floor function? If you do, do you see why the formula I gave satisfies the property that $a \bmod m \equiv a \pmod{m}$ for any integer $a,m$ such that $m > 0$? You can also check that Excel follows this formula for floating point. I said wrongly above; it follows even for negative $m$. Try it! – user21820 Dec 28 '14 at 16:33
  • My question is very basic. When I divide -340 by 60, I choose quotient -6 because it gives me -360 which is smaller than and nearest to -340. I don't understand what are we doing in the case of -340 mod -60 = -40? – DDC Dec 28 '14 at 16:37
  • @DDC what do you mean with "i choose quotient $-6$?" – Bman72 Dec 28 '14 at 16:41
  • @DDC: But I have given you the full answer already. Different people choose different definitions for the modulo operation. It so happens that Excel chose to use the definition I gave in my answer. There is no reason needed, unless you want to ask the programmers involved. – user21820 Dec 28 '14 at 16:42
  • We're trying to solve $-60q + r = -340$ in integers, so that $|r| < |-60|$. If $q = 6$, then $r = 20$. But if $q = 5$, then $r = -40$. – Robert Soupe Dec 28 '14 at 16:44
  • @RobertSoupe: Yes indeed, but it's hard to tell what's the reason it was implemented that way. I'm a programmer and might in fact do the same simply because it is easier, and not really because of any mathematical reason. – user21820 Dec 28 '14 at 16:46
  • Well, I gotta go see that Stephen Hawking movie. I might come back to this question later. – Robert Soupe Dec 28 '14 at 16:47
  • @RobertSoupe sir, I am sorry! I still don't get it! – DDC Dec 28 '14 at 16:50
  • @DDC: Just what do you not get?? If you don't understand my formula, read up on the floor function! If you do, you can substitute in your values yourself and see that it gives the answer you got! – user21820 Dec 28 '14 at 16:57
  • I tried it in Excel, but it doesn't work in case of -340, 60 and 340, -60! – DDC Dec 28 '14 at 17:06
  • @DDC: You're wrong. I tried it too and it works! – user21820 Dec 28 '14 at 17:07
  • I think the floor function is a red herring for the asker at this point. The concept of principal ideals and cosets might be much more productive. By the way, you've got to see The Theory of Everything, it's a wonderful movie. – Robert Soupe Dec 29 '14 at 00:59
  • 1
    I don't have Excel, but I do have OpenOffice Calc. After a few minutes getting confused by a 508 error, I got the MOD function to work for me. Indeed it gives 20 for a modulus of 60 and $-40$ for a modulus of $-60$. – Robert Soupe Dec 29 '14 at 01:01
  • @RobertSoupe: Well I don't think the floor function is a detour at all. We want to subtract away the closest integer multiple of the modulus. But which integer to choose? Well, the floor of the quotient is one obvious and consistent choice. Also, if I am right about the software, my answer also suggests why it is designed that way unlike what you think is more natural. That said, the question asked is of the same type as "Why is $(-8)^\frac{2}{6}$ negative but $((-8)^2)^\frac{1}{6}$ positive?" The answer is "Err... Depends on how you define them...". – user21820 Dec 29 '14 at 01:11
  • @user21820 Well, I have to admit that I'm not much of a programmer, not having gotten very far in many a C++ book. But I still think that for the asker the floor function is not making much sense. He might also be super confused if we just unleash a bunch of unexplained algebraic number theory terminology, but maybe, just maybe, if we can gently explain the concept of principal ideals, additive inverses and cosets, his confusion might clear up. – Robert Soupe Dec 29 '14 at 01:39
  • @RobertSoupe: Haha you can try. But seriously who can't understand the floor function shown on Wikipedia? Also, Hurkyl has given a nice complementary answer, which if correct gives further insight into what the language designers might be thinking. – user21820 Dec 29 '14 at 01:47
  • 1
    Now, @user21820, explain it to me, not to the asker: we have $\lfloor \frac{-340}{-60} \rfloor = 5$. Oh, I see, that leads to $q = 5$ and $r = -40$... – Robert Soupe Dec 29 '14 at 01:47
  • @RobertSoupe: Haha did you just answer yourself in your last comment? – user21820 Dec 29 '14 at 01:48
  • I guess I did... – Robert Soupe Dec 29 '14 at 01:50
1

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).

Robert Soupe
  • 14,663
1

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.

  • 1
    Upvote on the mathematical principles. If I could, I would give a quarter downvote on the philosophical principles. You do realize that several silly pieces of machinery are working together to carry your message here? – Robert Soupe Dec 30 '14 at 01:18
0

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).

Robert Soupe
  • 14,663
  • In MS Excel, When we do 340 mod -60 it goes forward (i.e. to -360) but in -340 mod -60 it goes back at -300. Same is with WolframApha! Why so? – DDC Dec 28 '14 at 16:19
  • Computer programs like Excel and REALbasic might not implement MOD the same way a mathematician would (kind of like how their LOG is a base 10 logarithm, not a natural logarithm). In The Definitive Guide to REALbasic, the author warns about this (I can get you the page number if you want). As for Wolfram|Alpha, let me do some testing and get back to you. – Robert Soupe Dec 28 '14 at 16:25
  • So I tried Mod[-340, 60] in Wolfram|Alpha. It said 20. So far so good. (Also check out the "visual representation," it might clarify things for you). Next, I tried Mod[-340, -60]. It said -40. This is not the best answer, in my opinion, but it is technically correct since $60 - 40 = 20$. – Robert Soupe Dec 28 '14 at 16:39
0

$$-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$$

kingW3
  • 13,496