Examine individual changes

This page allows you to examine the variables generated by the Edit Filter for an individual change.

Variables generated for this change

VariableValue
Edit count of the user ($1) (user_editcount)
null
Name of the user account ($1) (user_name)
'210.5.188.142'
Age of the user account ($1) (user_age)
0
Groups (including implicit) the user is in ($1) (user_groups)
[ 0 => '*' ]
Rights that the user has ($1) (user_rights)
[ 0 => 'createaccount', 1 => 'read', 2 => 'edit', 3 => 'createtalk', 4 => 'writeapi', 5 => 'viewmyprivateinfo', 6 => 'editmyprivateinfo', 7 => 'editmyoptions', 8 => 'abusefilter-log-detail', 9 => 'urlshortener-create-url', 10 => 'centralauth-merge', 11 => 'abusefilter-view', 12 => 'abusefilter-log', 13 => 'vipsscaler-test' ]
Whether or not a user is editing through the mobile interface ($1) (user_mobile)
false
Whether the user is editing from mobile app ($1) (user_app)
false
Page ID ($1) (page_id)
1352428
Page namespace ($1) (page_namespace)
0
Page title without namespace ($1) (page_title)
'Modulo'
Full page title ($1) (page_prefixedtitle)
'Modulo'
Edit protection level of the page ($1) (page_restrictions_edit)
[]
Last ten users to contribute to the page ($1) (page_recent_contributors)
[ 0 => '210.5.188.142', 1 => 'HeyElliott', 2 => 'Al Begamut', 3 => 'D.Lazard', 4 => '2600:1700:8662:2110:4E6:F479:7CF9:AD3E', 5 => 'Santropedro', 6 => 'MwGamera', 7 => 'AnonymousMathsFan2520', 8 => 'Ottoseppala', 9 => 'Desaccointier' ]
Page age in seconds ($1) (page_age)
608292190
Action ($1) (action)
'edit'
Edit summary/reason ($1) (summary)
'/* In programming languages */ '
Old content model ($1) (old_content_model)
'wikitext'
New content model ($1) (new_content_model)
'wikitext'
Old page wikitext, before the edit ($1) (old_wikitext)
'{{About|the binary operation ''mod({{mvar|a,n}})''|the ''(mod {{mvar|n}})'' notation|Modular arithmetic|other uses}} {{short description|Computational operation}} In [[computing]], the '''modulo operation''' returns the [[remainder]] or signed remainder of a [[Division (mathematics)|division]], after one number is divided by another (called the ''[[modular arithmetic|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 [[Division (mathematics)|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 [[integer]]s, 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; {{math|''a'' mod 0}} is undefined, being a [[division by zero]]). 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 [[modular arithmetic|modulo]] operation is an [[equivalence class]], and any member of the class may be chosen as [[representative (mathematics)|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 [[computer hardware|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|:|<math>\begin{align} &q \in \mathbb{Z} \\ &a = n q + r \\ &|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 [[remainder|the inequality satisfied by a remainder]].}} Standard [[Pascal (programming language)|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). {{math|''a''}} modulo 0 is undefined in most systems, although some do define it as {{math|''a''}}. {{bulleted list | [[File:Divmod truncated.svg|thumb|upright=1.2|{{colorbox|red}} Quotient ({{math|''q''}}) and {{colorbox|lightgreen}} remainder ({{math|''r''}}) as functions of dividend ({{math|''a''}}), using truncated division]] Many implementations use ''truncated division'', for which the quotient is defined by : <math>q = \operatorname{trunc}\left(\frac{a}{n}\right)</math> where <math>\operatorname{trunc}</math> is the [[Integral part|integral part function]] ([[Rounding#Rounding toward zero|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: : <math>r = a - n \operatorname{trunc}\left(\frac{a}{n}\right)</math> | [[File:Divmod floored.svg|thumb|upright=1.2|Quotient and remainder using floored division]] [[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>q = \left\lfloor\frac{a}{n}\right\rfloor</math> where ⌊⌋ is the [[floor function]] ([[Rounding#Rounding down|rounding down]]). Thus according to equation ({{EquationNote|1}}), the remainder has the ''same sign as the divisor'' {{var|n}}: : <math>r = a - n \left\lfloor\frac{a}{n}\right\rfloor</math> | [[File:Divmod Euclidean.svg|thumb|upright=1.2|Quotient and remainder using Euclidean division]] 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 quotient is defined by : <math>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|sgn}} is the [[sign function]], ⌊⌋ is the [[floor function]] ([[Rounding#Rounding down|rounding down]]), and ⌈⌉ is the [[ceiling function]] ([[Rounding#Rounding up|rounding up]]). Thus according to equation ({{EquationNote|1}}), the remainder is ''non negative'': : <math>r = a - |n| \left\lfloor\frac{a}{\left|n\right|}\right\rfloor</math> | [[File:Divmod rounding.svg|thumb|upright=1.2|Quotient and remainder using rounded division]] Common Lisp and [[IEEE 754-1985|IEEE 754]] use ''rounded division'', for which the quotient is defined by : <math>q = \operatorname{round}\left(\frac{a}{n}\right)</math> where {{math|round}} is the [[Rounding|round function]] ([[Rounding#Rounding half to even|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>r = a - n \operatorname{round}\left(\frac{a}{n}\right)</math> | [[File:Divmod ceiling.svg|thumb|upright=1.2|Quotient and remainder using ceiling division]] Common Lisp also uses ''ceiling division'', for which the quotient is defined by : <math>q = \left\lceil\frac{a}{n}\right\rceil</math> where ⌈⌉ is the [[ceiling function]] ([[Rounding#Rounding up|rounding up]]). Thus according to equation ({{EquationNote|1}}), the remainder has the ''opposite sign of that of the divisor'': : <math>r = a - n \left\lceil\frac{a}{n}\right\rceil</math> }} 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''<ref name="Leijen">{{cite web | last = Leijen | first = Daan | title = Division and Modulus for Computer Scientists | 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}}</ref>}} 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> ==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 (programming)|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 [[parity (mathematics)|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> Another alternative is to use the fact that for any odd number, the remainder may be either 1 or −1: <syntaxhighlight lang="cpp"> bool is_odd(int n) { return n % 2 == 1 || n % 2 == -1; } </syntaxhighlight> A simpler alternative is to treat the result of n % 2 as if it is a boolean value, where any non-zero value is true: <syntaxhighlight lang="cpp"> bool is_odd(int n) { return n % 2; } </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 operation|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= 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}}</ref> [[Compiler optimization]]s 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 (programming language)|C]]), unless the dividend is of an [[Signedness|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 [[Division algorithm#Division_by_a_constant|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]]. Some of these properties{{Which|date=October 2023}} 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" | [[Programming language|Language]] ! scope="col" | Operator ! scope="col" | Integer ! scope="col" | Floating-point ! abbr="Sign" scope="col" | [[#Variants of the definition|Definition]] |- | [[ABAP]] | {{code|MOD}} | {{yes}} | {{yes}} | Euclidean |- | [[ActionScript]] | {{code|%}} | {{yes}} | {{no}} | Truncated |- | rowspan="2" | [[Ada (programming language)|Ada]] | {{code|mod}} | {{yes}} | {{no}} | Floored<ref name="Ada">{{Cite journal|title=ISO/IEC 8652:2012 - Information technology — Programming languages — Ada|publisher=[[International Organization for Standardization|ISO]], [[International Electrotechnical Commission|IEC]]|year=2012|at=sec. 4.5.5 Multiplying Operators}}</ref> |- | {{code|rem}} | {{yes}} | {{no}} | Truncated<ref name="Ada" /> |- | [[ALGOL 68]] | {{code|÷×}}, {{code|mod}} | {{yes}} | {{no}} | Euclidean |- | [[AMPL]] | {{code|mod}} | {{yes}} | {{no}} | Truncated |- | [[APL (programming language)|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}} | {{no}} | Truncated |- | [[AutoLISP]] | {{code|(rem d n)}} | {{yes}} | {{no}} | Truncated |- | [[AWK]] | {{code|%}} | {{yes}} | {{no}} | Truncated |- | [[bash (Unix shell)|bash]] | {{code|%}} | {{yes}} | {{no}} | Truncated |- | [[BASIC]] | {{code|Mod}} | {{yes}} | {{no}} | Varies by implementation |- | [[bc (programming language)|bc]] | {{code|%}} | {{yes}} | {{no}} | Truncated |- | rowspan="3" | [[C (programming language)|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 name="C99">{{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 journal|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 journal|title=ISO/IEC 9899:1990: Programming languages – C |publisher=[[International Organization for Standardization|ISO]], [[International Electrotechnical Commission|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 |- | rowspan="2" | [[C Sharp (programming language)|C#]] | {{code|%}} | {{yes}} | {{yes}} | Truncated |- | {{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=learn.microsoft.com |language=en-us}}</ref> |- | [[Clarion (programming language)|Clarion]] | {{code|%}} | {{yes}} | {{no}} | Truncated |- | [[Clean (programming language)|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 name="CoffeeScript">[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|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}}</ref> |- | {{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 (programming language)|Crystal]] | {{code|%}}, {{code|modulo}} | {{yes}} | {{yes}} | Floored |- | {{code|remainder}} | {{yes}} | {{yes}} | Truncated |- | [[D (programming language)|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 (programming language)|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> |- | [[Eiffel (programming language)|Eiffel]] | {{code|\\}} | {{yes}} | {{no}} | Truncated |- | rowspan="2" | [[Elixir (programming language)|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 (programming language)|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 (programming language)|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 (programming language)|Euphoria]] | {{code|mod}} | {{yes}} | {{no}} | Floored |- | {{code|remainder}} | {{yes}} | {{no}} | Truncated |- | rowspan="2" | [[F Sharp (programming language)|F#]] | {{code|%}} | {{yes}} | {{yes}} | Truncated |- | {{code|Math.IEEERemainder}} | {{no}} | {{yes}} | Rounded<ref name=".NET" /> |- | [[Factor (programming language)|Factor]] | {{code|mod}} | {{yes}} | {{no}} | Truncated |- | [[FileMaker]] | {{code|Mod}} | {{yes}} | {{no}} | Floored |- | rowspan="3" | [[Forth (programming language)|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 (programming language)|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" | [[Godot (game engine)|GDScript (Godot)]] | {{code|%}} | {{yes}} | {{no}} | Truncated |- | {{code|fmod}} | {{no}} | {{yes}} | Truncated |- | {{code|posmod}} | {{yes}} | {{no}} | Floored |- | {{code|fposmod}} | {{no}} | {{yes}} | Floored |- | rowspan="5" | [[Go (programming language)|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> |- | {{code|big.Float.Quo}} | {{no}} | {{yes}} | Rounded<ref>{{Cite web |title=big package - math/big - pkg.go.dev |url=https://pkg.go.dev/math/big#Float.Quo |access-date=2024-04-12 |website=pkg.go.dev}}</ref> |- | [[Apache Groovy|Groovy]] | {{code|%}} | {{yes}} | {{no}} | Truncated |- | rowspan="3" | [[Haskell (programming language)|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'}} ([[Glasgow Haskell Compiler|GHC]]) | {{no}} | {{yes}} | Floored |- | [[Haxe]] | {{code|%}} | {{yes}} | {{no}} | Truncated |- | [[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--> |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 (programming language)|J]] | <code><nowiki>|</nowiki></code>{{efn|name=rev}} | {{yes}} | {{no}} | Floored |- | rowspan="2" | [[Java (programming language)|Java]] | {{code|%}} | {{yes}} | {{yes}} | Truncated |- | {{code|Math.floorMod}} | {{yes}} | {{no}} | Floored |- | [[JavaScript]]{{break}}[[TypeScript]] | {{code|%}} | {{yes}} | {{yes}} | Truncated |- | rowspan="2" | [[Julia (programming language)|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 (programming language)|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" | [[KornShell|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 (programming language)|Logo]] | {{code|MODULO}} | {{yes}} | {{no}} | Floored |- | {{code|REMAINDER}} | {{yes}} | {{no}} | Truncated |- | [[Lua (programming language)|Lua]] 5 | {{code|%}} | {{yes}} | {{yes}} | Floored |- | [[Lua (programming language)|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 (software)|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}} | {{no}} | Floored |- | {{code|rem}} | {{yes}} | {{no}} | Truncated |- | rowspan="2" | [[Maxima (software)|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]] ([[Netwide Assembler|NASM]], [[Netwide Assembler|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 (programming language)|Nim]] | {{code|mod}} | {{yes}} | {{no}} | Truncated |- | [[Oberon (programming language)|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 (programming language)|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 (programming language)|Occam]] | {{code|\}} | {{yes}} | {{no}} | Truncated |- | [[Pascal (programming language)|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>[http://perldoc.perl.org/perlop.html#Multiplicative-Operators Perl documentation]</ref>}} |- | {{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}} | 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 (Palm OS)|PRC]]) | {{code|MATH.OP - 'MOD; (\)'}} | {{yes}} | {{no}} | Undefined |- | [[OpenEdge Advanced Business Language|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}} | Euclidean<ref name="purescript_euclid">{{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="2" | [[Python (programming language)|Python]] | {{code|%}} | {{yes}} | {{yes}} | Floored |- | {{code|math.fmod}} | {{no}} | {{yes}} | Truncated |- | [[Q Sharp|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 (programming language)|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 (programming language)|Racket]] | {{code|modulo}} | {{yes}} | {{no}} | Floored |- | {{code|remainder}} | {{yes}} | {{no}} | Truncated |- | [[Raku (programming language)|Raku]] | {{code|%}} | {{no}} | {{yes}} | Floored |- | [[RealBasic]] | {{code|MOD}} | {{yes}} | {{no}} | Truncated |- | [[Reason (programming language)|Reason]] | {{code|mod}} | {{yes}} | {{no}} | Truncated |- | [[Rexx]] | {{code|//}} | {{yes}} | {{yes}} | Truncated |- | [[RPG (programming language)|RPG]] | {{code|%REM}} | {{yes}} | {{no}} | Truncated |- | rowspan="2" | [[Ruby (programming language)|Ruby]] | {{code|%}}, {{code|modulo()}} | {{yes}} | {{yes}} | Floored |- | {{code|remainder()}} | {{yes}} | {{yes}} | Truncated |- | rowspan="2" | [[Rust (programming language)|Rust]] | {{code|%}} | {{yes}} | {{yes}} | Truncated |- | {{code|rem_euclid()}} | {{yes}} | {{yes}} | Euclidean<ref name="rust_rem_euclid">{{Cite web|url=https://doc.rust-lang.org/std/primitive.f32.html#method.rem_euclid|title = F32 - Rust}}</ref> |- | [[SAS language|SAS]] | {{code|MOD}} | {{yes}} | {{no}} | Truncated |- | [[Scala (programming language)|Scala]] | {{code|%}} | {{yes}} | {{yes}} | Truncated |- | rowspan="2" | [[Scheme (programming language)|Scheme]] | {{code|modulo}} | {{yes}} | {{no}} | Floored |- | {{code|remainder}} | {{yes}} | {{no}} | Truncated |- | rowspan="4" | [[Scheme (programming language)|Scheme]] [[Scheme (programming language)#R6RS|R<sup>6</sup>RS]] | {{code|mod}} | {{yes}} | {{no}} | Euclidean<ref name="r6rs">[http://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 (programming language)|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|{{Code|sh}} (POSIX)]] (includes [[Bash (Unix shell)|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}} | {{no}} | Floored |- | {{code|rem:}} | {{yes}} | {{no}} | Truncated |- | [[Snap! (programming language)|Snap!]] | {{code|mod}} | {{yes}} | {{no}} | Floored |- | [[Parallax Propeller#Built in Spin byte code interpreter|Spin]] | {{code|//}} | {{yes}} | {{no}} | Floored |- | [[Solidity]] | {{code|%}} | {{yes}} | {{no}} | Floored |- | [[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 (programming language)|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]] | {{code|%}} | {{yes}} | {{no}} | Floored |- | {{code|fmod()}} | {{no}} | {{yes}} | Truncated (as C) |- | [[tcsh]] | {{code|%}} | {{yes}} | {{no}} | Truncated |- | [[Torque (game engine)|Torque]] | {{code|%}} | {{yes}} | {{no}} | Truncated |- | [[Turing (programming language)|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 |- | [[Vimscript|VimL]] | {{code|%}} | {{yes}} | {{no}} | Truncated |- | [[Visual Basic .NET|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" /> |- | [[x86 assembly language|x86 assembly]] | {{code|IDIV}} | {{yes}} | {{no}} | Truncated |- | rowspan="2" | [[XBase++]] | {{code|%}} | {{yes}} | {{yes}} | Truncated |- | {{code|Mod()}} | {{yes}} | {{yes}} | Floored |- | [[Zig (programming language)|Zig]] | {{code|%}}, {{code|@mod}}, {{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> |- | [[Z3 Theorem Prover|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 (programming language)|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 and ceiling functions|floor function]] as follows: ::<math>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:<ref name="Leijen" /> <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]]'s introduction of ''[[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:Computer arithmetic]] [[Category:Articles with example C++ code]] [[Category:Operators (programming)]] [[Category:Modular arithmetic]] [[Category:Operations on numbers]] [[de:Division mit Rest#Modulo]]'
New page wikitext, after the edit ($1) (new_wikitext)
'{{About|the binary operation ''mod({{mvar|a,n}})''|the ''(mod {{mvar|n}})'' notation|Modular arithmetic|other uses}} {{short description|Computational operation}} In [[computing]], the '''modulo operation''' returns the [[remainder]] or signed remainder of a [[Division (mathematics)|division]], after one number is divided by another (called the ''[[modular arithmetic|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 [[Division (mathematics)|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 [[integer]]s, 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; {{math|''a'' mod 0}} is undefined, being a [[division by zero]]). 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 [[modular arithmetic|modulo]] operation is an [[equivalence class]], and any member of the class may be chosen as [[representative (mathematics)|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 [[computer hardware|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|:|<math>\begin{align} &q \in \mathbb{Z} \\ &a = n q + r \\ &|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 [[remainder|the inequality satisfied by a remainder]].}} Standard [[Pascal (programming language)|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). {{math|''a''}} modulo 0 is undefined in most systems, although some do define it as {{math|''a''}}. {{bulleted list | [[File:Divmod truncated.svg|thumb|upright=1.2|{{colorbox|red}} Quotient ({{math|''q''}}) and {{colorbox|lightgreen}} remainder ({{math|''r''}}) as functions of dividend ({{math|''a''}}), using truncated division]] Many implementations use ''truncated division'', for which the quotient is defined by : <math>q = \operatorname{trunc}\left(\frac{a}{n}\right)</math> where <math>\operatorname{trunc}</math> is the [[Integral part|integral part function]] ([[Rounding#Rounding toward zero|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: : <math>r = a - n \operatorname{trunc}\left(\frac{a}{n}\right)</math> | [[File:Divmod floored.svg|thumb|upright=1.2|Quotient and remainder using floored division]] [[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>q = \left\lfloor\frac{a}{n}\right\rfloor</math> where ⌊⌋ is the [[floor function]] ([[Rounding#Rounding down|rounding down]]). Thus according to equation ({{EquationNote|1}}), the remainder has the ''same sign as the divisor'' {{var|n}}: : <math>r = a - n \left\lfloor\frac{a}{n}\right\rfloor</math> | [[File:Divmod Euclidean.svg|thumb|upright=1.2|Quotient and remainder using Euclidean division]] 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 quotient is defined by : <math>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|sgn}} is the [[sign function]], ⌊⌋ is the [[floor function]] ([[Rounding#Rounding down|rounding down]]), and ⌈⌉ is the [[ceiling function]] ([[Rounding#Rounding up|rounding up]]). Thus according to equation ({{EquationNote|1}}), the remainder is ''non negative'': : <math>r = a - |n| \left\lfloor\frac{a}{\left|n\right|}\right\rfloor</math> | [[File:Divmod rounding.svg|thumb|upright=1.2|Quotient and remainder using rounded division]] Common Lisp and [[IEEE 754-1985|IEEE 754]] use ''rounded division'', for which the quotient is defined by : <math>q = \operatorname{round}\left(\frac{a}{n}\right)</math> where {{math|round}} is the [[Rounding|round function]] ([[Rounding#Rounding half to even|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>r = a - n \operatorname{round}\left(\frac{a}{n}\right)</math> | [[File:Divmod ceiling.svg|thumb|upright=1.2|Quotient and remainder using ceiling division]] Common Lisp also uses ''ceiling division'', for which the quotient is defined by : <math>q = \left\lceil\frac{a}{n}\right\rceil</math> where ⌈⌉ is the [[ceiling function]] ([[Rounding#Rounding up|rounding up]]). Thus according to equation ({{EquationNote|1}}), the remainder has the ''opposite sign of that of the divisor'': : <math>r = a - n \left\lceil\frac{a}{n}\right\rceil</math> }} 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''<ref name="Leijen">{{cite web | last = Leijen | first = Daan | title = Division and Modulus for Computer Scientists | 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}}</ref>}} 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> ==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 (programming)|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 [[parity (mathematics)|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> Another alternative is to use the fact that for any odd number, the remainder may be either 1 or −1: <syntaxhighlight lang="cpp"> bool is_odd(int n) { return n % 2 == 1 || n % 2 == -1; } </syntaxhighlight> A simpler alternative is to treat the result of n % 2 as if it is a boolean value, where any non-zero value is true: <syntaxhighlight lang="cpp"> bool is_odd(int n) { return n % 2; } </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 operation|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= 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}}</ref> [[Compiler optimization]]s 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 (programming language)|C]]), unless the dividend is of an [[Signedness|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 [[Division algorithm#Division_by_a_constant|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]]. Some of these properties{{Which|date=October 2023}} 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" | [[Programming language|Language]] ! scope="col" | Operator ! scope="col" | Integer ! scope="col" | Floating-point ! abbr="Sign" scope="col" | [[#Variants of the definition|Definition]] |- | [[ABAP]] | {{code|MOD}} | {{yes}} | {{yes}} | Euclidean |- | [[ActionScript]] | {{code|%}} | {{yes}} | {{no}} | Truncated |- | rowspan="2" | [[Ada (programming language)|Ada]] | {{code|mod}} | {{yes}} | {{no}} | Floored<ref name="Ada">{{Cite journal|title=ISO/IEC 8652:2012 - Information technology — Programming languages — Ada|publisher=[[International Organization for Standardization|ISO]], [[International Electrotechnical Commission|IEC]]|year=2012|at=sec. 4.5.5 Multiplying Operators}}</ref> |- | {{code|rem}} | {{yes}} | {{no}} | Truncated<ref name="Ada" /> |- | [[ALGOL 68]] | {{code|÷×}}, {{code|mod}} | {{yes}} | {{no}} | Euclidean |- | [[AMPL]] | {{code|mod}} | {{yes}} | {{no}} | Truncated |- | [[APL (programming language)|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}} | {{no}} | Truncated |- | [[AutoLISP]] | {{code|(rem d n)}} | {{yes}} | {{no}} | Truncated |- | [[AWK]] | {{code|%}} | {{yes}} | {{no}} | Truncated |- | [[bash (Unix shell)|bash]] | {{code|%}} | {{yes}} | {{no}} | Truncated |- | [[BASIC]] | {{code|Mod}} | {{yes}} | {{no}} | Varies by implementation |- | [[bc (programming language)|bc]] | {{code|%}} | {{yes}} | {{no}} | Truncated |- | rowspan="3" | [[C (programming language)|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 name="C99">{{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 journal|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 journal|title=ISO/IEC 9899:1990: Programming languages – C |publisher=[[International Organization for Standardization|ISO]], [[International Electrotechnical Commission|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 |- | rowspan="2" | [[C Sharp (programming language)|C#]] | {{code|%}} | {{yes}} | {{yes}} | Truncated |- | {{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=learn.microsoft.com |language=en-us}}</ref> |- | [[Clarion (programming language)|Clarion]] | {{code|%}} | {{yes}} | {{no}} | Truncated |- | [[Clean (programming language)|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 name="CoffeeScript">[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|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}}</ref> |- | {{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 (programming language)|Crystal]] | {{code|%}}, {{code|modulo}} | {{yes}} | {{yes}} | Floored |- | {{code|remainder}} | {{yes}} | {{yes}} | Truncated |- | [[D (programming language)|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 (programming language)|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> |- | [[Eiffel (programming language)|Eiffel]] | {{code|\\}} | {{yes}} | {{no}} | Truncated |- | rowspan="2" | [[Elixir (programming language)|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 (programming language)|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 (programming language)|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 (programming language)|Euphoria]] | {{code|mod}} | {{yes}} | {{no}} | Floored |- | {{code|remainder}} | {{yes}} | {{no}} | Truncated |- | rowspan="2" | [[F Sharp (programming language)|F#]] | {{code|%}} | {{yes}} | {{yes}} | Truncated |- | {{code|Math.IEEERemainder}} | {{no}} | {{yes}} | Rounded<ref name=".NET" /> |- | [[Factor (programming language)|Factor]] | {{code|mod}} | {{yes}} | {{no}} | Truncated |- | [[FileMaker]] | {{code|Mod}} | {{yes}} | {{no}} | Floored |- | rowspan="3" | [[Forth (programming language)|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 (programming language)|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" | [[Godot (game engine)|GDScript (Godot)]] | {{code|%}} | {{yes}} | {{no}} | Truncated |- | {{code|fmod}} | {{no}} | {{yes}} | Truncated |- | {{code|posmod}} | {{yes}} | {{no}} | Floored |- | {{code|fposmod}} | {{no}} | {{yes}} | Floored |- | rowspan="4" | [[Go (programming language)|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> |- | [[Apache Groovy|Groovy]] | {{code|%}} | {{yes}} | {{no}} | Truncated |- | rowspan="3" | [[Haskell (programming language)|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'}} ([[Glasgow Haskell Compiler|GHC]]) | {{no}} | {{yes}} | Floored |- | [[Haxe]] | {{code|%}} | {{yes}} | {{no}} | Truncated |- | [[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--> |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 (programming language)|J]] | <code><nowiki>|</nowiki></code>{{efn|name=rev}} | {{yes}} | {{no}} | Floored |- | rowspan="2" | [[Java (programming language)|Java]] | {{code|%}} | {{yes}} | {{yes}} | Truncated |- | {{code|Math.floorMod}} | {{yes}} | {{no}} | Floored |- | [[JavaScript]]{{break}}[[TypeScript]] | {{code|%}} | {{yes}} | {{yes}} | Truncated |- | rowspan="2" | [[Julia (programming language)|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 (programming language)|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" | [[KornShell|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 (programming language)|Logo]] | {{code|MODULO}} | {{yes}} | {{no}} | Floored |- | {{code|REMAINDER}} | {{yes}} | {{no}} | Truncated |- | [[Lua (programming language)|Lua]] 5 | {{code|%}} | {{yes}} | {{yes}} | Floored |- | [[Lua (programming language)|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 (software)|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}} | {{no}} | Floored |- | {{code|rem}} | {{yes}} | {{no}} | Truncated |- | rowspan="2" | [[Maxima (software)|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]] ([[Netwide Assembler|NASM]], [[Netwide Assembler|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 (programming language)|Nim]] | {{code|mod}} | {{yes}} | {{no}} | Truncated |- | [[Oberon (programming language)|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 (programming language)|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 (programming language)|Occam]] | {{code|\}} | {{yes}} | {{no}} | Truncated |- | [[Pascal (programming language)|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>[http://perldoc.perl.org/perlop.html#Multiplicative-Operators Perl documentation]</ref>}} |- | {{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}} | 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 (Palm OS)|PRC]]) | {{code|MATH.OP - 'MOD; (\)'}} | {{yes}} | {{no}} | Undefined |- | [[OpenEdge Advanced Business Language|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}} | Euclidean<ref name="purescript_euclid">{{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="2" | [[Python (programming language)|Python]] | {{code|%}} | {{yes}} | {{yes}} | Floored |- | {{code|math.fmod}} | {{no}} | {{yes}} | Truncated |- | [[Q Sharp|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 (programming language)|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 (programming language)|Racket]] | {{code|modulo}} | {{yes}} | {{no}} | Floored |- | {{code|remainder}} | {{yes}} | {{no}} | Truncated |- | [[Raku (programming language)|Raku]] | {{code|%}} | {{no}} | {{yes}} | Floored |- | [[RealBasic]] | {{code|MOD}} | {{yes}} | {{no}} | Truncated |- | [[Reason (programming language)|Reason]] | {{code|mod}} | {{yes}} | {{no}} | Truncated |- | [[Rexx]] | {{code|//}} | {{yes}} | {{yes}} | Truncated |- | [[RPG (programming language)|RPG]] | {{code|%REM}} | {{yes}} | {{no}} | Truncated |- | rowspan="2" | [[Ruby (programming language)|Ruby]] | {{code|%}}, {{code|modulo()}} | {{yes}} | {{yes}} | Floored |- | {{code|remainder()}} | {{yes}} | {{yes}} | Truncated |- | rowspan="2" | [[Rust (programming language)|Rust]] | {{code|%}} | {{yes}} | {{yes}} | Truncated |- | {{code|rem_euclid()}} | {{yes}} | {{yes}} | Euclidean<ref name="rust_rem_euclid">{{Cite web|url=https://doc.rust-lang.org/std/primitive.f32.html#method.rem_euclid|title = F32 - Rust}}</ref> |- | [[SAS language|SAS]] | {{code|MOD}} | {{yes}} | {{no}} | Truncated |- | [[Scala (programming language)|Scala]] | {{code|%}} | {{yes}} | {{yes}} | Truncated |- | rowspan="2" | [[Scheme (programming language)|Scheme]] | {{code|modulo}} | {{yes}} | {{no}} | Floored |- | {{code|remainder}} | {{yes}} | {{no}} | Truncated |- | rowspan="4" | [[Scheme (programming language)|Scheme]] [[Scheme (programming language)#R6RS|R<sup>6</sup>RS]] | {{code|mod}} | {{yes}} | {{no}} | Euclidean<ref name="r6rs">[http://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 (programming language)|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|{{Code|sh}} (POSIX)]] (includes [[Bash (Unix shell)|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}} | {{no}} | Floored |- | {{code|rem:}} | {{yes}} | {{no}} | Truncated |- | [[Snap! (programming language)|Snap!]] | {{code|mod}} | {{yes}} | {{no}} | Floored |- | [[Parallax Propeller#Built in Spin byte code interpreter|Spin]] | {{code|//}} | {{yes}} | {{no}} | Floored |- | [[Solidity]] | {{code|%}} | {{yes}} | {{no}} | Floored |- | [[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 (programming language)|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]] | {{code|%}} | {{yes}} | {{no}} | Floored |- | {{code|fmod()}} | {{no}} | {{yes}} | Truncated (as C) |- | [[tcsh]] | {{code|%}} | {{yes}} | {{no}} | Truncated |- | [[Torque (game engine)|Torque]] | {{code|%}} | {{yes}} | {{no}} | Truncated |- | [[Turing (programming language)|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 |- | [[Vimscript|VimL]] | {{code|%}} | {{yes}} | {{no}} | Truncated |- | [[Visual Basic .NET|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" /> |- | [[x86 assembly language|x86 assembly]] | {{code|IDIV}} | {{yes}} | {{no}} | Truncated |- | rowspan="2" | [[XBase++]] | {{code|%}} | {{yes}} | {{yes}} | Truncated |- | {{code|Mod()}} | {{yes}} | {{yes}} | Floored |- | [[Zig (programming language)|Zig]] | {{code|%}}, {{code|@mod}}, {{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> |- | [[Z3 Theorem Prover|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 (programming language)|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 and ceiling functions|floor function]] as follows: ::<math>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:<ref name="Leijen" /> <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]]'s introduction of ''[[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:Computer arithmetic]] [[Category:Articles with example C++ code]] [[Category:Operators (programming)]] [[Category:Modular arithmetic]] [[Category:Operations on numbers]] [[de:Division mit Rest#Modulo]]'
Unified diff of changes made by edit ($1) (edit_diff)
'@@ -566,5 +566,5 @@ |- -| rowspan="5" | [[Go (programming language)|Go]] +| rowspan="4" | [[Go (programming language)|Go]] | {{code|%}} | {{yes}} @@ -586,9 +586,5 @@ | {{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> -|- -| {{code|big.Float.Quo}} -| {{no}} -| {{yes}} -| Rounded<ref>{{Cite web |title=big package - math/big - pkg.go.dev |url=https://pkg.go.dev/math/big#Float.Quo |access-date=2024-04-12 |website=pkg.go.dev}}</ref> + |- '
New page size ($1) (new_size)
46593
Old page size ($1) (old_size)
46802
Size change in edit ($1) (edit_delta)
-209
Lines added in edit ($1) (added_lines)
[ 0 => '| rowspan="4" | [[Go (programming language)|Go]]', 1 => '' ]
Lines removed in edit ($1) (removed_lines)
[ 0 => '| rowspan="5" | [[Go (programming language)|Go]]', 1 => '|-', 2 => '| {{code|big.Float.Quo}}', 3 => '| {{no}}', 4 => '| {{yes}}', 5 => '| Rounded<ref>{{Cite web |title=big package - math/big - pkg.go.dev |url=https://pkg.go.dev/math/big#Float.Quo |access-date=2024-04-12 |website=pkg.go.dev}}</ref>' ]
Whether or not the change was made through a Tor exit node (tor_exit_node)
false
Unix timestamp of change ($1) (timestamp)
'1712916910'