Modulo operation

In computing, the modulo operation returns the remainder or signed remainder of a division, after one number is divided by another.
Given two positive numbers a and n, a modulo n (abbreviated as a \bmod n) is the remainder of the division of a by n, where a is the dividend and n is the divisor.
For example, the expression
* 5 \bmod 2 would evaluate to 1, because 5 divided by 2 has a quotient of 2 and a remainder of 1, while
* 9 \bmod 3 would evaluate to 0, because the division of 9 by 3 has a quotient of 3 and a remainder of 0; there is nothing to subtract from 9 after multiplying 3 times 3.
When exactly one of a or n is negative, the naive definition breaks down, and programming languages differ in how these values are defined.
== Definitions ==
There are two main variants of how to define the remainder r of a quotient q given by \textstyle \frac{a}{n} that also include negative values.
=== Floored divison ===
Many implementations use floored division where the quotient is defined by the floor function \textstyle q = \left\lfloor\frac{a}{n}\right\rfloor and thus the remainder would have the same sign as the divisor n. Due to the floor function, the quotient is always rounded downwards, even if it is already negative.
:r = a - n \left\lfloor\frac{a}{n}\right\rfloor
Floored division is a consensus in mathematics, especially number theory. It is also used for the standard modulo operator mod or % in programming languages like Python, Lua, Ada, and in Microsoft Excel.
Java's % operator uses truncated division instead, but its function Math.floorMod can be used if floored division is needed.
=== Truncated divison ===
Other implementations use truncated division, where the quotient is defined by truncation \textstyle q = \operatorname{trunc}\left(\frac{a}{n}\right) and thus the remainder would have the same sign as the dividend a. The quotient is rounded towards zero: equal to the first integer in the direction of zero from the exact rational quotient.
:r = a - n \operatorname{trunc}\left(\frac{a}{n}\right)
Truncated division is used for the standard modulo operator % in Java, languages of the C family and PHP (and thus also WikiMedia which runs PHP to parse Wikitext).
It is also supported by math.fmod in Python and rem in Ada.
=== Comparison ===
The different handling of negative values for dividend or divisor can lead to unexpected results, especially for portations from one platform to another. A wiki editor figuring out game behavior in Excel sheets (floored division) will receive different results when implementing the Excel calculations in a wiki template (truncated division). To simulate a \bmod n with floored division in wikitext, (a mod n + n) mod n has to be used instead.
The table below shows the results of the standard modulo operators for the example a \bmod 4 with different values for a:
Division 	Ada 	Excel 	Wikitext	-7	-6	-5	-4	-3	-2	-1	0	1	2	3	4	5	6	7
Floored,"mathematical"	 	MOD(a;4)	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	
Truncated,"symmetric" 	a rem 4	 	a mod 4 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	
Category:Arithmetic