Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Modulo
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==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). Some systems leave {{math|''a''}} modulo 0 undefined, though others 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 <math>\lfloor\,\rfloor</math> 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]], <math>\lfloor\,\rfloor</math> is the [[floor function]] ([[Rounding#Rounding down|rounding down]]), and <math>\lceil\,\rceil</math> 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 | website = [[Microsoft]] | date = December 3, 2001 | url = https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/divmodnote-letter.pdf | access-date =2014-12-25}}</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><ref>{{Cite web |title=Ada 83 LRM, Sec 4.5: Operators and Expression Evaluation |url=http://archive.adaic.com/standards/83lrm/html/lrm-04-05.html#4.5.5 |access-date=2025-03-03 |website=archive.adaic.com}}</ref>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)