Modulo operation
From Wikipedia, the free encyclopedia
In computing, the modulo operation finds the remainder of division of one number by another.
Given two numbers, a (the dividend) and n (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.
See modular arithmetic for an older and related convention applied in number theory.
Contents |
[edit] Remainder calculation for the modulo operation
Language | Operator | Result has the same sign as |
---|---|---|
Ada | mod | Divisor |
rem | Dividend | |
C (1989) | % | Not defined |
C (1999) | % | Dividend |
ColdFusion | MOD | Dividend |
Fortran | mod | Dividend |
modulo | Divisor | |
Java | % | Dividend |
JavaScript | % | Dividend |
MATLAB | mod | Divisor |
rem | Dividend | |
MySQL | MOD % |
Dividend |
Objective Caml | mod | Not defined |
Pascal (Delphi) | mod | Dividend |
Perl | % | Not defined1 |
PHP | % | Dividend |
Python | % | Divisor |
Ruby | % | Divisor |
Verilog (2001) | % | Dividend |
VHDL | mod | Divisor |
rem | Dividend |
There are various ways of defining a remainder, and computers and calculators have various ways of storing and representing numbers, so what exactly constitutes the result of a modulo operation depends on the programming language and/or the underlying hardware.
In nearly all computing systems, the quotient resulting from the division is constrained to the set of integers, and the remainder r is typically constrained by either or
, the choice between the two dependent from the signs of a and n and the programming language being used.2 (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. If the definition is consistent with the division algorithm, then n = 0 implies , which is a contradiction (i.e., the usual remainder does not exist in this case).
The remainder can be calculated by using equations, in terms of other functions. Differences may arise according to the scope of the variables, which in common implementations is broader than in the definition just given. One useful equation for calculating the remainder r is
where is the floor function of x. See e.g. [1], [2], [3].
Raymond T. Boute[1] analyzed several definitions of integer division and modulo, and he introduces the “Euclidean” definition. Let q be the integer quotient of a and n, then:
Two corollaries are that
As described by Leijen,[2]
- Boute argues that Euclidean division is superior to the other ones in terms of regularity and useful mathematical properties, although floored division, promoted by Knuth, is also a good definition. Despite its widespread use, truncated division is shown to be inferior to the other definitions.
[edit] Modulo operation expression
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 operator, such as
a % n
or
a mod n
both of which are read as "a modulo n" when spoken aloud.
[edit] Performance issues
Modulo operations might be implemented such that division with remainder is calculated each time. For real-time computer software this can be slower than alternatives, for special cases. For example, the modulus of powers of 2 can alternatively be expressed as a bitwise AND operation:
x % 2^n == x & (2^n - 1)
Further examples:
x % 2 == x & 1
x % 4 == x & 3
x % 8 == x & 7
In devices and software that implement bitwise operations more efficiently than modulo, this can result in faster calculations.
[edit] See also
- Modulo — many uses of the word "modulo", all of which grew out of Carl F. Gauss's introduction of modular arithmetic in 1801.
- Modular arithmetic
[edit] Notes
- Note 1: The semantics of the modulo operator in Perl are defined to be those of the modulo operator of the C compiler that was used to compile the Perl interpreter itself.
- Note 2: Mathematically, these two choices are but two of the infinite number of choices available for the inequality satisfied by a remainder.
[edit] References
- ^ Boute, Raymond T. (April 1992). "The Euclidean definition of the functions div and mod". ACM Transactions on Programming Languages and Systems (TOPLAS) 14 (2): 127 - 144.
- ^ Leijen, Daan (December 3, 2001). Division and Modulus for Computer Scientists (PDF). Retrieved on 2006-08-27.