Modulo operation


In computing, the modulo operation finds the remainder or signed remainder after a division of one number by another.
Given two positive numbers, and, modulo is the remainder of the Euclidean division of by, where is the dividend and 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.
Although typically performed with and both being integers, many computing systems allow other types of numeric operands. The range of numbers for an integer modulo of is 0 to inclusive. See modular arithmetic for an older and related convention applied in number theory.
When either or is negative, the naive 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 and the remainder of divided by 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 or. Standard Pascal and ALGOL 68 give a positive remainder even for negative divisors, and some programming languages, such as C90, leave it to the implementation when either of or is negative. See the table for details. modulo 0 is undefined in most systems, although some do define it as.
As described by Leijen,
However, Boute concentrates on the properties of the modulo operation itself and does not rate the fact that the truncated division shows the symmetry and, which is similar to the ordinary division. As neither floor division nor Euclidean division offer this symmetry, Boute's judgement is at least incomplete.

Common pitfalls

When the result of a modulo operation has the sign of the dividend, it can lead to surprising mistakes.
For example, to test if an integer is odd, one might be inclined to test if the remainder by 2 is equal to 1:

bool is_odd

But in a language where modulo has the sign of the dividend, that is incorrect, because when is negative and odd, mod 2 returns −1, and the function returns false.
One correct alternative is to test that the remainder is not 0 :

bool is_odd

Another is to use the fact that, for any odd number, the remainder may be either 1 or −1:

bool is_odd

Notation

Some calculators have a function button, and many programming languages have a similar function, expressed as, for example. Some also support expressions that use "%", "mod", or "Mod" as a modulo or remainder operator, such as
or
or equivalent, for environments lacking a function

Performance issues

Modulo operations might be implemented such that a division with a remainder is calculated each time. For special cases, on some hardware, faster alternatives exist. For example, the modulo of powers of 2 can alternatively be expressed as a bitwise AND operation:
Examples :
In devices and software that implement bitwise operations more efficiently than modulo, these alternative forms can result in faster calculations.
Optimizing compilers may recognize expressions of the form expression % constant where constant is a power of two and automatically implement them as expression & , allowing the programmer to write clearer code without compromising performance. This simple optimization is not possible for languages in which the result of the modulo operation has the sign of the dividend, unless the dividend is of an unsigned integer type. This is because, if the dividend is negative, the modulo will be negative, whereas expression & will always be positive. For these languages, the equivalence x % 2n x < 0 ? x | ~ : x & has to be used instead, expressed using bitwise OR, NOT and AND operations.

Properties (identities)

Some modulo operations can be factored or expanded similarly to other mathematical operations. This may be useful in cryptography proofs, such as the Diffie–Hellman key exchange.
LanguageOperatorResult has same sign as
ABAPMODNonnegative always
ActionScript%Dividend
AdamodDivisor
AdaremDividend
ALGOL 68÷×, modNonnegative always
AMPLmodDividend
APL|Divisor
AppleScriptmodDividend
AutoLISPDividend
AWK%Dividend
BASICModUndefined
bash%Dividend
bc%Dividend
C %Implementation-defined
C divDividend
C++ %Implementation-defined
C++ divDividend
C %, divDividend
C++ %, divDividend
C#%Dividend
Clarion%Dividend
CleanremDividend
ClojuremodDivisor
ClojureremDividend
COBOLFUNCTION MODDivisor
CoffeeScript%Dividend
CoffeeScript%%Divisor
ColdFusion%, MODDividend
Common LispmodDivisor
Common LispremDividend
Crystal%Dividend
D%Dividend
Dart%Nonnegative always
DartremainderDividend
Eiffel\\Dividend
ElixirremDividend
ElmmodByDivisor
ElmremainderByDividend
ErlangremDividend
EuphoriamodDivisor
EuphoriaremainderDividend
F#%Dividend
FactormodDividend
FileMakerModDivisor
Forthmodimplementation defined
Forthfm/modDivisor
Forthsm/remDividend
FortranmodDividend
FortranmoduloDivisor
FrinkmodDivisor
GameMaker Studio mod, %Dividend
GDScript%Dividend
Go%Dividend
Groovy%Dividend
HaskellmodDivisor
HaskellremDividend
Haxe%Dividend
J|Divisor
Java%Dividend
JavaMath.floorModDivisor
JavaScript%Dividend
JuliamodDivisor
Julia%, remDividend
Kotlin%, remDividend
ksh%Dividend
LabVIEWmodDividend
LibreOffice=MODDivisor
LogoMODULODivisor
LogoREMAINDERDividend
Lua 5%Divisor
Lua 4modDivisor
Liberty BASICMODDividend
MathcadmodDivisor
Maplee mod mNonnegative always
MathematicaModDivisor
MATLABmodDivisor
MATLABremDividend
MaximamodDivisor
MaximaremainderDividend
Maya Embedded Language%Dividend
Microsoft Excel=MODDivisor
MinitabMODDivisor
mksh%Dividend
Modula-2MODDivisor
Modula-2REMDividend
MUMPS#Divisor
Netwide Assembler %, divModulo operator unsigned
Netwide Assembler %%Modulo operator signed
NimmodDividend
OberonMODDivisor
Objective-C%Dividend
Object Pascal, DelphimodDividend
OCamlmodDividend
Occam\Dividend
Pascal modNonnegative always
Programming Code Advanced \Undefined
Perl%Divisor
PhixmodDivisor
PhixremainderDividend
PHP%Dividend
PIC BASIC Pro\\Dividend
PL/ImodDivisor
PowerShell%Dividend
Programming Code MATH.OP - 'MOD; 'Undefined
ProgressmoduloDividend
Prolog modDivisor
Prolog remDividend
PureBasic%, ModDividend
PureScript`mod`Divisor
Python%Divisor
Q#%Dividend
R%%Divisor
RealBasicMODDividend
ReasonmodDividend
RacketmoduloDivisor
RacketremainderDividend
Rexx//Dividend
RPG%REMDividend
Ruby%, moduloDivisor
RubyremainderDividend
Rust%Dividend
Rustrem_euclidDivisor
SASMODDividend
Scala%Dividend
SchememoduloDivisor
SchemeremainderDividend
Scheme R6RSmodNonnegative always
Scheme R6RSmod0Nearest to zero
ScratchmodDivisor
Seed7modDivisor
Seed7remDividend
SenseTalkmoduloDivisor
SenseTalkremDividend
Shell%Dividend
Smalltalk\\Divisor
Smalltalkrem:Dividend
Snap!modDivisor
Spin//Divisor
Solidity%Divisor
SQL modDividend
SQL %Dividend
Standard MLmodDivisor
Standard MLInt.remDividend
StatamodNonnegative always
Swift%Dividend
Tcl%Divisor
TypeScript%Dividend
Torque%Dividend
TuringmodDivisor
Verilog %Dividend
VHDLmodDivisor
VHDLremDividend
VimL%Dividend
Visual BasicModDividend
WebAssemblyi32.rem_s, i64.rem_sDividend
x86 assemblyIDIVDividend
XBase++%Dividend
XBase++ModDivisor
Z3 theorem proverdiv, modNonnegative always

Generalizations

Modulo with offset

Sometimes it is useful for the result of modulo to lie not between 0 and, but between some number and. In that case, is called an offset. There does not seem to be a standard notation for this operation, so let us tentatively use mod. We thus have the following definition: mod just in case ≤ ≤ and mod mod. Clearly, the usual modulo operation corresponds to zero offset: mod mod0. The operation of modulo with offset is related to the floor function as follows:
mod = mod for all integers ; thus, this is true also in the particular case when = ; but that means that, which is what we wanted to prove. It remains to be shown that ≤ ≤. Let and be the integers such that with 0 ≤ ≤
The modulo with offset mod is implemented in Mathematica as Mod.