5

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?

Bill Dubuque
  • 272,048
rivanov
  • 159
  • it's because modulo n is always between 0 and said n. – Dleep May 16 '15 at 02:56
  • 1
    Does this answer your question? Modulo of a negative number – amWhy Feb 01 '20 at 01:14
  • @ALL This is not a dupe of the above, because this asks about negative $\rm\color{#c00}{moduli}$ but the other question asks about negative $\rm\color{#c00}{remainders}$. This makes a significant difference. Further some of the answers here are more general (handling both cases, and then some). Any dupe link should go the other way, since only here is the general case handled. – Bill Dubuque Feb 01 '20 at 02:58

5 Answers5

5

$\, 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,\,$ Alternative to remainders we can use as normal forms any complete set of residues $\!\bmod n,\,$ i.e. any set of integers such that every integer is congruent to exactly one element of the set (i.e. a set of irredundant representatives for every congruence class), e.g. $\!\bmod 5\,$ we can use least magnitude reps $\,0,\pm1, \pm2\ $ (here $\,4\bmod 5 = -1)$ or, more generally, any $5$ consecutive integers.

Normal form reps allow us to reduce testing congruence to testing equality of normal forms (similar to testing fraction equivalence by testing equality of their least term reductions), i.e.

$$\begin{align} a\equiv b\!\!\!\pmod{\!n}&\iff a\bmod n = b\bmod n\\[.3em] {\rm like}\quad\ \frac{a}c \,\equiv\, \frac{b}d &\iff \ell\left[\frac{a}c\right]\, =\, \ell\left[\frac{b}d\right],\ \ \ell = \text{least_terms}\end{align}\qquad$$

Recall the congruence relation $\,a\equiv b\pmod{\!n}\,$ means $\,n\mid a-b,\,$ i.e. $\,n\,$ divides $\,a-b.\,$ Take care not to confuse this $\!\bmod\!$ relation with the above operation (see here for more on such).

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 further discussion of such conventions in many places, e.g. on Wikipedia and D. Leijen, Division and Modulus for Computer Scientists., and R. Boute, The Euclidean Definition of the Functions div and mod, which includes the following figures

enter image description here enter image description here

Bill Dubuque
  • 272,048
  • Re: "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}$"

    Can it be normalized to a set not including $0$? Eg the set ${1,2}$ for $\pmod 2$. Can it be normalized to a set that is not contiguous? Eg the set ${-2,0,1,2,4}$ for $\pmod 2$?

    And is (Euclidean) integer division algorithm referring to floor division?

    – joseville Jan 22 '22 at 23:17
  • 1
    @joseville Yes to the residue set questions. Euclidean division refers to the algorithm for division with remainder well-known since grade school, where the remainder $,r = a\bmod n,$ satisfies $,0\le r < n.\ \ $ – Bill Dubuque Jan 23 '22 at 06:10
1

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

1

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

1

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

Zev Chonoles
  • 129,973
  • Yes I initially was computing 9%7 on google and then decided to try 9%-7 to which I got the bewildering response -5. – rivanov May 16 '15 at 03:08
  • @rivanov Just to be clear $x \bmod -7$ is exactly the same as $x \bmod 7$. So if you're ever confused by the negative modulus, just switch to positive -- there is no difference. –  May 16 '15 at 03:17
0

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.

Jyrki Lahtonen
  • 133,153