Modulo
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 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). a modulo 0 is undefined in most systems, although some do define it as a.

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.

As described by Leijen,

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.

However, truncated division satisfies the identity (− a) / b = − (a / b) = a / (− b) {\displaystyle ({-a})/b={-(a/b)}=a/({-b})}.
punctuation:no_comma