2

According to my professor (and maple)

a mod -b such as 1000%(-9)

Is an invalid question that cannot be answered, since c>0,
he claimed also that Maple will respond with "Error, invalid mod".

However, Wolfram Alpha will respond with -8, a negative remainder.
Yesterday I mailed Wolfram support about this, and the reply was (paraphrasing):

Our internal engineer team has concluded that the output of this question is correct.

Meaning there is no error in 1000%(-9)=-8
I'm confused.

Manumit
  • 203
  • I have read it all, maybe I missed something? most of the discussion revolves around -a mod b which gives -1000%9=8 – Manumit Sep 01 '15 at 12:13
  • Whoops! My mistake.... –  Sep 01 '15 at 12:17
  • 1
    Note: $-8 \pmod{-9}\equiv 1 \pmod {-9}$ – gammatester Sep 01 '15 at 12:18
  • 2
    I'll assume you want $a$ and $b$ to be integers, $b>0$. $a\bmod{-b}$ means whatever we agree it means. If we don't all agree, then it means different things to different people. Most people have no use for a negative modulus, so it's fine for them to say it doesn't mean anything. If I had to define it, I'd define it to be the same as $a\bmod b$. – Gerry Myerson Sep 01 '15 at 12:22
  • 1
    The result of mod-ing a negative number is not consistent among programming languages: https://en.wikipedia.org/wiki/Modulo_operation – peterwhy Sep 01 '15 at 12:43
  • Maple. For "77 mod (-25)" it returns "2" ... I don't know why you thought Maple did not do this. Of course the character "%" has a completely different meaning in Maple. – GEdgar Sep 01 '15 at 14:19

1 Answers1

4

This is a matter of convention. Usually the binary mod you write is "the remainder" (in a specified interval) when doing Euclidean division.

For integers (no assumption on positivity) $n,m$ with $m \neq 0$ I tend to define Euclidean division so that $n = q m +r$ with integers $q,r$ and $0 \le r \le |m|-1$, so the remainder is always positive.

Then $1000 \bmod -9$ makes perfect sense yet it would be $1$ not $-8$.

Others prefer different conventions, namely they take the remainder "between" $m$ and $0$ so it is positive (or zero) for positive $m$ and negative for negative (or zero) for negative $m$. This is what Wolfram|Alpha does.

Still others will make the sign of the remainder depend on the number $n$ to be divided.

Mainly, this is a consequence how one want integral division to behave: should the integral quotient be the floor, the truncation or still something else of the exact quotient. A nice overview is given on the Wikipedia page Modulo operation, as pointed out by peterwhy.

Personally I prefer the first as in a more general context modulo is considered with respect to ideals and the ideal generated by $b$ and $-b$ are the same, so I do not want to have different representatives of the classes.

But there are also good reasons for the other conventions; especially, it can be more reasonable in a computational context.

To say it is an invalid expression is in my opinion not a good idea. Somebody might not see a need to define or implement it, but there are perfectly valid ways to make sense of it.

quid
  • 42,135
  • I still dont understand why Wolfram outputs -8, and stands by it. Or maybe you explained that in 4 paragraph? – Manumit Sep 01 '15 at 13:28
  • 2
    @Manumit because $-8$ is between $0$ (inclusive) and $-9$ (exclusive). – peterwhy Sep 01 '15 at 13:39
  • 1
    @Manumit Longer answer, the reason behind could be how the quotient of integer division is defined. With normal division,$$1000/(-9) = -111.11\ldots$$ If the quotient of integer division is taken as the floored value, $q=\lfloor-111.11\ldots\rfloor = -112$, then the outcome of $\bmod$ has to satisfy $$\begin{align}1000 &= (-9)(-112) + r\r&=-8\end{align}$$ – peterwhy Sep 01 '15 at 13:56
  • 1
    @Manumit yes it was in that paragraph W|A uses a different convention than I. This should have been more clear. I expanded the answer a bit. The information given by peterwhy is really good. – quid Sep 01 '15 at 14:18