3

As far I can calculate $-1 \bmod 2 \equiv -1$, but the software I am using (R) is telling that $-1 \bmod 2 \equiv +1$.

This is the R code:

-1%%2
[1]1

Which is correct?

alexwlchan
  • 2,060
time
  • 1,595
  • Is $-1+1\equiv 0\mod 2$? What about $-1-(+1)$ or $1-(-1)$? Note that in the usual introduction to modular arithmetic, the $a=bq +r$ takes $r\in[0,b-1]$. – abiessu Jan 03 '15 at 06:04

2 Answers2

7

There are two different but related definitions attached to $\pmod{n}$.

For the first, we think of $\pmod{n}$ as an equivalence relation that is dividing $\mathbb{Z}$ into $n$ equivalence classes, wherein $a \equiv b \pmod{n}$ whenever $n|(b-a)$. In this context, you are certainly correct! $-1 \equiv -1 \pmod{2}$ since $-1 - (-1) = 0$ and certainly $2|0$. Further, it is also correct to say that $-1 \equiv 1 \pmod{2}$ since $-1 - 1 = -2$ and $2|-2$.

In the other definition, we think of $\pmod{n}$ as a function $f:\mathbb{Z} \rightarrow \{0, 1, 2, \cdots, n-1\}$ wherein $a \equiv r \pmod{n}$ if $r$ is the remainder we get after dividing $a$ by $n$. It's worth noting that, with the first definition in mind, $a$ and $r$ fall into the same equivalence class.

Your software is using the second definition.

Kaj Hansen
  • 33,011
2

If you're referring to the equivalence relation, then $-1 \equiv 1 \pmod 2$, so both answers are the same.

If it's specifically the remainder operator you're curious about, then see this answer of mine.