Modulo


In computing and mathematics, the modulo operation returns the remainder or signed remainder of a division, after one number is divided by another, the latter being called the modulus of the operation.
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" 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 and both being integers, many computing systems now allow other types of numeric operands. The range of values for an integer modulo operation of is 0 to. mod 1 is always 0.
When exactly one of or 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. 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 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. In number theory, the positive remainder is always chosen, but in computing, programming languages choose depending on the language and the signs of or. Standard Pascal and ALGOL 68, for example, 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. Some systems leave modulo 0 undefined, though others define it as.
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.

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.
For environments lacking a similar function, any of the three definitions above can be used.

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

Or with the binary arithmetic:

bool is_odd

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.
Compiler optimizations may recognize expressions of the form where is a power of two and automatically implement them as, 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 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.
Optimizations for general constant-modulus operations also exist by calculating the division first using the constant-divisor optimization.

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. The properties involving multiplication, division, and exponentiation generally require that and are integers.
LanguageOperatorIntegerFloating-pointDefinition
ABAPEuclidean
ActionScriptTruncated
AdaFloored
AdaTruncated
ALGOL 68,,,, modEuclidean
AMPLTruncated
APL|Floored
AppleScriptTruncated
AutoLISPTruncated
AWKTruncated
BASICVaries by implementation
bcTruncated
CC++, Truncated
CC++ Truncated
CC++ Rounded
C#Truncated
C#Rounded
ClarionTruncated
CleanTruncated
ClojureFloored
ClojureTruncated
COBOLFloored
COBOLTruncated
CoffeeScriptTruncated
CoffeeScriptFloored
ColdFusion, Truncated
Common Intermediate Language Truncated
Common Intermediate Language
Common LispFloored
Common LispTruncated
Crystal, Floored
CrystalTruncated
CSSFloored
CSSTruncated
DTruncated
DartEuclidean
DartTruncated
EiffelTruncated
ElixirTruncated
ElixirFloored
ElmFloored
ElmTruncated
ErlangTruncated
ErlangTruncated
EuphoriaTruncated
EuphoriaFloored
F#Truncated
F#Rounded
FactorTruncated
FactorEuclidean
FileMakerFloored
ForthImplementation defined
ForthFloored
ForthTruncated
FortranTruncated
FortranFloored
FrinkFloored
Full BASICFloored
Full BASICTruncated
GLSLUndefined
GLSLFloored
GameMaker Studio , Truncated
GDScript Truncated
GDScript Euclidean
GDScript Truncated
GDScript Euclidean
GoTruncated
GoTruncated
GoEuclidean
GoTruncated
GroovyTruncated
HaskellFloored
HaskellTruncated
Haskell Floored
HaxeTruncated
HLSLUndefined
J|Floored
JavaTruncated
JavaFloored
JavaScriptTypeScriptTruncated
JuliaFloored
Julia, Truncated
Kotlin, Truncated
KotlinFloored
kshTruncated
kshTruncated
LabVIEWTruncated
LibreOfficeFloored
LogoFloored
LogoTruncated
Lua 5Floored
Lua 4Truncated
Liberty BASICTruncated
MathcadFloored
Maple, Euclidean
MapleRounded
MapleRounded
MathematicaFloored
MATLABFloored
MATLABTruncated
MaximaFloored
MaximaTruncated
Maya Embedded LanguageTruncated
Microsoft ExcelFloored
MinitabFloored
Modula-2Floored
Modula-2Truncated
MUMPSFloored
Netwide Assembler ,
Netwide Assembler Implementation-defined
NimTruncated
OberonFloored-like
Objective-CTruncated
Object Pascal, DelphiTruncated
OCamlTruncated
OCamlTruncated
OccamTruncated
Pascal Euclidean-like
PerlFloored
PerlTruncated
PHPTruncated
PHPTruncated
PIC BASIC ProTruncated
PL/IFloored
PowerShellTruncated
Programming Code Undefined
ProgressTruncated
Prolog Floored
Prolog Truncated
PureBasic, Truncated
PureScriptEuclidean
Pure DataTruncated
Pure DataFloored
PythonFloored
PythonTruncated
PythonRounded
Q#Truncated
RFloored
RacketFloored
RacketTruncated
RakuFloored
RealBasicTruncated
ReasonTruncated
RexxTruncated
RPGTruncated
Ruby, Floored
RubyTruncated
RustTruncated
RustEuclidean
SASTruncated
ScalaTruncated
SchemeFloored
SchemeTruncated
Scheme R6RSEuclidean
Scheme R6RSRounded
Scheme R6RSEuclidean
Scheme R6RSRounded
ScratchFloored
Seed7Floored
Seed7Truncated
SenseTalkFloored
SenseTalkTruncated
Truncated
SmalltalkFloored
SmalltalkTruncated
Snap!Floored
SpinFloored
SolidityTruncated
SQL Truncated
SQL Truncated
Standard MLFloored
Standard MLTruncated
Standard MLTruncated
StataEuclidean
SwiftTruncated
SwiftRounded
SwiftTruncated
TclFloored
TclTruncated
tcshTruncated
TorqueTruncated
TuringFloored
Verilog Truncated
VHDLFloored
VHDLTruncated
VimLTruncated
Visual BasicTruncated
WebAssembly,
WebAssembly, Truncated
x86 assemblyTruncated
XBase++Truncated
XBase++Floored
Zig, Truncated
ZigFloored
Z3 theorem prover, Euclidean

In addition, many computer systems provide a functionality, which produces the quotient and the remainder at the same time. Examples include the x86 architecture's instruction, the C programming language's function, and Python's function.