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 asor
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.- Identity:
- *.
- * for all positive integer values of.
- * If is a prime number which is not a divisor of, then, due to Fermat's little theorem.
- Inverse:
- *.
- * denotes the modular multiplicative inverse, which is defined if and only if and are relatively prime, which is the case when the left hand side is defined:.
- Distributive:
- *.
- *.
- Division : , when the right hand side is defined. Undefined otherwise.
- Inverse multiplication: .
In programming languages
Language | Operator | Result has same sign as |
ABAP | MOD | Nonnegative always |
ActionScript | % | Dividend |
Ada | mod | Divisor |
Ada | rem | Dividend |
ALGOL 68 | ÷× , mod | Nonnegative always |
AMPL | mod | Dividend |
APL |
| Divisor |
AppleScript | mod | Dividend |
AutoLISP |
| Dividend |
AWK | % | Dividend |
BASIC | Mod | Undefined |
bash | % | Dividend |
bc | % | Dividend |
C | % | Implementation-defined |
C | div | Dividend |
C++ | % | Implementation-defined |
C++ | div | Dividend |
C | % , div | Dividend |
C++ | % , div | Dividend |
C# | % | Dividend |
Clarion | % | Dividend |
Clean | rem | Dividend |
Clojure | mod | Divisor |
Clojure | rem | Dividend |
COBOL | FUNCTION MOD | Divisor |
CoffeeScript | % | Dividend |
CoffeeScript | %% | Divisor |
ColdFusion | % , MOD | Dividend |
Common Lisp | mod | Divisor |
Common Lisp | rem | Dividend |
Crystal | % | Dividend |
D | % | Dividend |
Dart | % | Nonnegative always |
Dart | remainder | Dividend |
Eiffel | \\ | Dividend |
Elixir | rem | Dividend |
Elm | modBy | Divisor |
Elm | remainderBy | Dividend |
Erlang | rem | Dividend |
Euphoria | mod | Divisor |
Euphoria | remainder | Dividend |
F# | % | Dividend |
Factor | mod | Dividend |
FileMaker | Mod | Divisor |
Forth | mod | implementation defined |
Forth | fm/mod | Divisor |
Forth | sm/rem | Dividend |
Fortran | mod | Dividend |
Fortran | modulo | Divisor |
Frink | mod | Divisor |
GameMaker Studio | mod , % | Dividend |
GDScript | % | Dividend |
Go | % | Dividend |
Groovy | % | Dividend |
Haskell | mod | Divisor |
Haskell | rem | Dividend |
Haxe | % | Dividend |
J |
| Divisor |
Java | % | Dividend |
Java | Math.floorMod | Divisor |
JavaScript | % | Dividend |
Julia | mod | Divisor |
Julia | % , rem | Dividend |
Kotlin | % , rem | Dividend |
ksh | % | Dividend |
LabVIEW | mod | Dividend |
LibreOffice | =MOD | Divisor |
Logo | MODULO | Divisor |
Logo | REMAINDER | Dividend |
Lua 5 | % | Divisor |
Lua 4 | mod | Divisor |
Liberty BASIC | MOD | Dividend |
Mathcad | mod | Divisor |
Maple | e mod m | Nonnegative always |
Mathematica | Mod | Divisor |
MATLAB | mod | Divisor |
MATLAB | rem | Dividend |
Maxima | mod | Divisor |
Maxima | remainder | Dividend |
Maya Embedded Language | % | Dividend |
Microsoft Excel | =MOD | Divisor |
Minitab | MOD | Divisor |
mksh | % | Dividend |
Modula-2 | MOD | Divisor |
Modula-2 | REM | Dividend |
MUMPS | # | Divisor |
Netwide Assembler | % , div | Modulo operator unsigned |
Netwide Assembler | %% | Modulo operator signed |
Nim | mod | Dividend |
Oberon | MOD | Divisor |
Objective-C | % | Dividend |
Object Pascal, Delphi | mod | Dividend |
OCaml | mod | Dividend |
Occam | \ | Dividend |
Pascal | mod | Nonnegative always |
Programming Code Advanced | \ | Undefined |
Perl | % | Divisor |
Phix | mod | Divisor |
Phix | remainder | Dividend |
PHP | % | Dividend |
PIC BASIC Pro | \\ | Dividend |
PL/I | mod | Divisor |
PowerShell | % | Dividend |
Programming Code | MATH.OP - 'MOD; ' | Undefined |
Progress | modulo | Dividend |
Prolog | mod | Divisor |
Prolog | rem | Dividend |
PureBasic | % , Mod | Dividend |
PureScript | `mod` | Divisor |
Python | % | Divisor |
Q# | % | Dividend |
R | %% | Divisor |
RealBasic | MOD | Dividend |
Reason | mod | Dividend |
Racket | modulo | Divisor |
Racket | remainder | Dividend |
Rexx | // | Dividend |
RPG | %REM | Dividend |
Ruby | % , modulo | Divisor |
Ruby | remainder | Dividend |
Rust | % | Dividend |
Rust | rem_euclid | Divisor |
SAS | MOD | Dividend |
Scala | % | Dividend |
Scheme | modulo | Divisor |
Scheme | remainder | Dividend |
Scheme R6RS | mod | Nonnegative always |
Scheme R6RS | mod0 | Nearest to zero |
Scratch | mod | Divisor |
Seed7 | mod | Divisor |
Seed7 | rem | Dividend |
SenseTalk | modulo | Divisor |
SenseTalk | rem | Dividend |
Shell | % | Dividend |
Smalltalk | \\ | Divisor |
Smalltalk | rem: | Dividend |
Snap! | mod | Divisor |
Spin | // | Divisor |
Solidity | % | Divisor |
SQL | mod | Dividend |
SQL | % | Dividend |
Standard ML | mod | Divisor |
Standard ML | Int.rem | Dividend |
Stata | mod | Nonnegative always |
Swift | % | Dividend |
Tcl | % | Divisor |
TypeScript | % | Dividend |
Torque | % | Dividend |
Turing | mod | Divisor |
Verilog | % | Dividend |
VHDL | mod | Divisor |
VHDL | rem | Dividend |
VimL | % | Dividend |
Visual Basic | Mod | Dividend |
WebAssembly | i32.rem_s , i64.rem_s | Dividend |
x86 assembly | IDIV | Dividend |
XBase++ | % | Dividend |
XBase++ | Mod | Divisor |
Z3 theorem prover | div , mod | Nonnegative 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
.