| advertise add site services publishers database health videos | ![]() | about toolbar stats live show health store more stuff JOIN/LOGIN |
Trinitas Hospital's Operation Safe, OPERATION SAFE, Elizabeth, Union trinitashospital.org | Operation Theatre Equipment, Operation Equipment Exporters, Operation... medicalsurgicals.com | Tactical Operations Products - Tactical Operations Flashlight - LED allegromedical.com |
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.
[edit] Remainder calculation for the modulo operation
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 q and the remainder r satisfy This means there are two possible choices for the remainder, one negative and the other positive, and there are also two possible choices for the quotient. Usually, in number theory, the positive remainder is always chosen, but programming languages choose depending on the language and the signs of a and n.[3] However, Pascal and Algol68 do not satisfy these conditions for negative divisors, and 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. Knuth[2] described floored division where the quotient is defined by the floor function q=floor(a/n) and the remainder r is Here the quotient rounds towards negative infinity and the remainder has the same sign as the divisor. Raymond T. Boute[3] 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,[4]
Common Lisp also defines round- and ceiling-division where the quotient is given by q=round(a/n), q=ceil(a/n). IEEE 754 defines a remainder function where the quotient is a/n rounded according to the round to nearest convention. [edit] Common pitfallsWhen the result of a modulo operation has the sign of the dividend, it can sometimes lead to surprising mistakes: For example, to test whether an integer is odd, one might be inclined to test whether 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 n (the dividend) is negative and odd, n % 2 returns -1, and the function returns false. One correct alternative is to test that it is not 0 (because remainder 0 is the same regardless of the signs): bool is_odd(int n) { return n % 2 != 0; } [edit] Modulo operation expressionSome 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
or
or equivalent, for environments lacking a mod() function
[edit] Performance issuesModulo operations might be implemented such that a division with a 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:
Examples (assuming x is an integer):
In devices and software that implement bitwise operations more efficiently than modulo, these alternative forms can result in faster calculations. Optimizing C compilers generally recognize expressions of the form In some compilers, the modulo operation is implemented as [edit] See also
[edit] Notes
[edit] References
[edit] External links
|
| ↑ top of page ↑ | about thumbshots |