Given two numbers, (the dividend) and (the divisor), a modulo n (abbreviated as a mod n) is the remainder, on division of a by n. For instance, the expression "7 mod 3" would evaluate to 1, while "9 mod 3" would evaluate to 0. Although typically performed with a and n both being integers, many computing systems allow other types of numeric operands.
|Language||Operator||Result has the same sign as|
|C (ISO 1990)||%||Not defined|
|C (ISO 1999)||%||Dividend|
|GML (Game Maker)||mod||Dividend|
|Objective Caml||mod||Not defined|
|PL/I||mod||Divisor (ANSI PL/I)|
|Prolog (ISO 1995)||mod||Divisor|
In nearly all computing systems, the quotient and the remainder satisfy
Pascal and Algol68 do not satisfy these for negative divisors. Some programming languages, such as C89, don't even define a result if either of n or a is negative. See the table for details. a modulo 0 is undefined in the majority of systems, although some do define it to be a.
Many implementations use truncated division where the quotient is defined by truncation q = trunc(a/n) and the remainder by r=a-n q. With this definition the quotient is rounded towards zero and the remainder has the same sign as the dividend.
Raymond T. Boute introduces the Euclidean definition which is consistent with the division algorithm. Let q be the integer quotient of a and n, then:
Two corollaries are that
As described by Leijen,
Common Lisp also defines round- and ceiling-division where the quotient is given by , . IEEE 754 defines a remainder function where the quotient is rounded according to the round to nearest convention.
Some calculators have a mod() function button, and many programming languages have a mod() function or similar, expressed as mod(a, n), for example. Some also support expressions that use "%", "mod", or "Mod" as a modulo or remainder operator, such as
a % n
a mod n.
Modulo operations might be implemented such that division with remainder is calculated each time. For special cases, there are faster alternatives on some hardware. For example, the modulo of powers of 2 can alternatively be expressed as a bitwise AND operation:
x % 2n == x & (2n - 1).
Examples (assuming x is an integer):
x % 2 == x & 1
x % 4 == x & 3
x % 8 == x & 7.
In devices and software that implement bitwise operations more efficiently than modulo, these alternative forms can result in faster calculations.
In the C programming language, compiling with heavy speed optimizations will typically (depending on compiler and hardware) automatically convert modulo operations to bitwise AND in the assembly file.
In some compilers, the modulo operation is implemented as
mod(a, n) = a - n * floor(a / n). When performing both modulo and division on the same numbers, one can get the same result somewhat more efficiently by avoiding the actual modulo operator, and using the formula above on the result, avoiding an additional division operation.
WIPO ASSIGNS PATENT TO SAMSUNG ELECTRONICS FOR "MODULO OPERATION METHOD AND APPARATUS FOR SAME" (SOUTH KOREAN INVENTORS)
Dec 21, 2010; GENEVA, Dec. 21 -- Publication No. WO/2010/143892 was published on Dec. 16. Title of the invention: "MODULO OPERATION METHOD AND...
Publication No. WO/2009/084026 Published on July 9, Indian Inventor Develops High-Speed Modulo Operation Apparatus
Jul 15, 2009; GENEVA, July 15 -- Chandra Mohan Umapathy, India, has developed a system, method and apparatus for high-speed modulo operation....
US Patent Issued to Adobe Systems on April 3 for "Work Load Distribution Among Server Processes" (California Inventors)
Apr 14, 2012; ALEXANDRIA, Va., April 12 -- United States Patent no. 8,150,970, issued on April 3, was assigned to Adobe Systems Inc. (San Jose,...