language-icon Old Web
English
Sign In

Modulo operation

In computing, the modulo operation finds the remainder after division of one number by another (called the modulus of the operation). q ∈ Z a = n q + r | r | < | n | {displaystyle {egin{aligned}q,&in mathbb {Z} \a,&=nq+r\|r|,&<|n|end{aligned}}}     (1)or equivalentlyBoute 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. In computing, the modulo operation finds the remainder after division of one number by another (called the modulus of the operation). Given two positive numbers, a and n, a modulo n (abbreviated as a mod n) is the remainder of the Euclidean division of a by n, where a is the dividend and n is the divisor. 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 leaves a remainder of 0; there is nothing to subtract from 9 after multiplying 3 times 3. (Doing the division with a calculator will not show the result referred to here by this operation; the quotient will be expressed as a decimal fraction.) Although typically performed with a and n both being integers, many computing systems allow other types of numeric operands. The range of numbers for an integer modulo of n is 0 to n − 1. (a mod 1 is always 0; a mod 0 is undefined, possibly resulting in a division by zero error in programming languages.) See modular arithmetic for an older and related convention applied in number theory. When either a or n is negative, the naive definition breaks down and programming languages differ in how these values are defined. 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 nonnegative integer which 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 q and the remainder r of a divided by n satisfy 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. Usually, in number theory, the positive remainder is always chosen, but programming languages choose depending on the language and the signs of a or n. Standard Pascal and ALGOL 68 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 n or a is negative. See the table for details. a modulo 0 is undefined in most systems, although some do define it as a.

[ "Algorithm", "Arithmetic", "Discrete mathematics", "Algebra" ]
Parent Topic
Child Topic
    No Parent Topic