
Modulo operation

In computing, the modulo operation returns the remainder or signed remainder of a division, after one number is divided by another (called the "modulus" of the operation).
Given two positive numbers and , modulo (abbreviated as ) is the remainder of the Euclidean division of by , where is the dividend and is the divisor. The modulo operation is to be distinguished from the symbol , which refers to the modulus (or divisor) one is operating from.
For example, the expression "5 mod 2" would evaluate to 1, because 5 divided by 2 has a quotient of 2 and a remainder of 1, while "9 mod 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.
Although typically performed with and both being integers, many computing systems now allow other types of numeric operands. The range of numbers for an integer modulo of is 0 to inclusive ( mod 1 is always 0; is undefined, possibly resulting in a division by zero error in some programming languages). See modular arithmetic for an older and related convention applied in number theory.
When exactly one of or is negative, the naive definition breaks down, and programming languages differ in how these values are defined.
Variants of the definition.
In mathematics, the result of the modulo operation is an equivalence class, and any member of the class may be chosen as representative; however, the usual representative is the least positive residue, the smallest non-negative integer that belongs to that class (i.e., the remainder of the Euclidean division). However, other conventions are possible. Computers and calculators have various ways of storing and representing numbers; thus their definition of the modulo operation depends on the programming language or the underlying hardware.
In nearly all computing systems, the quotient and the remainder of divided by satisfy the following conditions:
However, this still leaves a sign ambiguity if the remainder is nonzero: two possible choices for the remainder occur, one negative and the other positive, and two possible choices for the quotient occur. In number theory, the positive remainder is always chosen, but in computing, programming languages choose depending on the language and the signs of or . Standard Pascal and ALGOL 68, for example, give a positive remainder (or 0) even for negative divisors, and some programming languages, such as C90, leave it to the implementation when either of or is negative (see the table under for details). modulo 0 is undefined in most systems, although some do define it as .
As described by Leijen,
However, truncated division satisfies the identity formula_1.
Notation.
Some calculators have a function button, and many programming languages have a similar function, expressed as , for example. Some also support expressions that use "%", "mod", or "Mod" as a modulo or remainder operator, such as or .
For environments lacking a similar function, any of the three definitions above can be used.
Common pitfalls.
When the result of a modulo operation has the sign of the dividend (truncating definition), it can lead to surprising mistakes.
For example, to test if an integer is odd, one might be inclined to test if the remainder by 2 is equal to 1:
bool is_odd(int n) {
 return n % 2 == 1;
But in a language where modulo has the sign of the dividend, that is incorrect, because when (the dividend) is negative and odd, mod 2 returns −1, and the function returns false.
One correct alternative is to test that the remainder is not 0 (because remainder 0 is the same regardless of the signs):
bool is_odd(int n) {
 return n % 2 != 0;
Another alternative is to use the fact that for any odd number, the remainder may be either 1 or −1:
bool is_odd(int n) {
 return n % 2 == 1 || n % 2 == -1;
Performance issues.
Modulo operations might be implemented such that a division with a remainder is calculated each time. For special cases, on some hardware, faster alternatives exist. For example, the modulo of powers of 2 can alternatively be expressed as a bitwise AND operation (assuming is a positive integer, or using a non-truncating definition):
Examples:
In devices and software that implement bitwise operations more efficiently than modulo, these alternative forms can result in faster calculations.
Compiler optimizations may recognize expressions of the form where is a power of two and automatically implement them as , allowing the programmer to write clearer code without compromising performance. This simple optimization is not possible for languages in which the result of the modulo operation has the sign of the dividend (including C), unless the dividend is of an unsigned integer type. This is because, if the dividend is negative, the modulo will be negative, whereas will always be positive. For these languages, the equivalence codice_2 has to be used instead, expressed using bitwise OR, NOT and AND operations.
Properties (identities).
Some modulo operations can be factored or expanded similarly to other mathematical operations. This may be useful in cryptography proofs, such as the Diffie–Hellman key exchange.
In programming languages.
In addition, many computer systems provide a functionality, which produces the quotient and the remainder at the same time. Examples include the x86 architecture's instruction, the C programming language's function, and Python's function.
Generalizations.
Modulo with offset.
Sometimes it is useful for the result of modulo to lie not between 0 and , but between some number and . In that case, is called an "offset." There does not seem to be a standard notation for this operation, so let us tentatively use . We thus have the following definition: just in case and . Clearly, the usual modulo operation corresponds to zero offset: . The operation of modulo with offset is related to the floor function as follows: 
The modulo with offset is implemented in Mathematica as .
Implementing other modulo definitions using truncation.
Despite the mathematic elegance of Kunth's floored division and Euclidean division, it is generally much more common to find a truncated division-based modulo in programming languages. Leijen provides the following algorithms for calculating the two divisions given a truncated integer division:
/* Euclidean and Floored divmod, in the style of C's ldiv() */
typedef struct {
 /* This structure is part of the C stdlib.h, but is reproduced here for clarity */
 long int quot;
 long int rem;
} ldiv_t;
/* Euclidean division */
inline ldiv_t ldivE(long numer, long denom) {
 /* The C99 and C++11 languages define both of these as truncating. */
 long q = numer / denom;
 long r = numer % denom;
 if (r < 0) {
 if (denom > 0) {
 q = q - 1;
 r = r + denom;
 } else {
 q = q + 1;
 r = r - denom;
 return (ldiv_t){.quot = q, .rem = r};
/* Floored division */
inline ldiv_t ldivF(long numer, long denom) {
 long q = numer / denom;
 long r = numer % denom;
 if ((r > 0 && denom < 0) || (r < 0 && denom > 0)) {
 q = q - 1;
 r = r + denom;
 return (ldiv_t){.quot = q, .rem = r};
Note that for both cases, the remainder can be calculated independently of the quotient, but not vice versa. The operations are combined here to save screen space, as the logical branches are the same.