Modulo
Given two integers, a and n, a mod n is the remainder on integer division of a by n. This is pronounced a modulo n.
Depending on the implementation, the remainder r is typically constrained to
- 0 ≤ |r| < |n|,
with a negative remainder only resulting when n < 0. This form is sometimes denoted separately as a Mod n (with a capital M in Mod) or a % n (a borrowing from the C programming language).
The difference in conventions is not very serious, in fact; it is reasonably thought of as reflecting the preference, for computational purposes, of a normal form over the underlying equivalence relation. This can be regarded mainly as a notational convention in this case, where there is a strict-sense normal form.
Some 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 "%" as a modulo operator, such as a % n.
a mod n 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.
An implementation of a modulo function that constrains the remainder set in the manner described above, as is found in the programming languages Perl and Python, can be described in terms of the floor function floor(z), the greatest integer less than or equal to z:
- mod(a,n) = a − (n × floor(a \x{00F7} n))
This definition allows for a and n to be typed as integers or rational numbers.
The expression a mod 0 is undefined in the majority of numerical systems, although some do define it to be a.