Lably
Skip to content

Modulo

Computational operation

  • 13 min read
  • 7 chapters

Learn it on Lably

Given two positive numbers a and n, a modulo n (often 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" evaluates 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 9 divided by 3 has a quotient of 3 and a remainder of 0.

Although typically performed with a and n both being integers, many computing systems now allow other types of numeric operands. The range of values for an integer modulo operation of n is 0 to n − 1. a mod 1 is always 0, assuming a is an integer.

When exactly one of a or n is negative, the basic definition breaks down, and programming languages differ in how these values are defined.

Variants of the definition

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 non-negative integer that 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≠0 satisfy the following conditions:

This still leaves a sign ambiguity if the remainder is non-zero: two possible choices for the remainder occur, one negative and the other positive; that choice determines which of the two consecutive quotients must be used to satisfy equation (1). In number theory, the positive remainder is always chosen, but in computing, programming languages choose depending on the language and the signs of a or n. Standard Pascal and ALGOL 68, for example, 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 under § In programming languages for details). Some systems leave a modulo 0 undefined, though others define it as a.

  • Many implementations use truncated division, for which the quotient is defined by q=trunc⁡(an) where trunc is the integral part function (rounding toward zero), i.e. the truncation to zero significant digits. Thus according to equation (1), the remainder has the same sign as the dividend a so can take 2|n| − 1 values: r=a−ntrunc⁡(an)

  • Donald Knuth promotes floored division, for which the quotient is defined by q=⌊an⌋ where ⌊⌋ is the floor function (rounding down). Thus according to equation (1), the remainder has the same sign as the divisor n: r=a−n⌊an⌋

  • Raymond T. Boute promotes Euclidean division, for which the non-negative remainder r∈{0,1,2...} is defined by r:=a−nqsuchthat0≤r<|n|. (Emphasis added.) Under this definition, we can say the following about the quotient q: q=a−rn∈Z=sgn(n)⋅a−r|n|=sgn(n)⋅(a|n|−r|n|)=sgn(n)⋅⌊a|n|⌋ where sgn is the sign function, ⌊⌋ is the floor function (rounding down), and a|n|∈Q, r|n|∈Q are rational numbers.

    Equivalently, one may instead define the quotient q∈Z as follows: q:=sgn⁡(n)⌊a|n|⌋={⌊an⌋if n>0⌈an⌉if n<0 where ⌈⌉ is the ceiling function (rounding up). Thus according to equation (1), the remainder r is non-negative: r=a−nq=a−|n|⌊a|n|⌋

  • Common Lisp and IEEE 754 use rounded division, for which the quotient is defined by q=round⁡(an) where round is the round function (rounding half to even). Thus according to equation (1), the remainder falls between −n2 and n2, and its sign depends on which side of zero it falls to be within these boundaries: r=a−nround⁡(an)

  • Common Lisp also uses ceiling division, for which the quotient is defined by q=⌈an⌉ where ⌈⌉ is the ceiling function (rounding up). Thus according to equation (1), the remainder has the opposite sign of that of the divisor: r=a−n⌈an⌉

If both the dividend and divisor are positive, then the truncated, floored, and Euclidean definitions agree. If the dividend is positive and the divisor is negative, then the truncated and Euclidean definitions agree. If the dividend is negative and the divisor is positive, then the floored and Euclidean definitions agree. If both the dividend and divisor are negative, then the truncated and floored definitions agree.

However, truncated division satisfies the identity (−a)/b=−(a/b)=a/(−b).

Sign in to keep reading

6 more chapters are free with a Lably account.

More in Basics