|
"fmod" redirects here. For the computer audio library, see FMOD.
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.
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 resulting from the division is constrained to the set of integers, and the remainder r is typically constrained by either 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 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 Raymond T. Boute[2] 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,[3]
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
Performance issuesModulo 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:
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. 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 See also
Notes
References
|
This article is from Wikipedia. All text is available under the terms of the GNU Free Documentation License.
Mercedes Car
This site monitored by SitePinger.net