# Modulo

In computing, the modulo operation returns the remainder or signed remainder of a division, after one number is divided by another, called the modulus of the operation.
Given two positive numbers a and n, a modulo n (often abbreviated as a mod n) is the remainder of the Euclidean division of a by n, where a is the dividend and n 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 a and n both being integers, many computing systems now allow other types of numeric operands. The range of values for an integer modulo operation of n is 0 to n − 1. a mod 1 is always 0.
When exactly one of a or n 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 (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 q and the remainder r of a divided by n 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 (1). In number theory, the positive remainder is always chosen, but in computing, programming languages choose depending on the language and the signs of a or n. Standard Pascal and ALGOL 68, for example, give a positive remainder (or 0) even for negative divisors, and some programming languages, such as C90, leave it to the implementation when either of n or a is negative (see the table under § In programming languages for details). Some systems leave a modulo 0 undefined, though others define it as a.
* Quotient (q) and remainder (r) as functions of dividend (a), using truncated division Many implementations use truncated division, for which the quotient is defined by where $\operatorname {trunc}$ is the integral part function (rounding toward zero), i.e. the truncation to zero significant digits. Thus according to equation (1), the remainder has the same sign as the dividend a so can take 2|n| − 1 values:
    $q=\operatorname {trunc} \left({\frac {a}{n}}\right)$
    $r=a-n\operatorname {trunc} \left({\frac {a}{n}}\right)$
* Quotient and remainder using floored division Donald Knuth promotes floored division, for which the quotient is defined by where $\lfloor \,\rfloor$ is the floor function (rounding down). Thus according to equation (1), the remainder has the same sign as the divisor n:
    $q=\left\lfloor {\frac {a}{n}}\right\rfloor$
    $r=a-n\left\lfloor {\frac {a}{n}}\right\rfloor$
* Quotient and remainder using Euclidean division Raymond T. Boute promotes Euclidean division, for which the quotient is defined by where sgn is the sign function, $\lfloor \,\rfloor$ is the floor function (rounding down), and $\lceil \,\rceil$ is the ceiling function (rounding up). Thus according to equation (1), the remainder is non negative:
    $q=\operatorname {sgn}(n)\left\lfloor {\frac {a}{\left|n\right|}}\right\rfloor ={\begin{cases}\left\lfloor {\frac {a}{n}}\right\rfloor &{\text{if }}n>0\\\left\lceil {\frac {a}{n}}\right\rceil &{\text{if }}n<0\\\end{cases}}$
    $r=a-|n|\left\lfloor {\frac {a}{\left|n\right|}}\right\rfloor$
* Quotient and remainder using rounded division Common Lisp and IEEE 754 use rounded division, for which the quotient is defined by where round is the round function (rounding half to even). Thus according to equation (1), the remainder falls between $-{\frac {n}{2}}$ and ${\frac {n}{2}}$, and its sign depends on which side of zero it falls to be within these boundaries:
    $q=\operatorname {round} \left({\frac {a}{n}}\right)$
    $r=a-n\operatorname {round} \left({\frac {a}{n}}\right)$
* Quotient and remainder using ceiling division Common Lisp also uses ceiling division, for which the quotient is defined by where ⌈⌉ is the ceiling function (rounding up). Thus according to equation (1), the remainder has the opposite sign of that of the divisor:
    $q=\left\lceil {\frac {a}{n}}\right\rceil$
    $r=a-n\left\lceil {\frac {a}{n}}\right\rceil$
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.
As described by Leijen,
   Boute argues that Euclidean division is superior to the other ones in terms of regularity and useful mathematical properties, although floored division, promoted by Knuth, is also a good definition. Despite its widespread use, truncated division is shown to be inferior to the other definitions.
However, truncated division satisfies the identity $({-a})/b={-(a/b)}=a/({-b})$.

## Notation

Some calculators have a mod function button, and many programming languages have a similar function, expressed as mod(a, n), for example. Some also support expressions that use "%", "mod", or "Mod" as a modulo or remainder operator, such as a % n or a mod n.
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 (truncated definition), 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:
But in a language where modulo has the sign of the dividend, that is incorrect, because when n (the dividend) is negative and odd, n mod 2 returns −1, and the function returns false.
One correct alternative is to test that the remainder is not 0 (because remainder 0 is the same regardless of the signs):
Or with the binary arithmetic:

## 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 (assuming x is a positive integer, or using a non-truncating definition):
    x % 2$^n$ == x & (2$^n$ - 1)
Examples:
    x % 2 == x & 1
    x % 4 == x & 3
    x % 8 == x & 7
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 expression % constant where constant is a power of two and automatically implement them as expression & (constant-1), 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 (including C), unless the dividend is of an unsigned integer type. This is because, if the dividend is negative, the modulo will be negative, whereas expression & (constant-1) will always be positive. For these languages, the equivalence x % 2$^n$ == x < 0 ? x | ~(2$^n$ - 1) : x & (2$^n$ - 1) 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 a and n are integers.
* Identity:
** (a mod n) mod n = a mod n.
** n$^x$ mod n = 0 for all positive integer values of x.
** If p is a prime number which is not a divisor of b, then ab$^{p−1}$ mod p = a mod p, due to Fermat's little theorem.
* Inverse:
** [(−a mod n) + (a mod n)] mod n = 0.
** b$^{−1}$ mod n denotes the modular multiplicative inverse, which is defined if and only if b and n are relatively prime, which is the case when the left hand side is defined: [(b$^{−1}$ mod n)(b mod n)] mod n = 1.
* Distributive:
** (a + b) mod n = [(a mod n) + (b mod n)] mod n.
** ab mod n = [(a mod n)(b mod n)] mod n.
* Division (definition): ⁠a/b⁠ mod n = [(a mod n)(b$^{−1}$ mod n)] mod n, when the right hand side is defined (that is when b and n are coprime), and undefined otherwise.
* Inverse multiplication: [(ab mod n)(b$^{−1}$ mod n)] mod n = a mod n.

## In programming languages

Modulo operators in various programming languages :
| Language | Operator | Integer | Floating-point | Definition |
| ABAP | MOD | Yes | Yes | Euclidean |
| ActionScript | % | Yes | No | Truncated |
| Ada | mod | Yes | No | Floored |
| Ada | rem | Yes | No | Truncated |
| ALGOL 68 | ÷×, mod | Yes | No | Euclidean |
| AMPL | mod | Yes | No | Truncated |
| APL | | | Yes | Yes | Floored |
| AppleScript | mod | Yes | No | Truncated |
| AutoLISP | (rem d n) | Yes | No | Truncated |
| AWK | % | Yes | No | Truncated |
| bash | % | Yes | No | Truncated |
| BASIC | Mod | Yes | No | Varies by implementation |
| bc | % | Yes | No | Truncated |
| C C++ | %, div | Yes | No | Truncated |
| C C++ | fmod (C) std::fmod (C++) | No | Yes | Truncated |
| C C++ | remainder (C) std::remainder (C++) | No | Yes | Rounded |
| C# | % | Yes | Yes | Truncated |
| C# | Math.IEEERemainder | No | Yes | Rounded |
| Clarion | % | Yes | No | Truncated |
| Clean | rem | Yes | No | Truncated |
| Clojure | mod | Yes | No | Floored |
| Clojure | rem | Yes | No | Truncated |
| COBOL | FUNCTION MOD | Yes | No | Floored |
| COBOL | FUNCTION REM | Yes | Yes | Truncated |
| CoffeeScript | % | Yes | No | Truncated |
| CoffeeScript | %% | Yes | No | Floored |
| ColdFusion | %, MOD | Yes | No | Truncated |
| Common Intermediate Language | rem (signed) | Yes | Yes | Truncated |
| Common Intermediate Language | rem.un (unsigned) | Yes | No | — |
| Common Lisp | mod | Yes | Yes | Floored |
| Common Lisp | rem | Yes | Yes | Truncated |
| Crystal | %, modulo | Yes | Yes | Floored |
| Crystal | remainder | Yes | Yes | Truncated |
| CSS | mod | Yes | Yes | Floored |
| CSS | rem | Yes | Yes | Truncated |
| D | % | Yes | Yes | Truncated |
| Dart | % | Yes | Yes | Euclidean |
| Dart | remainder | Yes | Yes | Truncated |
| Eiffel | \\ | Yes | No | Truncated |
| Elixir | rem/2 | Yes | No | Truncated |
| Elixir | Integer.mod/2 | Yes | No | Floored |
| Elm | modBy | Yes | No | Floored |
| Elm | remainderBy | Yes | No | Truncated |
| Erlang | rem | Yes | No | Truncated |
| Erlang | math:fmod/2 | No | Yes | Truncated (same as C) |
| Euphoria | mod | Yes | No | Floored |
| Euphoria | remainder | Yes | No | Truncated |
| F# | % | Yes | Yes | Truncated |
| F# | Math.IEEERemainder | No | Yes | Rounded |
| Factor | mod | Yes | No | Truncated |
| FileMaker | Mod | Yes | No | Floored |
| Forth | mod | Yes | No | Implementation defined |
| Forth | fm/mod | Yes | No | Floored |
| Forth | sm/rem | Yes | No | Truncated |
| Fortran | mod | Yes | Yes | Truncated |
| Fortran | modulo | Yes | Yes | Floored |
| Frink | mod | Yes | No | Floored |
| Full BASIC | MOD | Yes | Yes | Floored |
| Full BASIC | REMAINDER | Yes | Yes | Truncated |
| GLSL | % | Yes | No | Undefined |
| GLSL | mod | No | Yes | Floored |
| GameMaker Studio (GML) | mod, % | Yes | No | Truncated |
| GDScript (Godot) | % | Yes | No | Truncated |
| GDScript (Godot) | fmod | No | Yes | Truncated |
| GDScript (Godot) | posmod | Yes | No | Euclidean |
| GDScript (Godot) | fposmod | No | Yes | Euclidean |
| Go | % | Yes | No | Truncated |
| Go | math.Mod | No | Yes | Truncated |
| Go | big.Int.Mod | Yes | No | Euclidean |
| Go | big.Int.Rem | Yes | No | Truncated |
| Groovy | % | Yes | No | Truncated |
| Haskell | mod | Yes | No | Floored |
| Haskell | rem | Yes | No | Truncated |
| Haskell | Data.Fixed.mod' (GHC) | No | Yes | Floored |
| Haxe | % | Yes | No | Truncated |
| HLSL | % | Yes | Yes | Undefined |
| J | | | Yes | No | Floored |
| Java | % | Yes | Yes | Truncated |
| Java | Math.floorMod | Yes | No | Floored |
| JavaScript TypeScript | % | Yes | Yes | Truncated |
| Julia | mod | Yes | Yes | Floored |
| Julia | %, rem | Yes | Yes | Truncated |
| Kotlin | %, rem | Yes | Yes | Truncated |
| Kotlin | mod | Yes | Yes | Floored |
| ksh | % | Yes | No | Truncated (same as POSIX sh) |
| ksh | fmod | No | Yes | Truncated |
| LabVIEW | mod | Yes | Yes | Truncated |
| LibreOffice | =MOD | Yes | No | Floored |
| Logo | MODULO | Yes | No | Floored |
| Logo | REMAINDER | Yes | No | Truncated |
| Lua 5 | % | Yes | Yes | Floored |
| Lua 4 | mod(x,y) | Yes | Yes | Truncated |
| Liberty BASIC | MOD | Yes | No | Truncated |
| Mathcad | mod(x,y) | Yes | No | Floored |
| Maple | e mod m (by default), modp(e, m) | Yes | No | Euclidean |
| Maple | mods(e, m) | Yes | No | Rounded |
| Maple | frem(e, m) | Yes | Yes | Rounded |
| Mathematica | Mod[a, b] | Yes | No | Floored |
| MATLAB | mod | Yes | No | Floored |
| MATLAB | rem | Yes | No | Truncated |
| Maxima | mod | Yes | No | Floored |
| Maxima | remainder | Yes | No | Truncated |
| Maya Embedded Language | % | Yes | No | Truncated |
| Microsoft Excel | =MOD | Yes | Yes | Floored |
| Minitab | MOD | Yes | No | Floored |
| Modula-2 | MOD | Yes | No | Floored |
| Modula-2 | REM | Yes | No | Truncated |
| MUMPS | # | Yes | No | Floored |
| Netwide Assembler (NASM, NASMX) | %, div (unsigned) | Yes | No | — |
| Netwide Assembler (NASM, NASMX) | %% (signed) | Yes | No | Implementation-defined |
| Nim | mod | Yes | No | Truncated |
| Oberon | MOD | Yes | No | Floored-like |
| Objective-C | % | Yes | No | Truncated (same as C99) |
| Object Pascal, Delphi | mod | Yes | No | Truncated |
| OCaml | mod | Yes | No | Truncated |
| OCaml | mod_float | No | Yes | Truncated |
| Occam | \ | Yes | No | Truncated |
| Pascal (ISO-7185 and -10206) | mod | Yes | No | Euclidean-like |
| Perl | % | Yes | No | Floored |
| Perl | POSIX::fmod | No | Yes | Truncated |
| Phix | mod | Yes | No | Floored |
| Phix | remainder | Yes | No | Truncated |
| PHP | % | Yes | No | Truncated |
| PHP | fmod | No | Yes | Truncated |
| PIC BASIC Pro | \\ | Yes | No | Truncated |
| PL/I | mod | Yes | No | Floored (ANSI PL/I) |
| PowerShell | % | Yes | No | Truncated |
| Programming Code (PRC) | MATH.OP - 'MOD; (\)' | Yes | No | Undefined |
| Progress | modulo | Yes | No | Truncated |
| Prolog (ISO 1995) | mod | Yes | No | Floored |
| Prolog (ISO 1995) | rem | Yes | No | Truncated |
| PureBasic | %, Mod(x,y) | Yes | No | Truncated |
| PureScript | `mod` | Yes | No | Euclidean |
| Pure Data | % | Yes | No | Truncated (same as C) |
| Pure Data | mod | Yes | No | Floored |
| Python | % | Yes | Yes | Floored |
| Python | math.fmod | No | Yes | Truncated |
| Python | math.remainder | No | Yes | Rounded |
| Q# | % | Yes | No | Truncated |
| R | %% | Yes | Yes | Floored |
| Racket | modulo | Yes | No | Floored |
| Racket | remainder | Yes | No | Truncated |
| Raku | % | No | Yes | Floored |
| RealBasic | MOD | Yes | No | Truncated |
| Reason | mod | Yes | No | Truncated |
| Rexx | // | Yes | Yes | Truncated |
| RPG | %REM | Yes | No | Truncated |
| Ruby | %, modulo | Yes | Yes | Floored |
| Ruby | remainder | Yes | Yes | Truncated |
| Rust | % | Yes | Yes | Truncated |
| Rust | rem_euclid | Yes | Yes | Euclidean |
| SAS | MOD | Yes | No | Truncated |
| Scala | % | Yes | Yes | Truncated |
| Scheme | modulo | Yes | No | Floored |
| Scheme | remainder | Yes | No | Truncated |
| Scheme R$^6$RS | mod | Yes | No | Euclidean |
| Scheme R$^6$RS | mod0 | Yes | No | Rounded |
| Scheme R$^6$RS | flmod | No | Yes | Euclidean |
| Scheme R$^6$RS | flmod0 | No | Yes | Rounded |
| Scratch | mod | Yes | Yes | Floored |
| Seed7 | mod | Yes | Yes | Floored |
| Seed7 | rem | Yes | Yes | Truncated |
| SenseTalk | modulo | Yes | No | Floored |
| SenseTalk | rem | Yes | No | Truncated |
| sh (POSIX) (includes bash, mksh, &c.) | % | Yes | No | Truncated (same as C) |
| Smalltalk | \\ | Yes | No | Floored |
| Smalltalk | rem: | Yes | No | Truncated |
| Snap! | mod | Yes | No | Floored |
| Spin | // | Yes | No | Floored |
| Solidity | % | Yes | No | Truncated |
| SQL (SQL:1999) | mod(x,y) | Yes | No | Truncated |
| SQL (SQL:2011) | % | Yes | No | Truncated |
| Standard ML | mod | Yes | No | Floored |
| Standard ML | Int.rem | Yes | No | Truncated |
| Standard ML | Real.rem | No | Yes | Truncated |
| Stata | mod(x,y) | Yes | No | Euclidean |
| Swift | % | Yes | No | Truncated |
| Swift | remainder(dividingBy:) | No | Yes | Rounded |
| Swift | truncatingRemainder(dividingBy:) | No | Yes | Truncated |
| Tcl | % | Yes | No | Floored |
| Tcl | fmod | No | Yes | Truncated (as C) |
| tcsh | % | Yes | No | Truncated |
| Torque | % | Yes | No | Truncated |
| Turing | mod | Yes | No | Floored |
| Verilog (2001) | % | Yes | No | Truncated |
| VHDL | mod | Yes | No | Floored |
| VHDL | rem | Yes | No | Truncated |
| VimL | % | Yes | No | Truncated |
| Visual Basic | Mod | Yes | No | Truncated |
| WebAssembly | i32.rem_u, i64.rem_u (unsigned) | Yes | No | — |
| WebAssembly | i32.rem_s, i64.rem_s (signed) | Yes | No | Truncated |
| x86 assembly | IDIV | Yes | No | Truncated |
| XBase++ | % | Yes | Yes | Truncated |
| XBase++ | Mod | Yes | Yes | Floored |
| Zig | %, @mod, @rem | Yes | Yes | Truncated |
| Z3 theorem prover | div, mod | Yes | No | Euclidean |
In addition, many computer systems provide a divmod functionality, which produces the quotient and the remainder at the same time. Examples include the x86 architecture's IDIV instruction, the C programming language's div function, and Python's divmod function.

## Generalizations
### Modulo with offset

Sometimes it is useful for the result of a modulo n to lie not between 0 and n − 1, but between some number d and d + n − 1. In that case, d is called an offset and d = 1 is particularly common.
There does not seem to be a standard notation for this operation, so let us tentatively use a mod$_d$ n. We thus have the following definition: x = a mod$_d$ n just in case d ≤ x ≤ d + n − 1 and x mod n = a mod n. Clearly, the usual modulo operation corresponds to zero offset: a mod n = a mod$_0$ n.
The operation of modulo with offset is related to the floor function as follows:
       $a\operatorname {mod} _{d}n=a-n\left\lfloor {\frac {a-d}{n}}\right\rfloor .$
To see this, let x=a−n⌊ a−d/n ⌋{\textstyle x=a-n\left\lfloor {\frac {a-d}{n}}\right\rfloor }. We first show that x mod n = a mod n. It is in general true that (a + bn) mod n = a mod n for all integers b; thus, this is true also in the particular case when b=−⌊ a−d/n ⌋{\textstyle b=-\!\left\lfloor {\frac {a-d}{n}}\right\rfloor }; but that means that xmodn=(a−n⌊ a−d/n ⌋)modn=amodn{\textstyle x{\bmod {n}}=\left(a-n\left\lfloor {\frac {a-d}{n}}\right\rfloor \right)\!{\bmod {n}}=a{\bmod {n}}}, which is what we wanted to prove. It remains to be shown that d ≤ x ≤ d + n − 1. Let k and r be the integers such that a − d = kn + r with 0 ≤ r ≤ n − 1 (see Euclidean division). Then ⌊ a−d/n ⌋=k{\textstyle \left\lfloor {\frac {a-d}{n}}\right\rfloor =k}, thus x=a−n⌊ a−d/n ⌋=a−nk=d+r{\textstyle x=a-n\left\lfloor {\frac {a-d}{n}}\right\rfloor =a-nk=d+r}. Now take 0 ≤ r ≤ n − 1 and add d to both sides, obtaining d ≤ d + r ≤ d + n − 1. But we've seen that x = d + r, so we are done.
The modulo with offset a mod$_d$ n is implemented in Mathematica as Mod[a, n, d] .

### Implementing other modulo definitions using truncation

Despite the mathematical elegance of Knuth's floored division and Euclidean division, it is generally much more common to find a truncated division-based modulo in programming languages. Leijen provides the following algorithms for calculating the two divisions given a truncated integer division:
For both cases, the remainder can be calculated independently of the quotient, but not vice versa. The operations are combined here to save screen space, as the logical branches are the same.