article

In computing, the modulo operation finds the remainder of division of one number by another.

Given two numbers, a and n, a modulo n (abbreviated as a mod n) is the remainder, on division of a by n. For instance, the expression "8 mod 3" would evaluate to 2, 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.

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

0 \leq |r| < |n|,\,

with a negative remainder only resulting when n < 0. Some programming languages, such as ANSI C, don't define a result if either of n or a is negative. 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 0 \leq r < 0, which is a contradiction (i.e., the usual remainder does not exist in this case).

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

r = a - n \left\lfloor {a \over n} \right\rfloor

where \lfloor x\rfloor is the floor function of x. See e.g. *.

Modulo operation expression


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 "%", "mod", or "Mod" as a modulo operator, such as

a % n

or

a mod n

both of which are read as "a modulo n" when spoken aloud.

See also


Computer arithmetic

Zbytek po dělení | Modulo (Rest) | Modulo (informatique) | Modulus (wiskunde) | Modulo

 

This article is licensed under the GNU Free Documentation License. It uses material from the "Modulo operation".

Home Pageartsbusinesscomputersgameshealthhospitalshomekids & teensnewsphysiciansrecreationreferenceregionalscienceshoppingsocietysportsworld