Textbook
For an integer $a$ and a positive integer $m$, there are unique integers $q$ and $r$ with $a=qm+r$ and $0\le r<m$. The number $r$ is the remainder, written $a\bmod m$. Its required range makes the decomposition unique.
Since $17=3\cdot5+2$, it follows that $17\bmod5=2$. For a negative input, $-1=(-1)\cdot5+4$, so $-1\bmod5=4$. A programming languageβs remainder operator may use a different convention for negative inputs. Translating the mathematical expression into code requires checking that contract.
Uniqueness follows from the range restriction. If $a=qm+r=q'm+r'$ with valid remainders, then $(q-q')m=r'-r$. The right side has absolute value below $m$, while the left is a multiple of $m$, so both must be zero. Existence follows by taking $q=\lfloor a/m\rfloor$ and $r=a-qm$: the floor inequalities directly give $0\le r<m$.
Definitions and further reading: Mathematics for Computer Science.
In plain language
Packing 17 objects into full boxes of capacity 5 leaves three boxes and two loose objects; the remainder describes the loose part. A negative input uses the same chosen range from zero to $m-1$, like a dial with five marks. A backward step from zero reaches the last mark, and each complete turn returns to the previous position. The box analogy ends there: a remainder records a position without keeping the accounts for every completed lap.
Example
The example compares division of 17 by 5 with input β1. The last step rejects remainder β1 because of its range, even though using quotient zero would preserve the equality.
Remainders: Reading a mod m
Full groups
Removing three groups of 5 from 17 leaves 2.
Step 1 of 3: Full groups
Read all steps
Full groups
Removing three groups of 5 from 17 leaves 2.
- State
- 17current15current2current
Negative input
With q = β1, the remainder falls in the required range.
- State
- β1currentβ5current4current
Range check
With modulus 5, only 0, 1, 2, 3, and 4 are allowed.
- State
- β1excluded0included1included2included3included4included
For input β1, the pair $q=0$, $r=-1$ passes the equality check but fails the range check. The pair $q=-1$, $r=4$ passes both and is therefore selected. Verification reconstructs the input as $qm+r$, then separately compares the remainder with zero and the modulus. Passing just one of these checks does not establish a valid Euclidean remainder.
Related concepts: Floor.