{{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, the latter being 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.<ref>{{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}}</ref>

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).<ref>{{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}}</ref> 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 \neq 0</math> satisfy the following conditions: {{NumBlk|::|<math>\begin{align} &q \in \mathbb{Z} \\ &a = n q + r \quad (n \neq 0) \\ &|r| < |n| \end{align}</math>|{{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 | thumb|upright=1.2|{{colorbox|red}} Quotient ({{math|''q''}}) and {{colorbox|lightgreen}} remainder ({{math|''r''}}) as functions of dividend ({{math|''a''}}), using truncated division <p>Many implementations use ''truncated division'', for which the quotient is defined by <math display="block">q = \operatorname{trunc}\left(\frac{a}{n}\right)</math> where <math>\operatorname{trunc}</math> 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'' {{mvar|a}} so can take {{math|1=2{{!}}''n''{{!}} − 1}} values: <math display="block">r = a - n \operatorname{trunc}\left(\frac{a}{n}\right)</math> </p> | thumb|upright=1.2|Quotient and remainder using floored division <p>Donald Knuth<ref>{{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}}</ref> promotes ''floored division'', for which the quotient is defined by <math display="block">q = \left\lfloor\frac{a}{n}\right\rfloor</math> where <math>\lfloor\,\rfloor</math> is the floor function (rounding down). Thus according to equation ({{EquationNote|1}}), the remainder has the ''same sign as the divisor'' {{mvar|n}}: <math display="block">r = a - n \left\lfloor\frac{a}{n}\right\rfloor</math> </p> |thumb|upright=1.2|Quotient and remainder using Euclidean division <p>Raymond T. Boute<ref>{{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}}</ref> promotes ''Euclidean division'', for which the <strong>non-negative remainder</strong> <math>r \in \{0, 1, 2...\}</math> is defined by <math display="block">r := a - nq \ \mathrm{such\ that} \ {\color{red}{0 \leq r}} < |n|.</math> (Emphasis added.) Under this definition, we can say the following about the quotient <math>q</math>: <math display="block">\begin{align} q &= \frac{a - r}{n} \in \mathbb{Z} \\ &= \text{sgn}(n) \cdot \frac{a-r}{|n|} \\ &= \text{sgn}(n) \cdot \left( \frac{a}{|n|} - \frac{r}{|n|} \right) \\ &= \text{sgn}(n) \cdot \left\lfloor \frac{a}{\left|n\right|} \right\rfloor \end{align} </math> where {{math|sgn}} is the sign function, <math>\lfloor\,\rfloor</math> is the floor function (rounding down), and <math>\frac{a}{|n|} \in \mathbb{Q}</math>, <math>\frac{r}{|n|} \in \mathbb{Q}</math> are rational numbers. </p> <p>Equivalently, one may instead define the quotient <math>q \in \mathbb{Z}</math> as follows: <math display="block">q := \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}</math> where <math>\lceil\,\rceil</math> is the ceiling function (rounding up). Thus according to equation ({{EquationNote|1}}), the remainder <math>r</math> is <strong>non-negative</strong>: <math display="block">r = a - nq = a - |n| \left\lfloor\frac{a}{\left|n\right|}\right\rfloor</math> </p> | thumb|upright=1.2|Quotient and remainder using rounded division <p>Common Lisp and IEEE 754 use ''rounded division'', for which the quotient is defined by <math display="block">q = \operatorname{round}\left(\frac{a}{n}\right)</math> where {{math|round}} is the round function (rounding half to even). Thus according to equation ({{EquationNote|1}}), the remainder falls between <math>-\frac{n}{2}</math> and <math>\frac{n}{2}</math>, and its sign depends on which side of zero it falls to be within these boundaries: <math display="block">r = a - n \operatorname{round}\left(\frac{a}{n}\right)</math> </p> | thumb|upright=1.2|Quotient and remainder using ceiling division <p>Common Lisp also uses ''ceiling division'', for which the quotient is defined by <math display="block">q = \left\lceil\frac{a}{n}\right\rceil</math> where ⌈⌉ is the ceiling function (rounding up). Thus according to equation ({{EquationNote|1}}), the remainder has the ''opposite sign of that of the divisor'': <math display="block">r = a - n \left\lceil\frac{a}{n}\right\rceil</math> </p> }}

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 <math>({-a})/b = {-(a/b)} = a/({-b})</math>.<ref>{{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 }}</ref><ref>{{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}}</ref>

==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:

<syntaxhighlight lang="cpp"> bool is_odd(int n) { return n % 2 == 1; } </syntaxhighlight>

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):

<syntaxhighlight lang="cpp"> bool is_odd(int n) { return n % 2 != 0; } </syntaxhighlight>

Or with the binary arithmetic: <syntaxhighlight lang="cpp"> bool is_odd(int n) { return n & 1; } </syntaxhighlight>

==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): :<code>x % 2<sup>n</sup> == x & (2<sup>n</sup> - 1)</code>

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.<ref>{{cite web |first= Adam |last= Horvath |url= https://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}}</ref>

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 <code>x % 2<sup>n</sup> == x < 0 ? x | ~(2<sup>n</sup> - 1) : x & (2<sup>n</sup> - 1)</code> 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== {{sticky header}} {| class="wikitable sortable sticky-header" |+ 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<ref>{{Cite web |date=2024 |title=ABAP Keyword Documentation |url=https://help.sap.com/doc/abapdocu_758_index_htm/7.58/en-US/index.htm?file=abenarith_operators.htm |access-date=2026-01-18 |website=help.sap.com |language=en-US}}</ref>

|- | ActionScript | {{code|%}} | {{yes}} | {{yes}} | Truncated<ref>{{Cite web |title=Operators - Adobe ActionScript® 3 (AS3) API Reference |url=https://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/operators.html#modulo |access-date=2026-01-19 |website=help.adobe.com |language=en-us}}</ref>

|- | rowspan="2" | Ada | {{code|mod}} | {{yes}} | {{no}} | Floored<ref name="Ada">{{Cite book|title=ISO/IEC 8652:2012 - Information technology — Programming languages — Ada|publisher=ISO, IEC|year=2012|at=sec. 4.5.5 Multiplying Operators}}</ref> |- | {{code|rem}} | {{yes}} | {{no}} | Truncated<ref name="Ada" />

|- | ALGOL 68 | {{code|÷×}}, {{code|÷*}}, {{code|%×}}, {{code|%*}}, <code>'''mod'''</code>{{efn|The revised report uses boldface to distinguish between keywords and identifiers, but permits other formats, including capitalization (e.g. {{code|MOD}}).}} | {{yes}} | {{no}} | Euclidean<ref>{{Cite book|title=Revised Report on the Algorithmic Language Algol 68|publisher=IFIP WG2.1|year=1973|at=sec. 10.2.3.3.n}}</ref>

|- | AMPL | {{code|mod}} | {{yes}} | {{no}} | Truncated

|- | APL | <code><nowiki>|</nowiki></code>{{efn|name=rev|Argument order reverses, i.e., <code><nowiki>α|ω</nowiki></code> computes <math>\omega\bmod\alpha</math>, the remainder when dividing {{code|ω}} by {{code|α}}.}} | {{yes}} | {{yes}} | Floored

|- | AppleScript | {{code|mod}} | {{yes}} | {{yes}} | Truncated

|- | AutoLISP | {{code|rem}} | {{yes}} | {{yes}} | Truncated

|- | AWK | {{code|%}} | {{yes}} | {{yes}} | Truncated (same as {{code|fmod}} in C)<ref>{{Cite web |title=awk |url=https://pubs.opengroup.org/onlinepubs/9799919799/utilities/awk.html#tag_20_06_13_02 |access-date=2026-01-19 |website=pubs.opengroup.org}}</ref>

|- | BASIC | {{code|Mod}} | {{yes}} | {{no}} | Varies by implementation

|- | bc | {{code|%}} | {{yes}} | {{yes}} | Truncated<ref>{{Cite web |title=bc |url=https://pubs.opengroup.org/onlinepubs/9799919799.2024edition//utilities/bc.html#tag_20_09_13_03 |access-date=2026-05-23 |website=pubs.opengroup.org}}</ref>

|- | 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.<ref>{{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}}</ref> The standards before then leave the behavior implementation-defined.<ref>{{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}}</ref>}} |- | {{code|fmod}} (C){{break}}{{code|std::fmod}} (C++) | {{no}} | {{yes}} | Truncated<ref>{{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}}.}}</ref> |- | {{code|remainder}} (C){{break}}{{code|std::remainder}} (C++) | {{no}} | {{yes}} | Rounded<ref>{{Cite book|title=ISO/IEC 9899:1999: Programming languages — C |publisher=ISO, IEC |year=1999 |at=sec. 7.12.10.2 |quote=The <code>'''remainder'''</code> functions compute the remainder <code>'''x''' REM '''y'''</code> required by IEC 60559.}}</ref>

|- | rowspan="2" | C# | {{code|%}} | {{yes}} | {{yes}} | Truncated<ref>{{Cite web |title=Expressions - C# language specification |url=https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/expressions#12124-remainder-operator |access-date=2026-01-19 |website=learn.microsoft.com |language=en-us}}</ref> |- | {{code|Math.IEEERemainder}} | {{no}} | {{yes}} | Rounded<ref name=".NET">{{Cite web |last=dotnet-bot |title=Math.IEEERemainder(Double, Double) Method (System) |url=https://learn.microsoft.com/en-us/dotnet/api/system.math.ieeeremainder?view=net-7.0 |access-date=2022-10-04 |website=Microsoft Learn |language=en-us}}</ref>

|- | Clarion | {{code|%}} | {{yes}} | {{no}} | Truncated

|- | Clean | {{code|rem}} | {{yes}} | {{no}} | Truncated

|- | rowspan="2" | Clojure | {{code|mod}} | {{yes}} | {{no}} | Floored<ref>{{Cite web |title=clojure.core - Clojure v1.10.3 API documentation |url=https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/mod |access-date=2022-03-16 |website=clojure.github.io}}</ref> |- | {{code|rem}} | {{yes}} | {{no}} | Truncated<ref>{{Cite web |title=clojure.core - Clojure v1.10.3 API documentation |url=https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/rem |access-date=2022-03-16 |website=clojure.github.io}}</ref>

|- | rowspan="2" | COBOL | {{code|FUNCTION MOD}} | {{yes}} | {{no}} | Floored<ref name="isocobol">{{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}}</ref> |- | {{code|FUNCTION REM}} | {{yes}} | {{yes}} | Truncated<ref name="isocobol"/>

|- | rowspan="2" | CoffeeScript | {{code|%}} | {{yes}} | {{no}} | Truncated |- | {{code|%%}} | {{yes}} | {{no}} | Floored<ref>[http://coffeescript.org/#operators CoffeeScript operators]</ref>

|- | ColdFusion | {{code|%}}, {{code|MOD}} | {{yes}} | {{no}} | Truncated

|- | rowspan="2" | Common Intermediate Language | {{code|rem}} (signed) | {{yes}} | {{yes}} | Truncated<ref>{{cite book|author=((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}}</ref> |- | {{code|rem.un}} (unsigned) | {{yes}} | {{no}} | {{N/A}}

|- | rowspan="2" | Common Lisp | {{code|mod}} | {{yes}} | {{yes}} | Floored<ref name="Common Lisp">{{Cite web |title=CLHS: Function MOD, REM |url=https://www.lispworks.com/documentation/HyperSpec/Body/f_mod_r.htm |access-date=2026-01-20 |website=www.lispworks.com}}</ref> |- | {{code|rem}} | {{yes}} | {{yes}} | Truncated<ref name="Common Lisp" />

|- | rowspan="2" | Crystal | {{code|%}}, {{code|modulo}} | {{yes}} | {{yes}} | Floored |- | {{code|remainder}} | {{yes}} | {{yes}} | Truncated

|- | rowspan="2"| CSS | {{code|mod()}} | {{yes}} | {{yes}} | Floored<ref>{{Cite web |date=2024-06-22 |title=mod() - CSS: Cascading Style Sheets {{!}} MDN |url=https://developer.mozilla.org/en-US/docs/Web/CSS/mod |access-date=2024-10-23 |website=developer.mozilla.org |language=en-US}}</ref> |- | {{code|rem()}} | {{yes}} | {{yes}} | Truncated<ref>{{Cite web |date=2024-10-15 |title=rem() - CSS: Cascading Style Sheets {{!}} MDN |url=https://developer.mozilla.org/en-US/docs/Web/CSS/rem |access-date=2024-10-23 |website=developer.mozilla.org |language=en-US}}</ref>

|- | D | {{code|%}} | {{yes}} | {{yes}} | Truncated<ref>{{Cite web|title=Expressions - D Programming Language|url=https://dlang.org/spec/expression.html#mul_expressions|access-date=2021-06-01|website=dlang.org}}</ref>

|- | rowspan="2" | Dart | {{code|%}} | {{yes}} | {{yes}} | Euclidean<ref>{{Cite web|title=operator % method - num class - dart:core library - Dart API|url=https://api.dart.dev/stable/2.13.1/dart-core/num/operator_modulo.html|access-date=2021-06-01|website=api.dart.dev}}</ref> |- | {{code|remainder()}} | {{yes}} | {{yes}} | Truncated<ref>{{Cite web|title=remainder method - num class - dart:core library - Dart API|url=https://api.dart.dev/stable/2.13.1/dart-core/num/remainder.html|access-date=2021-06-01|website=api.dart.dev}}</ref>

|- | dc | {{code|%}} | {{yes}} | {{yes}} | Truncated<ref>{{Cite web |title=dc, an arbitrary precision calculator |url=https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html#SEC4 |archive-url=http://web.archive.org/web/20251210120902/https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html#SEC4 |archive-date=2025-12-10 |access-date=2026-05-23 |website=www.gnu.org}}</ref>

|- | Eiffel | {{code|\\}} | {{yes}} | {{no}} | Truncated

|- | rowspan="2" | Elixir | {{code|rem/2}} | {{yes}} | {{no}} | Truncated<ref>{{Cite web|title=Kernel — Elixir v1.11.3|url=https://hexdocs.pm/elixir/Kernel.html#rem/2|access-date=2021-01-28|website=hexdocs.pm}}</ref> |- | {{code|Integer.mod/2}} | {{yes}} | {{no}} | Floored<ref>{{Cite web|title=Integer — Elixir v1.11.3|url=https://hexdocs.pm/elixir/Integer.html#mod/2|access-date=2021-01-28|website=hexdocs.pm}}</ref>

|- | rowspan="2" | Elm | {{code|modBy}} | {{yes}} | {{no}} | Floored<ref>{{Cite web |title=Basics - core 1.0.5 |url=https://package.elm-lang.org/packages/elm/core/latest/Basics#modBy |access-date=2022-03-16 |website=package.elm-lang.org}}</ref> |- | {{code|remainderBy}} | {{yes}} | {{no}} | Truncated<ref>{{Cite web |title=Basics - core 1.0.5 |url=https://package.elm-lang.org/packages/elm/core/latest/Basics#remainderBy |access-date=2022-03-16 |website=package.elm-lang.org}}</ref>

|- | rowspan="2" | Erlang | {{code|rem}} | {{yes}} | {{no}} | Truncated |- | {{code|math:fmod/2}} | {{no}} | {{yes}} | Truncated (same as C)<ref>{{Cite web|title=Erlang -- math|url=https://erlang.org/doc/man/math.html#fmod-2|access-date=2021-06-01|website=erlang.org}}</ref>

|- | rowspan="2" | Euphoria | {{code|remainder}} | {{yes}} | {{yes}} | Truncated<ref>{{Cite web |title=OpenEuphoria: Euphoria v4.0 |url=https://openeuphoria.org/docs/std_math.html#_3523_remainder |access-date=2026-01-20 |website=openeuphoria.org}}</ref> |- | {{code|mod}} | {{yes}} | {{yes}} | Floored<ref>{{Cite web |title=OpenEuphoria: Euphoria v4.0 |url=https://openeuphoria.org/docs/std_math.html#_3534_mod |access-date=2026-01-20 |website=openeuphoria.org}}</ref>

|- | rowspan="2" | F# | {{code|%}} | {{yes}} | {{yes}} | Truncated (same as C#)<ref>{{Cite web |title=18. The F# Library FSharp.Core.dll - F# Language Specification |url=https://fsharp.github.io/fslang-spec/the-f-library-fsharpcoredll/#1821-basic-arithmetic-operators |access-date=2026-01-19 |website=fsharp.github.io}}</ref> |- | {{code|Math.IEEERemainder}} | {{no}} | {{yes}} | Rounded<ref name=".NET" />

|- | rowspan="2" | Factor | {{code|mod}} | {{yes}} | {{yes}} | Truncated<ref>{{Cite web |title=mod ( x y -- z ) - Factor Documentation |url=https://docs.factorcode.org/content/word-mod,math.html |access-date=2026-01-21 |website=docs.factorcode.org}}</ref> |- | {{code|rem}} | {{yes}} | {{yes}} | Euclidean<ref>{{Cite web |title=rem ( x y -- z ) - Factor Documentation |url=https://docs.factorcode.org/content/word-rem,math.html |access-date=2026-01-21 |website=docs.factorcode.org}}</ref>

|- | 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<ref>{{Cite web |title=Manipulation and properties of numeric values — Fortran Programming Language |url=https://fortran-lang.org/learn/intrinsics/numeric/#mod |access-date=2026-05-22 |website=fortran-lang.org}}</ref> |- | {{code|modulo}} | {{yes}} | {{yes}} | Floored<ref>{{Cite web |title=Manipulation and properties of numeric values — Fortran Programming Language |url=https://fortran-lang.org/learn/intrinsics/numeric/#modulo |access-date=2026-05-22 |website=fortran-lang.org}}</ref>

|- | Frink | {{code|mod}} | {{yes}} | {{no}} | Floored

|- | rowspan="2" | Full BASIC | {{code|MOD}} | {{yes}} | {{yes}} | Floored<ref>{{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).}}</ref> |- | {{code|REMAINDER}} | {{yes}} | {{yes}} | Truncated<ref>{{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)."}}</ref>

|- | rowspan="2" | GLSL | {{code|%}} | {{yes}} | {{no}} | Undefined<ref>{{cite web|url=https://www.khronos.org/registry/OpenGL/specs/gl/GLSLangSpec.4.50.pdf|title=GLSL Language Specification, Version 4.50.7|at=section 5.9 Expressions|quote=If both operands are non-negative, then the remainder is non-negative. Results are undefined if one or both operands are negative.}}</ref> |- | {{code|mod}} | {{no}} | {{yes}} | Floored<ref>{{cite web|url=https://www.khronos.org/registry/OpenGL/specs/gl/GLSLangSpec.4.50.pdf|title=GLSL Language Specification, Version 4.50.7|at=section 8.3 Common Functions}}</ref>

|- | GameMaker Studio (GML) | {{code|mod}}, {{code|%}} | {{yes}} | {{no}} | Truncated

|- | rowspan="4" | GDScript (Godot) | {{code|%}} | {{yes}} | {{no}} | Truncated<ref>{{Cite web |title=int |url=https://docs.godotengine.org/en/stable/classes/class_int.html#class-int-operator-mod-int |access-date=2026-01-19 |website=Godot Engine documentation |language=en}}</ref> |- | {{code|posmod}} | {{yes}} | {{no}} | Euclidean<ref>{{Cite web |title=@GlobalScope |url=https://docs.godotengine.org/en/stable/classes/class_%40globalscope.html#class-globalscope-method-posmod |access-date=2026-01-19 |website=Godot Engine documentation |language=en}}</ref> |- | {{code|fmod}} | {{no}} | {{yes}} | Truncated<ref>{{Cite web |title=@GlobalScope |url=https://docs.godotengine.org/en/stable/classes/class_%40globalscope.html#class-globalscope-method-fmod |access-date=2026-01-19 |website=Godot Engine documentation |language=en}}</ref> |- | {{code|fposmod}} | {{no}} | {{yes}} | Euclidean<ref>{{Cite web |title=@GlobalScope |url=https://docs.godotengine.org/en/stable/classes/class_%40globalscope.html#class-globalscope-method-fposmod |access-date=2026-01-19 |website=Godot Engine documentation |language=en}}</ref>

|- | rowspan="4" | Go | {{code|%}} | {{yes}} | {{no}} | Truncated<ref>{{Cite web |title=The Go Programming Language Specification - The Go Programming Language |url=https://go.dev/ref/spec#Integer_operators |access-date=2022-02-28 |website=go.dev}}</ref> |- | {{code|math.Mod}} | {{no}} | {{yes}} | Truncated<ref>{{Cite web |title=math package - math - pkg.go.dev |url=https://pkg.go.dev/math#Mod |access-date=2022-02-28 |website=pkg.go.dev}}</ref> |- | {{code|big.Int.Mod}} | {{yes}} | {{no}} | Euclidean<ref>{{Cite web |title=big package - math/big - pkg.go.dev |url=https://pkg.go.dev/math/big#Int.Mod |access-date=2022-02-28 |website=pkg.go.dev}}</ref> |- | {{code|big.Int.Rem}} | {{yes}} | {{no}} | Truncated<ref>{{Cite web |title=big package - math/big - pkg.go.dev |url=https://pkg.go.dev/math/big#Int.Rem |access-date=2024-04-12 |website=pkg.go.dev}}</ref> |- | Groovy | {{code|%}} | {{yes}} | {{no}} | Truncated

|- | rowspan="3" | Haskell | {{code|mod}} | {{yes}} | {{no}} | Floored<ref name="Haskell 2010">{{Cite web |title=6 Predefined Types and Classes |url=https://www.haskell.org/onlinereport/haskell2010/haskellch6.html#x13-1370006.4.2 |access-date=2022-05-22 |website=www.haskell.org}}</ref> |- | {{code|rem}} | {{yes}} | {{no}} | Truncated<ref name="Haskell 2010" /> |- | {{code|Data.Fixed.mod'}} (GHC) | {{no}} | {{yes}} | Floored

|- | Haxe | {{code|%}} | {{yes}} | {{yes}} | Truncated<ref>{{Cite web |title=Binary Operators |url=https://haxe.org/manual/expression-operators-binops.html#arithmetic-operators |access-date=2026-01-20 |website=Haxe - The Cross-platform Toolkit}}</ref>

|- | HLSL | {{code|%}} | {{yes}} | {{yes}} | Undefined<ref>{{cite web |url=https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-operators#additive-and-multiplicative-operators |title=Operators |author=<!--Not stated--> |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.}}</ref>

|- | J | <code><nowiki>|</nowiki></code>{{efn|name=rev}} | {{yes}} | {{no}} | Floored

|- | rowspan="8" | Java | {{code|%}} | {{yes}} | {{yes}} | Truncated<ref>{{Cite web |title=Java Platform, Standard Edition Java API Reference |url=https://docs.oracle.com/en/java/javase/26/docs/specs/jls/jls-15.html#jls-15.17.3 |access-date=2026-05-24 |website=docs.oracle.com}}</ref> |- | {{code|Integer.remainderUnsigned}}, {{code|Long.remainderUnsigned}} | {{yes}} | {{no}} | {{N/A}}<ref>{{Cite web |title=Java Platform, Standard Edition Java API Reference |url=https://docs.oracle.com/en/java/javase/26/docs/api/java.base/java/lang/Integer.html#remainderUnsigned(int,int) |access-date=2026-05-25 |website=docs.oracle.com |language=en}}</ref><ref>{{Cite web |title=Java Platform, Standard Edition Java API Reference |url=https://docs.oracle.com/en/java/javase/26/docs/api/java.base/java/lang/Long.html#remainderUnsigned(long,long) |access-date=2026-05-25 |website=docs.oracle.com |language=en}}</ref> |- | {{code|Math.ceilMod}}, {{code|StrictMath.ceilMod}} | {{yes}} | {{no}} | Ceiling<ref>{{Cite web |title=Java Platform, Standard Edition Java API Reference |url=https://docs.oracle.com/en/java/javase/26/docs/api/java.base/java/lang/Math.html#ceilMod(int,int) |access-date=2026-05-25 |website=docs.oracle.com |language=en}}</ref><ref>{{Cite web |title=Java Platform, Standard Edition Java API Reference |url=https://docs.oracle.com/en/java/javase/26/docs/api/java.base/java/lang/Math.html#ceilMod(long,int) |access-date=2026-05-25 |website=docs.oracle.com |language=en}}</ref><ref>{{Cite web |title=Java Platform, Standard Edition Java API Reference |url=https://docs.oracle.com/en/java/javase/26/docs/api/java.base/java/lang/Math.html#ceilMod(long,long) |access-date=2026-05-25 |website=docs.oracle.com |language=en}}</ref><ref>{{Cite web |title=Java Platform, Standard Edition Java API Reference |url=https://docs.oracle.com/en/java/javase/26/docs/api/java.base/java/lang/StrictMath.html#ceilMod(int,int) |access-date=2026-05-25 |website=docs.oracle.com |language=en}}</ref><ref>{{Cite web |title=Java Platform, Standard Edition Java API Reference |url=https://docs.oracle.com/en/java/javase/26/docs/api/java.base/java/lang/StrictMath.html#ceilMod(long,int) |access-date=2026-05-25 |website=docs.oracle.com |language=en}}</ref><ref>{{Cite web |title=Java Platform, Standard Edition Java API Reference |url=https://docs.oracle.com/en/java/javase/26/docs/api/java.base/java/lang/StrictMath.html#ceilMod(long,long) |access-date=2026-05-25 |website=docs.oracle.com |language=en}}</ref> |- | {{code|Math.floorMod}}, {{code|StrictMath.floorMod}} | {{yes}} | {{no}} | Floored<ref>{{Cite web |title=Java Platform, Standard Edition Java API Reference |url=https://docs.oracle.com/en/java/javase/26/docs/api/java.base/java/lang/Math.html#floorMod(int,int) |access-date=2026-05-25 |website=docs.oracle.com |language=en}}</ref><ref>{{Cite web |title=Java Platform, Standard Edition Java API Reference |url=https://docs.oracle.com/en/java/javase/26/docs/api/java.base/java/lang/Math.html#floorMod(long,int) |access-date=2026-05-25 |website=docs.oracle.com |language=en}}</ref><ref>{{Cite web |title=Java Platform, Standard Edition Java API Reference |url=https://docs.oracle.com/en/java/javase/26/docs/api/java.base/java/lang/Math.html#floorMod(long,long) |access-date=2026-05-25 |website=docs.oracle.com |language=en}}</ref><ref>{{Cite web |title=Java Platform, Standard Edition Java API Reference |url=https://docs.oracle.com/en/java/javase/26/docs/api/java.base/java/lang/StrictMath.html#floorMod(int,int) |access-date=2026-05-25 |website=docs.oracle.com |language=en}}</ref><ref>{{Cite web |title=Java Platform, Standard Edition Java API Reference |url=https://docs.oracle.com/en/java/javase/26/docs/api/java.base/java/lang/StrictMath.html#floorMod(long,int) |access-date=2026-05-25 |website=docs.oracle.com |language=en}}</ref><ref>{{Cite web |title=Java Platform, Standard Edition Java API Reference |url=https://docs.oracle.com/en/java/javase/26/docs/api/java.base/java/lang/StrictMath.html#floorMod(long,long) |access-date=2026-05-25 |website=docs.oracle.com |language=en}}</ref> |- | {{code|Math.IEEEremainder}}, {{code|StrictMath.IEEEremainder}} | {{no}} | {{yes}} | Rounded<ref>{{Cite web |title=Java Platform, Standard Edition Java API Reference |url=https://docs.oracle.com/en/java/javase/26/docs/api/java.base/java/lang/Math.html#IEEEremainder(double,double) |access-date=2026-05-25 |website=docs.oracle.com |language=en}}</ref><ref>{{Cite web |title=Java Platform, Standard Edition Java API Reference |url=https://docs.oracle.com/en/java/javase/26/docs/api/java.base/java/lang/StrictMath.html#IEEEremainder(double,double) |access-date=2026-05-25 |website=docs.oracle.com |language=en}}</ref> |- | <code>''a''.mod(''b'')</code> ({{code|BigInteger}}) | {{yes}} | {{no}} | Euclidean<ref>{{Cite web |title=Java Platform, Standard Edition Java API Reference |url=https://docs.oracle.com/en/java/javase/26/docs/api/java.base/java/math/BigInteger.html#mod(java.math.BigInteger) |access-date=2026-05-25 |website=docs.oracle.com |language=en}}</ref> |- | <code>''a''.remainder(''b'')</code> ({{code|BigInteger}}) | {{yes}} | {{no}} | Truncated<ref>{{Cite web |title=Java Platform, Standard Edition Java API Reference |url=https://docs.oracle.com/en/java/javase/26/docs/api/java.base/java/math/BigInteger.html#remainder(java.math.BigInteger) |access-date=2026-05-25 |website=docs.oracle.com |language=en}}</ref> |- | <code>''a''.remainder(''b'')</code>, {{nowrap|<code>''a''.remainder(''b'', mathContext)</code>}} ({{code|BigDecimal}}) | {{no}} | {{yes}} | Truncated<ref>{{Cite web |title=Java Platform, Standard Edition Java API Reference |url=https://docs.oracle.com/en/java/javase/26/docs/api/java.base/java/math/BigDecimal.html#remainder(java.math.BigDecimal) |access-date=2026-05-25 |website=docs.oracle.com |language=en}}</ref><ref>{{Cite web |title=Java Platform, Standard Edition Java API Reference |url=https://docs.oracle.com/en/java/javase/26/docs/api/java.base/java/math/BigDecimal.html#remainder(java.math.BigDecimal,java.math.MathContext) |access-date=2026-05-25 |website=docs.oracle.com |language=en}}</ref>

|- | JavaScript{{break}}TypeScript | {{code|%}} | {{yes}} | {{yes}} | Truncated

|- | rowspan="2" | Julia | {{code|mod}} | {{yes}} | {{yes}} | Floored<ref>{{Cite web|title=Mathematics · The Julia Language|url=https://docs.julialang.org/en/v1/base/math/#Base.mod|access-date=2021-11-20|website=docs.julialang.org}}</ref> |- | {{code|%}}, {{code|rem}} | {{yes}} | {{yes}} | Truncated<ref>{{Cite web|title=Mathematics · The Julia Language|url=https://docs.julialang.org/en/v1/base/math/#Base.rem|access-date=2021-11-20|website=docs.julialang.org}}</ref>

|- | rowspan="2" | Kotlin | {{code|%}}, {{code|rem}} | {{yes}} | {{yes}} | Truncated<ref>{{Cite web|title=rem - Kotlin Programming Language|url=https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/rem.html|access-date=2021-05-05|website=Kotlin|language=en}}</ref> |- | {{code|mod}} | {{yes}} | {{yes}} | Floored<ref>{{Cite web|title=mod - Kotlin Programming Language|url=https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/mod.html|access-date=2021-05-05|website=Kotlin|language=en}}</ref>

|- | rowspan="2" | ksh | {{code|%}} | {{yes}} | {{no}} | Truncated (same as POSIX {{code|sh}}) |- | {{code|fmod}} | {{no}} | {{yes}} | Truncated

|- | LabVIEW | {{code|mod}} | {{yes}} | {{yes}} | Truncated

|- | LibreOffice | {{code|MOD}} | {{yes}} | {{yes}} | Floored<ref>{{Cite web |title=Mathematical Functions |url=https://help.libreoffice.org/latest/en-US/text/scalc/01/04060106.html#hd_id3158247 |access-date=2026-01-21 |website=help.libreoffice.org}}</ref>

|- | 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}} | {{yes}} | Floored<ref>{{Cite web |title=Mod: Get the remainder on division—Wolfram Documentation |url=https://reference.wolfram.com/language/ref/Mod.html.en |access-date=2026-05-25 |website=reference.wolfram.com |language=en}}</ref>

|- | rowspan="2" | MATLAB | {{code|mod}} | {{yes}} | {{yes}} | Floored<ref>{{Cite web |title=mod - Remainder after division (modulo operation) - MATLAB |url=https://www.mathworks.com/help/matlab/ref/double.mod.html |archive-url=https://web.archive.org/web/20260115162309/https://www.mathworks.com/help/matlab/ref/double.mod.html |archive-date=2026-01-15 |access-date=2026-01-21 |website=www.mathworks.com |language=en}}</ref> |- | {{code|rem}} | {{yes}} | {{yes}} | Truncated<ref>{{Cite web |title=rem - Remainder after division - MATLAB |url=https://www.mathworks.com/help/matlab/ref/double.rem.html |archive-url=https://web.archive.org/web/20260101202349/https://www.mathworks.com/help/matlab/ref/double.rem.html |archive-date=2026-01-01 |access-date=2026-01-21 |website=www.mathworks.com |language=en}}</ref>

|- | rowspan="2" | Maxima | {{code|mod}} | {{yes}} | {{no}} | Floored |- | {{code|remainder}} | {{yes}} | {{no}} | Truncated

|- | Maya Embedded Language | {{code|%}} | {{yes}} | {{no}} | Truncated

|- | Microsoft Excel | {{code|MOD}} | {{yes}} | {{yes}} | Floored<ref>{{Cite web |title=MOD function - Microsoft Support |url=https://support.microsoft.com/en-us/office/mod-function-9b6cd169-b6ee-406a-a97b-edf2a9dc24f3 |access-date=2026-01-21 |website=support.microsoft.com}}</ref>

|- | 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<ref>{{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}}</ref>

|- | 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}} | Truncated<ref>{{Cite web|title=OCaml library : Stdlib|url=https://ocaml.org/releases/4.13/api/Stdlib.html#VAL(mod)|access-date=2022-02-19|website=ocaml.org}}</ref> |- | {{code|mod_float}} | {{no}} | {{yes}} | Truncated<ref>{{Cite web|title=OCaml library : Stdlib|url=https://ocaml.org/releases/4.13/api/Stdlib.html#VALmod_float|access-date=2022-02-19|website=ocaml.org}}</ref>

|- | 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.<ref>[https://perldoc.perl.org/perlop.html#Multiplicative-Operators Perl documentation]</ref>}} |- | {{code|POSIX::fmod}} | {{no}} | {{yes}} | Truncated (same as C)<ref>{{Cite web |title=POSIX - Perl interface to IEEE Std 1003.1 - Perldoc Browser |url=https://perldoc.perl.org/POSIX#fmod |access-date=2026-05-22 |website=perldoc.perl.org}}</ref>

|- | rowspan="2" | PHP | {{code|%}} | {{yes}} | {{no}} | Truncated<ref>{{Cite web|title=PHP: Arithmetic Operators - Manual|url=https://www.php.net/manual/en/language.operators.arithmetic.php|access-date=2021-11-20|website=www.php.net}}</ref> |- | {{code|fmod}} | {{no}} | {{yes}} | Truncated<ref>{{Cite web|title=PHP: fmod - Manual|url=https://www.php.net/manual/en/function.fmod.php|access-date=2021-11-20|website=www.php.net}}</ref>

|- | 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 | {{code|mod}} | {{yes}} | {{no}} | Floored |- | {{code|rem}} | {{yes}} | {{no}} | Truncated

|- | PureBasic | {{code|%}}, {{code|Mod(x,y)}} | {{yes}} | {{no}} | Truncated

|- | PureScript | {{code|`mod`}} | {{yes}} | {{no}} | Euclidean<ref>{{Cite web|url=https://pursuit.purescript.org/packages/purescript-prelude/5.0.1/docs/Data.EuclideanRing#t:EuclideanRing|title = EuclideanRing}}</ref>

|- | rowspan="2" | Pure Data | {{code|%}} | {{yes}} | {{no}} | Truncated (same as C) |- | {{code|mod}} | {{yes}} | {{no}} | Floored

|- | rowspan="3" | Python | {{code|%}} | {{yes}} | {{yes}} | Floored<ref>{{Cite web |title=6. Expressions |url=https://docs.python.org/3/reference/expressions.html#binary-arithmetic-operations |access-date=2026-01-21 |website=Python documentation |language=en}}</ref> |- | {{code|math.fmod}} | {{no}} | {{yes}} | Truncated<ref>{{Cite web |title=math — Mathematical functions |url=https://docs.python.org/3/library/math.html#math.fmod |access-date=2026-01-21 |website=Python documentation |language=en}}</ref> |- | {{code|math.remainder}} | {{no}} | {{yes}} | Rounded<ref>{{Cite web |title=math — Mathematical functions |url=https://docs.python.org/3/library/math.html#math.remainder |access-date=2026-01-21 |website=Python documentation |language=en}}</ref>

|- | Q# | {{code|%}} | {{yes}} | {{no}} | Truncated<ref>{{Cite web|url=https://docs.microsoft.com/en-us/quantum/quantum-qr-expressions?view=qsharp-preview#numeric-expressions|title=Expressions|last=QuantumWriter|website=docs.microsoft.com|language=en-us|access-date=2018-07-11}}</ref>

|- | R | {{code|%%}} | {{yes}} | {{yes}} | Floored<ref>{{Cite web |title=R: Arithmetic Operators |url=https://search.r-project.org/R/refmans/base/html/Arithmetic.html |access-date=2022-12-24 |website=search.r-project.org}}</ref>

|- | 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<ref>{{Cite web |title=Operator expressions - The Rust Reference |url=https://doc.rust-lang.org/stable/reference/expressions/operator-expr.html#r-expr.arith-logic.behavior |access-date=2026-01-21 |website=doc.rust-lang.org}}</ref> |- | {{code|rem_euclid()}} | {{yes}} | {{yes}} | Euclidean<ref>{{Cite web|url=https://doc.rust-lang.org/std/primitive.f32.html#method.rem_euclid|title = F32 - Rust}}</ref>

|- | 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 R<sup>6</sup>RS | {{code|mod}} | {{yes}} | {{no}} | Euclidean<ref name="r6rs">[https://www.r6rs.org/final/html/r6rs/r6rs-Z-H-14.html#node_sec_11.7.3.1 r6rs.org]</ref> |- | {{code|mod0}} | {{yes}} | {{no}} | Rounded<ref name="r6rs"/> |- | {{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

|- | {{Code|sh}} (POSIX) (includes bash, mksh, &c.) | {{code|%}} | {{yes}} | {{no}} | Truncated (same as C)<ref>{{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}}</ref>

|- | rowspan="2" | Smalltalk | {{code|\\}} | {{yes}} | {{yes}} | Floored<ref name="Smalltalk">{{Cite book|title=ANSI INCITS 319-1998 (R2002): Information Technology - Programming Languages - Smalltalk|publisher=American National Standards Institute (ANSI)|year=1998}}</ref>{{rp|at=sec. 5.6.2.9}} |- | {{code|rem:}} | {{yes}} | {{yes}} | Truncated<ref name="Smalltalk" />{{rp|at=sec. 5.6.2.30}}

|- | Snap! | {{code|mod}} | {{yes}} | {{no}} | Floored

|- | Spin | {{code|//}} | {{yes}} | {{no}} | Floored

|- | Solidity | {{code|%}} | {{yes}} | {{no}} | Truncated<ref>{{Cite web|title=Solidity Documentation|url=https://docs.soliditylang.org/en/v0.8.28/types.html|access-date=2024-10-17|website=docs.soliditylang.org}}</ref>

|- | 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}} | Truncated<ref>{{Cite web|title=Apple Developer Documentation|url=https://developer.apple.com/documentation/swift/binaryinteger/2885003|access-date=2021-11-20|website=developer.apple.com}}</ref> |- | {{code|remainder(dividingBy:)}} | {{no}} | {{yes}} | Rounded<ref>{{Cite web|title=Apple Developer Documentation|url=https://developer.apple.com/documentation/swift/floatingpoint/3017981-remainder|access-date=2021-11-20|website=developer.apple.com}}</ref> |- | {{code|truncatingRemainder(dividingBy:)}} | {{no}} | {{yes}} | Truncated<ref>{{Cite web|title=Apple Developer Documentation|url=https://developer.apple.com/documentation/swift/floatingpoint/3017995-truncatingremainder|access-date=2021-11-20|website=developer.apple.com}}</ref>

|- | rowspan="2" | Tcl | {{nowrap|<code>expr ''a'' % ''n''</code>}}, {{nowrap|<code>::tcl::mathop::% ''a'' ''n''</code>}}{{efn|The {{code|expr}} command is defined independantly of commands in the {{code|::tcl::mathop}} namespace.<ref>{{Cite web |title=mathop manual page - Tcl Mathematical Operator Commands |url=https://www.tcl-lang.org/man/tcl9.0/TclCmd/mathop.html#M4 |access-date=2026-05-22 |website=www.tcl-lang.org}}</ref>}} | {{yes}} | {{no}} | Floored<ref>{{Cite web |title=expr manual page - Tcl Built-In Commands |url=https://www.tcl-lang.org/man/tcl9.0/TclCmd/expr.html#M16 |access-date=2026-05-22 |website=www.tcl-lang.org}}</ref><ref>{{Cite web |title=mathop manual page - Tcl Mathematical Operator Commands |url=https://www.tcl-lang.org/man/tcl9.0/TclCmd/mathop.html#M11 |access-date=2026-05-22 |website=www.tcl-lang.org}}</ref> |- | {{nowrap|<code>expr fmod(''a'', ''n'')</code>}}, {{nowrap|<code>::tcl::mathfunc::fmod ''a'' ''n''</code>}}{{efn|The {{code|expr}} command is defined in terms of commands in the {{code|::tcl::mathfunc}} namespace.<ref>{{Cite web |title=mathfunc manual page - Tcl Mathematical Functions |url=https://www.tcl-lang.org/man/tcl9.0/TclCmd/mathfunc.html#M4 |access-date=2026-05-22 |website=www.tcl-lang.org}}</ref>}} | {{no}} | {{yes}} | Truncated (same 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}}<ref name="wasmcs2">{{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}}</ref> |- | {{code|i32.rem_s}}, {{code|i64.rem_s}} (signed) | {{yes}} | {{no}} | Truncated<ref name="wasmcs2" />

|- | rowspan="4" | x86 assembly | {{code|DIV}} (unsigned) || {{yes}} || {{no}} || {{N/A}} |- | {{code|IDIV}} (signed) || {{yes}} || {{no}} || Truncated |- | {{code|FPREM}} || {{no}} || {{yes}} || Truncated |- | {{code|FPREM1}} || {{no}} || {{yes}} || Rounded

|- | rowspan="2" | XBase++ | {{code|%}} | {{yes}} | {{yes}} | Truncated |- | {{code|Mod()}} | {{yes}} | {{yes}} | Floored

|- | rowspan="2" | Zig | {{code|%}}, {{code|@rem}} | {{yes}} | {{yes}} | Truncated<ref>{{Cite web |title=Zig Documentation |url=https://ziglang.org/documentation/0.10.0/#Table-of-Operators |access-date=2022-12-18 |website=Zig Programming Language}}</ref> |- | {{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|DIV}} and {{code|IDIV}} instructions, 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'' mod<sub>''d''</sub> ''n''}}. We thus have the following definition:<ref name="Mathematica Mod" >{{cite web |url=https://reference.wolfram.com/language/ref/Mod.html |title=Mod |author=<!--Not stated--> |date= 2020 |website= Wolfram Language & System Documentation Center |publisher=Wolfram Research |access-date=April 8, 2020 }}</ref> {{math|1=''x'' = ''a'' mod<sub>''d''</sub> ''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'' mod<sub>0</sub> ''n''}}.

The operation of modulo with offset is related to the floor function as follows: <math display="block">a \operatorname{mod}_d n = a - n \left\lfloor\frac{a-d}{n}\right\rfloor.</math>

To see this, let <math display="inline">x = a - n \left\lfloor\frac{a-d}{n}\right\rfloor</math>. 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 <math display="inline">b = -\!\left\lfloor\frac{a-d}{n}\right\rfloor</math>; but that means that <math display="inline">x \bmod n = \left(a - n \left\lfloor\frac{a-d}{n}\right\rfloor\right)\! \bmod n = a \bmod n</math>, 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 <math display="inline">\left\lfloor\frac{a-d}{n}\right\rfloor = k</math>, thus <math display="inline">x = a - n \left\lfloor\frac{a-d}{n}\right\rfloor = a - n k = d +r</math>. 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'' mod<sub>''d''</sub> ''n''}} is implemented in Mathematica as {{code|Mod[a, n, d]}}&thinsp;.<ref name="Mathematica Mod" />

=== 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:

<syntaxhighlight lang="c"> /* 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}; } </syntaxhighlight>

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.<!--

The other modes of rounding are: <syntaxhighlight lang="c"> /* Round-division */ ldiv_t ldivR(long numer, long denom) {

}

/* Ceiling-division */ ldiv_t ldivC(long numer, long denom) {

} </syntaxhighlight> -->

==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] * [https://www.rlefebvre.ca/modulorama.htm Modulorama], animation of a cyclic representation of multiplication tables (explanation in French)

{{DEFAULTSORT:Modulo operation}} Category:Computer arithmetic Category:Articles with example C++ code Category:Operators (programming) Category:Modular arithmetic Category:Operations on numbers

de:Division mit Rest#Modulo