FazBrowse Wikipedia Viewer | Main Page |
Search Wikipedia:
|
Viewing: https://en.wikipedia.org/wiki/Modulo_operator

Modulo

Contents
Performance issues
In programming languages
    Implementing other modulo definitions using truncation
See also
Notes
External links

In mathematics and computing, modulo is the binary operation of which the value is the remainder or signed remainder of a division of one number by another, the latter being called the modulus of the operation.

Given two positive numbers a modulo a by m is the divisor.

For example, a and m−1, inclusive. The value of a or amodm is, technically, an equivalence class, and any member of the class may be chosen as representative; however, the usual representative is the least positive residue, the smallest non-negative integer that belongs to that class (i.e., the remainder of the Euclidean division). For example, [2].

However, other conventions are possible. Computers and calculators have various ways of storing and representing numbers; thus their definition of the modulo operation depends on the programming language or the underlying hardware.

In nearly all computing systems, the quotient a divided by q∈ℤa=nq+r(n≠0)|r|<|n| 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 n or a.

Many implementations use ''truncated division'', for which the quotient is defined by\n<math display=\"block\">q = \\operatorname{trunc}\\left(\\frac{a}{n}\\right)</math>\nwhere <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.\nThus according to equation ({{EquationNote|1}}), the remainder has the ''same sign as the dividend'' {{mvar|a}} so can take {{math|1=2{{!}}''n''{{!}} − 1}} values:\n<math display=\"block\">r = a - n \\operatorname{trunc}\\left(\\frac{a}{n}\\right)</math>\n</p>\n"},"2":{"wt":" [[File:Divmod floored.svg|class=skin-invert-image|thumb|upright=1.2|Quotient and remainder using floored division]]\n<p>[[Donald Knuth]]<ref>{{cite book|first=Donald. E. |last=Knuth |title=The Art of Computer Programming |url=https://archive.org/details/artofcomputerpro0003knut |url-access=registration |publisher=Addison-Wesley |year=1972}}</ref> promotes ''floored division'', for which the quotient is defined by\n<math display=\"block\">q = \\left\\lfloor\\frac{a}{n}\\right\\rfloor</math>\nwhere <math>\\lfloor\\,\\rfloor</math> is the [[floor function]] ([[Rounding#Rounding down|rounding down]]).\nThus according to equation ({{EquationNote|1}}), the remainder has the ''same sign as the divisor'' {{mvar|n}}:\n<math display=\"block\">r = a - n \\left\\lfloor\\frac{a}{n}\\right\\rfloor</math>\n</p>\n"},"3":{"wt":"[[File:Divmod Euclidean.svg|class=skin-invert-image|thumb|upright=1.2|Quotient and remainder using Euclidean division]]\n<p>Raymond T. Boute<ref>{{cite journal |last = Boute |first = Raymond T. |title = The Euclidean definition of the functions div and mod |journal = ACM Transactions on Programming Languages and Systems |volume = 14 |issue = 2 |pages = 127–144 |publisher = ACM Press (New York, NY, USA) |date = April 1992 |url = http://portal.acm.org/citation.cfm?id=128862&coll=portal&dl=ACM |doi = 10.1145/128861.128862| hdl = 1854/LU-314490 |s2cid = 8321674 |hdl-access = free}}</ref> promotes ''[[Euclidean division]]'', for which the <strong>non-negative remainder</strong> <math>r \\in \\{0, 1, 2...\\}</math> is defined by\n<math display=\"block\">r := a - nq \\ \\mathrm{such\\ that} \\ {\\color{red}{0 \\leq r}} < |n|.</math> (Emphasis added.)\nUnder this definition, we can say the following about the quotient <math>q</math>:\n<math display=\"block\">\\begin{align}\nq &= \\frac{a - r}{n} \\in \\mathbb{Z} \\\\\n&= \\text{sgn}(n) \\cdot \\frac{a-r}{|n|} \\\\\n&= \\text{sgn}(n) \\cdot \\left( \\frac{a}{|n|} - \\frac{r}{|n|} \\right) \\\\\n&= \\text{sgn}(n) \\cdot \\left\\lfloor \\frac{a}{\\left|n\\right|} \\right\\rfloor\n\\end{align}\n</math>\nwhere {{math|sgn}} is the [[sign function]], <math>\\lfloor\\,\\rfloor</math> is the [[floor function]] ([[Rounding#Rounding down|rounding down]]), and <math>\\frac{a}{|n|} \\in \\mathbb{Q}</math>, <math>\\frac{r}{|n|} \\in \\mathbb{Q}</math> are [[rational numbers]].\n</p>\n<p>Equivalently, one may instead define the quotient <math>q \\in \\mathbb{Z}</math> as follows:\n<math display=\"block\">q := \\sgn(n) \\left\\lfloor\\frac{a}{\\left|n\\right|}\\right\\rfloor =\n\\begin{cases}\n \\left\\lfloor\\frac{a}{n}\\right\\rfloor & \\text{if } n > 0 \\\\\n \\left\\lceil\\frac{a}{n}\\right\\rceil & \\text{if } n < 0 \\\\\n\\end{cases}</math>\nwhere <math>\\lceil\\,\\rceil</math> is the [[ceiling function]] ([[Rounding#Rounding up|rounding up]]).\nThus according to equation ({{EquationNote|1}}), the remainder <math>r</math> is <strong>non-negative</strong>:\n<math display=\"block\">r = a - nq = a - |n| \\left\\lfloor\\frac{a}{\\left|n\\right|}\\right\\rfloor</math>\n</p>\n"},"4":{"wt":" [[File:Divmod rounding.svg|class=skin-invert-image|thumb|upright=1.2|Quotient and remainder using rounded division]]\n<p>Common Lisp and [[IEEE 754-1985|IEEE 754]] use ''rounded division'', for which the quotient is defined by\n<math display=\"block\">q = \\operatorname{round}\\left(\\frac{a}{n}\\right)</math>\nwhere {{math|round}} is the [[Rounding|round function]] ([[Rounding#Rounding half to even|rounding half to even]]).\nThus 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:\n<math display=\"block\">r = a - n \\operatorname{round}\\left(\\frac{a}{n}\\right)</math>\n</p>\n"},"5":{"wt":" [[File:Divmod ceiling.svg|class=skin-invert-image|thumb|upright=1.2|Quotient and remainder using ceiling division]]\n<p>Common Lisp also uses ''ceiling division'', for which the quotient is defined by\n<math display=\"block\">q = \\left\\lceil\\frac{a}{n}\\right\\rceil</math>\nwhere ⌈⌉ is the [[ceiling function]] ([[Rounding#Rounding up|rounding up]]).\nThus according to equation ({{EquationNote|1}}), the remainder has the ''opposite sign of that of the divisor'':\n<math display=\"block\">r = a - n \\left\\lceil\\frac{a}{n}\\right\\rceil</math>\n</p>\n"}},"i":0}}]}'>
  •   Quotient (r) as functions of dividend (trunc is the integral part function (rounding toward zero), i.e. the truncation to zero significant digits. Thus according to equation (1), the remainder has the same sign as the dividend r=a−ntrunc⁡(an)

  • Quotient and remainder using floored division

    Donald Knuth promotes floored division, for which the quotient is defined by n: r:=a−nq such that 0≤r<|n|. (Emphasis added.) Under this definition, we can say the following about the quotient sgn is the sign function, r|n|∈ℚ are rational numbers.

    Equivalently, one may instead define the quotient ⌈⌉ is the ceiling function (rounding up). Thus according to equation (1), the remainder q=round⁡(an) where n2, and its sign depends on which side of zero it falls to be within these boundaries: r=a−n⌈an⌉

If both the dividend and divisor are positive, then the truncated, floored, and Euclidean definitions agree. If the dividend is positive and the divisor is negative, then the truncated and Euclidean definitions agree. If the dividend is negative and the divisor is positive, then the floored and Euclidean definitions agree. If both the dividend and divisor are negative, then the truncated and floored definitions agree.

However, truncated division satisfies the identity

Some calculators have a a % n or n (the dividend) is negative and odd, boolis_odd(intn){ returnn&1; }

Performance issues

edit

Modulo operations might be implemented such that a division with a remainder is calculated each time. For special cases, on some hardware, faster alternatives exist. For example, the modulo of powers of 2 can alternatively be expressed as a bitwise AND operation (assuming x % 4 == x & 3

constant is a power of two and automatically implement them as n are integers.

External links

edit

View original on Wikipedia | Back | FazBrowse Home