modulo
{{About|the binary operation mod({{mvar|a,n}})|the (mod {{mvar|n}}) notation|Modular arithmetic|other uses}}
{{short description|Computational operation}}
In computing and mathematics, 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 {{math|a}} and {{math|n}}, {{math|a}} modulo {{math|n}} (often abbreviated as {{math|a mod n}}) is the remainder of the Euclidean division of {{math|a}} by {{math|n}}, where {{math|a}} is the dividend and {{math|n}} is the divisor.{{Cite web|last=Weisstein|first=Eric W.|title=Congruence|url=https://mathworld.wolfram.com/Congruence.html|access-date=2020-08-27|website=Wolfram MathWorld |language=en}}
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 {{math|a}} and {{math|n}} both being integers, many computing systems now allow other types of numeric operands. The range of values for an integer modulo operation of {{math|n}} is 0 to {{math|n − 1}}. {{math|a}} mod 1 is always 0.
When exactly one of {{math|a}} or {{math|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).{{Cite web|last=Caldwell|first=Chris|title=residue|url=https://primes.utm.edu/glossary/page.php?sort=Residue|access-date=August 27, 2020|website=Prime Glossary}} 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 {{math|q}} and the remainder {{math|r}} of {{math|a}} divided by {{math|n}} satisfy the following conditions:
{{NumBlk|::|
&q \in \mathbb{Z} \\
&a = n q + r \\
&|r| < |n|
\end{align}|{{EquationRef|1}}}}
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 {{math|a}} or {{math|n}}.{{efn|Mathematically, these two choices are but two of the infinite number of choices available for the inequality satisfied by a remainder.}} 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 {{math|n}} or {{math|a}} is negative (see the table under {{Section link||In programming languages}} for details). Some systems leave {{math|a}} modulo 0 undefined, though others define it as {{math|a}}.
{{bulleted list
Many implementations use truncated division, for which the quotient is defined by
:
where is the integral part function (rounding toward zero), i.e. the truncation to zero significant digits.
Thus according to equation ({{EquationNote|1}}), the remainder has the same sign as the dividend {{var|a}} so can take {{math|1=2{{!}}n{{!}} − 1}} values:
:
Donald Knuth{{cite book|first=Donald. E. |last=Knuth |title=The Art of Computer Programming |url=https://archive.org/details/artofcomputerpro0003knut |url-access=registration |publisher=Addison-Wesley |year=1972}} promotes floored division, for which the quotient is defined by
:
where is the floor function (rounding down).
Thus according to equation ({{EquationNote|1}}), the remainder has the same sign as the divisor {{var|n}}:
:
Raymond T. Boute{{cite journal |last = Boute |first = Raymond T. |title = The Euclidean definition of the functions div and mod |journal = ACM Transactions on Programming Languages and Systems |volume = 14 |issue = 2 |pages = 127–144 |publisher = ACM Press (New York, NY, USA) |date = April 1992 |url = http://portal.acm.org/citation.cfm?id=128862&coll=portal&dl=ACM |doi = 10.1145/128861.128862| hdl = 1854/LU-314490 |s2cid = 8321674 |hdl-access = free}} promotes Euclidean division, for which the quotient is defined by
:
\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}
where {{math|sgn}} is the sign function, is the floor function (rounding down), and is the ceiling function (rounding up).
Thus according to equation ({{EquationNote|1}}), the remainder is non negative:
:
Common Lisp and IEEE 754 use rounded division, for which the quotient is defined by
:
where {{math|round}} is the round function (rounding half to even).
Thus according to equation ({{EquationNote|1}}), the remainder falls between and , and its sign depends on which side of zero it falls to be within these boundaries:
:
Common Lisp also uses ceiling division, for which the quotient is defined by
:
where ⌈⌉ is the ceiling function (rounding up).
Thus according to equation ({{EquationNote|1}}), the remainder has the opposite sign of that of the divisor:
:
}}
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,
{{Quote|text=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.|sign=Daan Leijen|source=Division and Modulus for Computer Scientists{{cite web
| last = Leijen
| first = Daan
| title = Division and Modulus for Computer Scientists
| website = Microsoft
| date = December 3, 2001
| url = https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/divmodnote-letter.pdf
| access-date =2014-12-25}}}}
However, truncated division satisfies the identity .{{cite web |last1=Peterson |first1=Doctor |title=Mod Function and Negative Numbers |url=http://mathforum.org/library/drmath/view/52343.html |website=Math Forum - Ask Dr. Math |access-date=22 October 2019 |date=5 July 2001|archive-url=https://web.archive.org/web/20191022160922/http://mathforum.org/library/drmath/view/52343.html |archive-date=2019-10-22 }}{{Cite web |title=Ada 83 LRM, Sec 4.5: Operators and Expression Evaluation |url=http://archive.adaic.com/standards/83lrm/html/lrm-04-05.html#4.5.5 |access-date=2025-03-03 |website=archive.adaic.com}}
Notation
{{About|the binary mod operation|the (mod m) notation|congruence relation|section=yes}}
Some calculators have a {{math|mod()}} function button, and many programming languages have a similar function, expressed as {{math|mod(a, n)}}, for example. Some also support expressions that use "%", "mod", or "Mod" as a modulo or remainder operator, such as {{code|a % n}} or {{code|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:
bool is_odd(int n) {
return n % 2 == 1;
}
But in a language where modulo has the sign of the dividend, that is incorrect, because when {{math|n}} (the dividend) is negative and odd, {{math|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):
bool is_odd(int n) {
return n % 2 != 0;
}
Or with the binary arithmetic:
bool is_odd(int n) {
return n & 1;
}
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 {{math|x}} is a positive integer, or using a non-truncating definition):
:x % 2n == x & (2n - 1)
Examples:
:{{code|1=x % 2 == x & 1}}
:{{code|1=x % 4 == x & 3}}
:{{code|1=x % 8 == x & 7}}
In devices and software that implement bitwise operations more efficiently than modulo, these alternative forms can result in faster calculations.{{cite web |first= Adam |last= Horvath |url= http://blog.teamleadnet.com/2012/07/faster-division-and-modulo-operation.html |title= Faster division and modulo operation - the power of two |date= July 5, 2012}}
Compiler optimizations may recognize expressions of the form {{code|expression % constant}} where {{code|constant}} is a power of two and automatically implement them as {{code|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 {{code|expression & (constant-1)}} will always be positive. For these languages, the equivalence x % 2n == x < 0 ? x | ~(2n - 1) : x & (2n - 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)
{{see also|Modular arithmetic#Properties}}
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 {{math|a}} and {{math|n}} are integers.
- Identity:
- {{math|(a mod n) mod n {{=}} a mod n}}.
- {{math|n{{sup|x}} mod n {{=}} 0}} for all positive integer values of {{math|x}}.
- If {{math|p}} is a prime number which is not a divisor of {{math|b}}, then {{math|ab{{i sup|p−1}} mod p {{=}} a mod p}}, due to Fermat's little theorem.
- Inverse:
- {{math|[(−a mod n) + (a mod n)] mod n {{=}} 0}}.
- {{math|b{{sup|−1}} mod n}} denotes the modular multiplicative inverse, which is defined if and only if {{math|b}} and {{math|n}} are relatively prime, which is the case when the left hand side is defined: {{math|[(b{{sup|−1}} mod n)(b mod n)] mod n {{=}} 1}}.
- Distributive:
- {{math|(a + b) mod n {{=}} [(a mod n) + (b mod n)] mod n}}.
- {{math|ab mod n {{=}} [(a mod n)(b mod n)] mod n}}.
- Division (definition): {{math|{{sfrac|a|b}} mod n {{=}} [(a mod n)(b{{sup|−1}} mod n)] mod n}}, when the right hand side is defined (that is when {{math|b}} and {{math|n}} are coprime), and undefined otherwise.
- Inverse multiplication: {{math|[(ab mod n)(b{{sup|−1}} mod n)] mod n {{=}} a mod n}}.
In programming languages
class="wikitable sortable"
|+ Modulo operators in various programming languages |
scope="col" | Language
! scope="col" | Operator ! scope="col" | Integer ! scope="col" | Floating-point ! abbr="Sign" scope="col" | Definition |
---|
ABAP
| {{code|MOD}} | {{yes}} | {{yes}} | Euclidean |
ActionScript
| {{code|%}} | {{yes}} | {{no}} | Truncated |
rowspan="2" | Ada
| {{code|mod}} | {{yes}} | {{no}} | Floored{{Cite book|title=ISO/IEC 8652:2012 - Information technology — Programming languages — Ada|publisher=ISO, IEC|year=2012|at=sec. 4.5.5 Multiplying Operators}} |
{{code|rem}}
| {{yes}} | {{no}} |
ALGOL 68
| {{code|÷×}}, {{code|mod}} | {{yes}} | {{no}} | Euclidean |
AMPL
| {{code|mod}} | {{yes}} | {{no}} | Truncated |
APL
| | {{yes}} | {{yes}} | Floored |
AppleScript
| {{code|mod}} | {{yes}} | {{no}} | Truncated |
AutoLISP
| {{code|(rem d n)}} | {{yes}} | {{no}} | Truncated |
AWK
| {{code|%}} | {{yes}} | {{no}} | Truncated |
bash
| {{code|%}} | {{yes}} | {{no}} | Truncated |
BASIC
| {{code|Mod}} | {{yes}} | {{no}} | Varies by implementation |
bc
| {{code|%}} | {{yes}} | {{no}} | Truncated |
rowspan="3" | C{{break}}C++
| {{code|%}}, {{code|div}} | {{yes}} | {{no}} | Truncated{{efn|name=c|C99 and C++11 define the behavior of {{code|%}} to be truncated.{{cite web |title=C99 specification (ISO/IEC 9899:TC2) |url=http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf |access-date=16 August 2018 |at=sec. 6.5.5 Multiplicative operators |date=2005-05-06}} The standards before then leave the behavior implementation-defined.{{Cite book|year=2003|title=ISO/IEC 14882:2003: Programming languages – C++|publisher=International Organization for Standardization (ISO), International Electrotechnical Commission (IEC)|at=sec. 5.6.4|quote=the binary % operator yields the remainder from the division of the first expression by the second. .... If both operands are nonnegative then the remainder is nonnegative; if not, the sign of the remainder is implementation-defined}}}} |
{{code|fmod}} (C){{break}}{{code|std::fmod}} (C++)
| {{no}} | {{yes}} | Truncated{{Cite book|title=ISO/IEC 9899:1990: Programming languages – C |publisher=ISO, IEC |year=1990 |at=sec. 7.5.6.4 |quote=The {{math|fmod}} function returns the value {{math|x - i * y}}, for some integer {{math|i}} such that, if {{math|y}} is nonzero, the result has the same sign as {{math|x}} and magnitude less than the magnitude of {{math|y}}.}} |
{{code|remainder}} (C){{break}}{{code|std::remainder}} (C++)
| {{no}} | {{yes}} | Rounded |
rowspan="2" | C#
| {{code|%}} | {{yes}} | {{yes}} | Truncated |
{{code|Math.IEEERemainder}}
| {{no}} | {{yes}} |
Clarion
| {{code|%}} | {{yes}} | {{no}} | Truncated |
Clean
| {{code|rem}} | {{yes}} | {{no}} | Truncated |
rowspan="2" | Clojure
| {{code|mod}} | {{yes}} | {{no}} |
{{code|rem}}
| {{yes}} | {{no}} |
rowspan="2" | COBOL
| {{code|FUNCTION MOD}} | {{yes}} | {{no}} | Floored{{cite book|title=ISO/IEC 1989:2023 – Programming language COBOL|author=ISO/IEC JTC 1/SC 22/WG 4|publisher=ISO|date=January 2023|url=https://www.iso.org/standard/74527.html|url-access=subscription}} |
{{code|FUNCTION REM}}
| {{yes}} | {{yes}} |
rowspan="2" | CoffeeScript
| {{code|%}} | {{yes}} | {{no}} | Truncated |
{{code|%%}}
| {{yes}} | {{no}} | Floored[http://coffeescript.org/#operators CoffeeScript operators] |
ColdFusion
| {{code|%}}, {{code|MOD}} | {{yes}} | {{no}} | Truncated |
rowspan="2" | Common Intermediate Language
| {{code|rem}} (signed) | {{yes}} | {{yes}} | Truncated{{cite book|last=ISO/IEC JTC 1/SC 22|date=February 2012|title=ISO/IEC 23271:2012 — Information technology — Common Language Infrastructure (CLI)|url=https://www.iso.org/standard/58046.html|publisher=ISO|at=§§ III.3.55–56}} |
{{code|rem.un}} (unsigned)
| {{yes}} | {{no}} | {{N/A}} |
rowspan="2" | Common Lisp
| {{code|mod}} | {{yes}} | {{yes}} | Floored |
{{code|rem}}
| {{yes}} | {{yes}} | Truncated |
rowspan="2" | Crystal
| {{code|%}}, {{code|modulo}} | {{yes}} | {{yes}} | Floored |
{{code|remainder}}
| {{yes}} | {{yes}} | Truncated |
rowspan="2"| CSS
| {{code|mod()}} | {{yes}} | {{yes}} |
{{code|rem()}}
| {{yes}} | {{yes}} |
D
| {{code|%}} | {{yes}} | {{yes}} |
rowspan="2" | Dart
| {{code|%}} | {{yes}} | {{yes}} |
{{code|remainder()}}
| {{yes}} | {{yes}} |
Eiffel
| {{code|\\}} | {{yes}} | {{no}} | Truncated |
rowspan="2" | Elixir
| {{code|rem/2}} | {{yes}} | {{no}} |
{{code|Integer.mod/2}}
| {{yes}} | {{no}} |
rowspan="2" | Elm
| {{code|modBy}} | {{yes}} | {{no}} |
{{code|remainderBy}}
| {{yes}} | {{no}} |
rowspan="2" | Erlang
| {{code|rem}} | {{yes}} | {{no}} | Truncated |
{{code|math:fmod/2}}
| {{no}} | {{yes}} | Truncated (same as C){{Cite web|title=Erlang -- math|url=https://erlang.org/doc/man/math.html#fmod-2|access-date=2021-06-01|website=erlang.org}} |
rowspan="2" | Euphoria
| {{code|mod}} | {{yes}} | {{no}} | Floored |
{{code|remainder}}
| {{yes}} | {{no}} | Truncated |
rowspan="2" | F#
| {{code|%}} | {{yes}} | {{yes}} | Truncated |
{{code|Math.IEEERemainder}}
| {{no}} | {{yes}} |
Factor
| {{code|mod}} | {{yes}} | {{no}} | Truncated |
FileMaker
| {{code|Mod}} | {{yes}} | {{no}} | Floored |
rowspan="3" | Forth
| {{code|mod}} | {{yes}} | {{no}} | Implementation defined |
{{code|fm/mod}}
| {{yes}} | {{no}} | Floored |
{{code|sm/rem}}
| {{yes}} | {{no}} | Truncated |
rowspan="2" | Fortran
| {{code|mod}} | {{yes}} | {{yes}} | Truncated |
{{code|modulo}}
| {{yes}} | {{yes}} | Floored |
Frink
| {{code|mod}} | {{yes}} | {{no}} | Floored |
rowspan="2" | Full BASIC
| {{code|MOD}} | {{yes}} | {{yes}} | Floored{{cite book|last=ANSI|title=Programming Languages — Full BASIC|url=https://archive.org/details/federalinformat6821nati_0|date=28 January 1987|publisher=American National Standards Institute|location=New York|at=§ 5.4.4|quote=X modulo Y, i.e., X-Y*INT(X/Y).}} |
{{code|REMAINDER}}
| {{yes}} | {{yes}} | Truncated{{cite book|last=ANSI|title=Programming Languages — Full BASIC|url=https://archive.org/details/federalinformat6821nati_0|date=28 January 1987|publisher=American National Standards Institute|location=New York|at=§ 5.4.4|quote="The remainder function, i.e., X-Y*IP(X/Y)."}} |
rowspan="2" | GLSL
| {{code|%}} | {{yes}} | {{no}} |
{{code|mod}}
| {{no}} | {{yes}} |
GameMaker Studio (GML)
| {{code|mod}}, {{code|%}} | {{yes}} | {{no}} | Truncated |
rowspan="4" | GDScript (Godot)
| {{code|%}} | {{yes}} | {{no}} | Truncated |
{{code|fmod}}
| {{no}} | {{yes}} | Truncated |
{{code|posmod}}
| {{yes}} | {{no}} | Euclidean |
{{code|fposmod}}
| {{no}} | {{yes}} | Euclidean |
rowspan="4" | Go
| {{code|%}} | {{yes}} | {{no}} |
{{code|math.Mod}}
| {{no}} | {{yes}} |
{{code|big.Int.Mod}}
| {{yes}} | {{no}} |
{{code|big.Int.Rem}}
| {{yes}} | {{no}} |
Groovy
| {{code|%}} | {{yes}} | {{no}} | Truncated |
rowspan="3" | Haskell
| {{code|mod}} | {{yes}} | {{no}} |
{{code|rem}}
| {{yes}} | {{no}} |
{{code|Data.Fixed.mod'}} (GHC)
| {{no}} | {{yes}} | Floored |
Haxe
| {{code|%}} | {{yes}} | {{no}} | Truncated |
HLSL
| {{code|%}} | {{yes}} | {{yes}} | Undefined{{cite web |url=https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-operators#additive-and-multiplicative-operators |title=Operators |author= |date=30 June 2021 |publisher=Microsoft |access-date=2021-07-19 |quote=The % operator is defined only in cases where either both sides are positive or both sides are negative. Unlike C, it also operates on floating-point data types, as well as integers.}} |
J
| | {{yes}} | {{no}} | Floored |
rowspan="2" | Java
| {{code|%}} | {{yes}} | {{yes}} | Truncated |
{{code|Math.floorMod}}
| {{yes}} | {{no}} | Floored |
JavaScript{{break}}TypeScript
| {{code|%}} | {{yes}} | {{yes}} | Truncated |
rowspan="2" | Julia
| {{code|mod}} | {{yes}} | {{yes}} |
{{code|%}}, {{code|rem}}
| {{yes}} | {{yes}} |
rowspan="2" | Kotlin
| {{code|%}}, {{code|rem}} | {{yes}} | {{yes}} |
{{code|mod}}
| {{yes}} | {{yes}} |
rowspan="2" | ksh
| {{code|%}} | {{yes}} | {{no}} | Truncated (same as POSIX sh) |
{{code|fmod}}
| {{no}} | {{yes}} | Truncated |
LabVIEW
| {{code|mod}} | {{yes}} | {{yes}} | Truncated |
LibreOffice
| {{code|1==MOD()}} | {{yes}} | {{no}} | Floored |
rowspan="2" | Logo
| {{code|MODULO}} | {{yes}} | {{no}} | Floored |
{{code|REMAINDER}}
| {{yes}} | {{no}} | Truncated |
Lua 5
| {{code|%}} | {{yes}} | {{yes}} | Floored |
Lua 4
| {{code|mod(x,y)}} | {{yes}} | {{yes}} | Truncated |
Liberty BASIC
| {{code|MOD}} | {{yes}} | {{no}} | Truncated |
Mathcad
| {{code|mod(x,y)}} | {{yes}} | {{no}} | Floored |
rowspan="3" | Maple
| {{code|e mod m}} (by default), {{code|modp(e, m)}} | {{yes}} | {{no}} | Euclidean |
{{code|mods(e, m)}}
| {{yes}} | {{no}} | Rounded |
{{code|frem(e, m)}}
| {{yes}} | {{yes}} | Rounded |
Mathematica
| {{code|Mod[a, b]}} | {{yes}} | {{no}} | Floored |
rowspan="2" | MATLAB
| {{code|mod}} | {{yes}} | {{yes}} | Floored |
{{code|rem}}
| {{yes}} | {{yes}} | Truncated |
rowspan="2" | Maxima
| {{code|mod}} | {{yes}} | {{no}} | Floored |
{{code|remainder}}
| {{yes}} | {{no}} | Truncated |
Maya Embedded Language
| {{code|%}} | {{yes}} | {{no}} | Truncated |
Microsoft Excel
| {{code|1==MOD()}} | {{yes}} | {{yes}} | Floored |
Minitab
| {{code|MOD}} | {{yes}} | {{no}} | Floored |
rowspan="2" | Modula-2
| {{code|MOD}} | {{yes}} | {{no}} | Floored |
{{code|REM}}
| {{yes}} | {{no}} | Truncated |
MUMPS
| {{code|#}} | {{yes}} | {{no}} | Floored |
rowspan="2" | Netwide Assembler (NASM, NASMX)
| {{code|%}}, {{code|div}} (unsigned) | {{yes}} | {{no}} | {{N/A}} |
{{code|%%}} (signed)
| {{yes}} | {{no}} | Implementation-defined{{cite web |title=Chapter 3: The NASM Language |url=https://nasm.us/doc/nasmdoc3.html#section-3.5.11 |website=NASM - The Netwide Assembler version 2.15.05}} |
Nim
| {{code|mod}} | {{yes}} | {{no}} | Truncated |
Oberon
| {{code|MOD}} | {{yes}} | {{no}} | Floored-like{{efn|Divisor must be positive, otherwise undefined.}} |
Objective-C
| {{code|%}} | {{yes}} | {{no}} | Truncated (same as C99) |
Object Pascal, Delphi
| {{code|mod}} | {{yes}} | {{no}} | Truncated |
rowspan="2" | OCaml
| {{code|mod}} | {{yes}} | {{no}} |
{{code|mod_float}}
| {{no}} | {{yes}} |
Occam
| {{code|\}} | {{yes}} | {{no}} | Truncated |
Pascal (ISO-7185 and -10206)
| {{code|mod}} | {{yes}} | {{no}} | Euclidean-like{{efn|1=As discussed by Boute, ISO Pascal's definitions of {{code|div}} and {{code|mod}} do not obey the Division Identity of {{math|1=D = d · (D / d) + D % d}}, and are thus fundamentally broken.}} |
rowspan="2" | Perl
| {{code|%}} | {{yes}} | {{no}} | Floored{{efn|Perl usually uses arithmetic modulo operator that is machine-independent. For examples and exceptions, see the Perl documentation on multiplicative operators.[http://perldoc.perl.org/perlop.html#Multiplicative-Operators Perl documentation]}} |
{{code|POSIX::fmod}}
| {{no}} | {{yes}} | Truncated |
rowspan="2" | Phix
| {{code|mod}} | {{yes}} | {{no}} | Floored |
{{code|remainder}}
| {{yes}} | {{no}} | Truncated |
rowspan="2" | PHP
| {{code|%}} | {{yes}} | {{no}} |
{{code|fmod}}
| {{no}} | {{yes}} |
PIC BASIC Pro
| {{code|\\}} | {{yes}} | {{no}} | Truncated |
PL/I
| {{code|mod}} | {{yes}} | {{no}} | Floored (ANSI PL/I) |
PowerShell
| {{code|%}} | {{yes}} | {{no}} | Truncated |
Programming Code (PRC)
| {{code|MATH.OP - 'MOD; (\)'}} | {{yes}} | {{no}} | Undefined |
Progress
| {{code|modulo}} | {{yes}} | {{no}} | Truncated |
rowspan="2"| Prolog ([https://www.iso.org/standard/21413.html ISO 1995])
| {{code|mod}} | {{yes}} | {{no}} | Floored |
{{code|rem}}
| {{yes}} | {{no}} | Truncated |
PureBasic
| {{code|%}}, {{code|Mod(x,y)}} | {{yes}} | {{no}} | Truncated |
PureScript
| {{code|`mod`}} | {{yes}} | {{no}} |
rowspan="2" | Pure Data
| {{code|%}} | {{yes}} | {{no}} | Truncated (same as C) |
{{code|mod}}
| {{yes}} | {{no}} | Floored |
rowspan="3" | Python
| {{code|%}} | {{yes}} | {{yes}} | Floored |
{{code|math.fmod}}
| {{no}} | {{yes}} | Truncated |
{{code|math.remainder}}
| {{no}} | {{yes}} | Rounded |
Q#
| {{code|%}} | {{yes}} | {{no}} |
R
| {{code|%%}} | {{yes}} | {{yes}} |
rowspan="2" |Racket
| {{code|modulo}} | {{yes}} | {{no}} | Floored |
{{code|remainder}}
| {{yes}} | {{no}} | Truncated |
Raku
| {{code|%}} | {{no}} | {{yes}} | Floored |
RealBasic
| {{code|MOD}} | {{yes}} | {{no}} | Truncated |
Reason
| {{code|mod}} | {{yes}} | {{no}} | Truncated |
Rexx
| {{code|//}} | {{yes}} | {{yes}} | Truncated |
RPG
| {{code|%REM}} | {{yes}} | {{no}} | Truncated |
rowspan="2" | Ruby
| {{code|%}}, {{code|modulo()}} | {{yes}} | {{yes}} | Floored |
{{code|remainder()}}
| {{yes}} | {{yes}} | Truncated |
rowspan="2" | Rust
| {{code|%}} | {{yes}} | {{yes}} | Truncated |
{{code|rem_euclid()}}
| {{yes}} | {{yes}} |
SAS
| {{code|MOD}} | {{yes}} | {{no}} | Truncated |
Scala
| {{code|%}} | {{yes}} | {{yes}} | Truncated |
rowspan="2" | Scheme
| {{code|modulo}} | {{yes}} | {{no}} | Floored |
{{code|remainder}}
| {{yes}} | {{no}} | Truncated |
rowspan="4" | Scheme R6RS
| {{code|mod}} | {{yes}} | {{no}} | Euclidean[http://www.r6rs.org/final/html/r6rs/r6rs-Z-H-14.html#node_sec_11.7.3.1 r6rs.org] |
{{code|mod0}}
| {{yes}} | {{no}} |
{{code|flmod}}
| {{no}} | {{yes}} | Euclidean |
{{code|flmod0}}
| {{no}} | {{yes}} | Rounded |
Scratch
| {{code|mod}} | {{yes}} | {{yes}} | Floored |
rowspan="2" | Seed7
| {{code|mod}} | {{yes}} | {{yes}} | Floored |
{{code|rem}}
| {{yes}} | {{yes}} | Truncated |
rowspan="2" | SenseTalk
| {{code|modulo}} | {{yes}} | {{no}} | Floored |
{{code|rem}}
| {{yes}} | {{no}} | Truncated |
POSIX shell (includes bash, mksh, &c.)
| {{code|%}} | {{yes}} | {{no}} | Truncated (same as C){{Cite web|title=Shell Command Language|url=https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_04|access-date=2021-02-05|website=pubs.opengroup.org}} |
rowspan="2" | Smalltalk
| {{code|\\}} | {{yes}} | {{no}} | Floored |
{{code|rem:}}
| {{yes}} | {{no}} | Truncated |
Snap!
| {{code|mod}} | {{yes}} | {{no}} | Floored |
Spin
| {{code|//}} | {{yes}} | {{no}} | Floored |
Solidity
| {{code|%}} | {{yes}} | {{no}} |
SQL (SQL:1999)
| {{code|mod(x,y)}} | {{yes}} | {{no}} | Truncated |
SQL (SQL:2011)
| {{code|%}} | {{yes}} | {{no}} | Truncated |
rowspan="3" | Standard ML
| {{code|mod}} | {{yes}} | {{no}} | Floored |
{{code|Int.rem}}
| {{yes}} | {{no}} | Truncated |
{{code|Real.rem}}
| {{no}} | {{yes}} | Truncated |
Stata
| {{code|mod(x,y)}} | {{yes}} | {{no}} | Euclidean |
rowspan="3" | Swift
| {{code|%}} | {{yes}} | {{no}} |
{{code|remainder(dividingBy:)}}
| {{no}} | {{yes}} |
{{code|truncatingRemainder(dividingBy:)}}
| {{no}} | {{yes}} |
rowspan="2" | Tcl
| {{code|%}} | {{yes}} | {{no}} | Floored |
{{code|fmod()}}
| {{no}} | {{yes}} | Truncated (as C) |
tcsh
| {{code|%}} | {{yes}} | {{no}} | Truncated |
Torque
| {{code|%}} | {{yes}} | {{no}} | Truncated |
Turing
| {{code|mod}} | {{yes}} | {{no}} | Floored |
Verilog (2001)
| {{code|%}} | {{yes}} | {{no}} | Truncated |
rowspan="2" | VHDL
| {{code|mod}} | {{yes}} | {{no}} | Floored |
{{code|rem}}
| {{yes}} | {{no}} | Truncated |
VimL
| {{code|%}} | {{yes}} | {{no}} | Truncated |
Visual Basic
| {{code|Mod}} | {{yes}} | {{no}} | Truncated |
rowspan="2" | WebAssembly
| {{code|i32.rem_u}}, {{code|i64.rem_u}} (unsigned) | {{yes}} | {{no}} | {{N/A}}{{cite web|editor-first1=Andreas|editor-last1=Rossberg|date=19 April 2022|title=WebAssembly Core Specification: Version 2.0|url=https://www.w3.org/TR/wasm-core-2/|publisher=World Wide Web Consortium|at=§ 4.3.2 Integer Operations}} |
{{code|i32.rem_s}}, {{code|i64.rem_s}} (signed)
| {{yes}} | {{no}} |
x86 assembly
| {{code|IDIV}} | {{yes}} | {{no}} | Truncated |
rowspan="2" | XBase++
| {{code|%}} | {{yes}} | {{yes}} | Truncated |
{{code|Mod()}}
| {{yes}} | {{yes}} | Floored |
rowspan="2" | Zig
| {{code|%}}, {{code|@rem}} | {{yes}} | {{yes}} |
{{code|@mod}}
| {{yes}} | {{yes}} | Floored |
Z3 theorem prover
| {{code|div}}, {{code|mod}} | {{yes}} | {{no}} | Euclidean |
In addition, many computer systems provide a {{code|divmod}} functionality, which produces the quotient and the remainder at the same time. Examples include the x86 architecture's {{code|IDIV}} instruction, the C programming language's {{code|div()}} function, and Python's {{code|divmod()}} function.
Generalizations
=Modulo with offset=
Sometimes it is useful for the result of {{mvar|a}} modulo {{mvar|n}} to lie not between 0 and {{math|n − 1}}, but between some number {{mvar|d}} and {{math|d + n − 1}}. In that case, {{mvar|d}} is called an offset and {{math|1=d = 1}} is particularly common.
There does not seem to be a standard notation for this operation, so let us tentatively use {{math|a modd n}}. We thus have the following definition:{{cite web |url=https://reference.wolfram.com/language/ref/Mod.html |title=Mod |author= |date= 2020 |website= Wolfram Language & System Documentation Center |publisher=Wolfram Research |access-date=April 8, 2020 }} {{math|1=x = a modd n}} just in case {{math|d ≤ x ≤ d + n − 1}} and {{math|1=x mod n = a mod n}}. Clearly, the usual modulo operation corresponds to zero offset: {{math|1=a mod n = a mod0 n}}.
The operation of modulo with offset is related to the floor function as follows:
::
To see this, let . We first show that {{math|1=x mod n = a mod n}}. It is in general true that {{math|1=(a + bn) mod n = a mod n}} for all integers {{mvar|b}}; 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 {{math|d ≤ x ≤ d + n − 1}}. Let {{mvar|k}} and {{mvar|r}} be the integers such that {{math|1=a − d = kn + r}} with {{math|0 ≤ r ≤ n − 1}} (see Euclidean division). Then , thus . Now take {{math|0 ≤ r ≤ n − 1}} and add {{mvar|d}} to both sides, obtaining {{math|d ≤ d + r ≤ d + n − 1}}. But we've seen that {{math|1=x = d + r}}, so we are done.
The modulo with offset {{math|a modd n}} is implemented in Mathematica as {{code|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:
/* Euclidean and Floored divmod, in the style of C's ldiv() */
typedef struct {
/* This structure is part of the C stdlib.h, but is reproduced here for clarity */
long int quot;
long int rem;
} ldiv_t;
/* Euclidean division */
inline ldiv_t ldivE(long numer, long denom) {
/* The C99 and C++11 languages define both of these as truncating. */
long q = numer / denom;
long r = numer % denom;
if (r < 0) {
if (denom > 0) {
q = q - 1;
r = r + denom;
} else {
q = q + 1;
r = r - denom;
}
}
return (ldiv_t){.quot = q, .rem = r};
}
/* Floored division */
inline ldiv_t ldivF(long numer, long denom) {
long q = numer / denom;
long r = numer % denom;
if ((r > 0 && denom < 0) || (r < 0 && denom > 0)) {
q = q - 1;
r = r + denom;
}
return (ldiv_t){.quot = q, .rem = r};
}
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.
See also
- Modulo (disambiguation) – many uses of the word modulo, all of which grew out of Carl F. Gauss' approach to modular arithmetic in 1801.
- Modulo (mathematics), general use of the term in mathematics
- Modular exponentiation
- Turn (angle)
Notes
{{notelist}}
References
{{Reflist}}
External links
- [https://harry.garrood.me/blog/integer-division/ Different kinds of integer division]
- [http://www.rlefebvre.ca/modulorama.htm Modulorama], animation of a cyclic representation of multiplication tables (explanation in French)
{{DEFAULTSORT:Modulo operation}}
Category:Articles with example C++ code
Category:Operators (programming)