Qortora · Search · Indexed page

en.wikipedia.orgFetched 2026-08-15T00:53:26Z

Modulo - Wikipedia

Modulo - Wikipedia Jump to content Main menu Main menu move to sidebar hide Navigation Main page Contents Current events Random article About Wikipedia Contact us Contribute Help Learn to edit Community portal Recent changes Upload file Special pages Search Search Appearance Dona…

Open original source · Full cached text

Modulo - Wikipedia Jump to content Main menu Main menu move to sidebar hide Navigation Main page Contents Current events Random article About Wikipedia Contact us Contribute Help Learn to edit Community portal Recent changes Upload file Special pages Search Search Appearance Donate Create account Log in Personal tools Donate Create account Log in Contents move to sidebar hide (Top) 1 Variants of the definition 2 Notation 3 Common pitfalls 4 Performance issues 5 Properties (identities) 6 In programming languages 7 Generalizations Toggle Generalizations subsection 7.1 Modulo with offset 7.2 Implementing other modulo definitions using truncation 8 See also 9 Notes 10 References 11 External links Toggle the table of contents Modulo 26 languages العربية Català Dansk Deutsch Español فارسی Français Հայերեն Bahasa Indonesia Íslenska Italiano 日本語 Қазақша 한국어 Norsk bokmål Polski Português Română Simple English Slovenščina Српски / srpski Svenska Türkçe Tiếng Việt 粵語 中文 Edit links Article Talk English Read Edit View history Tools Tools move to sidebar hide Actions Read Edit View history General What links here Related changes Upload file Permanent link Page information Cite this page Get shortened URL Switch to legacy parser Print/export Download as PDF Printable version In other projects Wikidata item Appearance move to sidebar hide From Wikipedia, the free encyclopedia (Redirected from Modulo operation) This article is about the binary operation mod(a,n). For the (mod n) notation, see Modular arithmetic. For other uses, see Modulo (disambiguation). Computational operation In computing and mathematics, the modulo operation returns the remainder or signed remainder of a division, after one number is divided by another, the latter being called the modulus of the operation. Given two positive numbers a and n, a modulo n (often abbreviated as a mod n) is the remainder of the Euclidean division of a by n, where a is the dividend and n is the divisor.[1] 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 a and n both being integers, many computing systems now allow other types of numeric operands. The range of values for an integer modulo operation of n is 0 to n − 1. a mod 1 is always 0, assuming a is an integer. When exactly one of a or n is negative, the basic definition breaks down, and programming languages differ in how these values are defined. Variants of the definition [edit] In mathematics, the result of the modulo operation is an equivalence class, and any member of the class may be chosen as representative; however, the usual representative is the least positive residue, the smallest non-negative integer that belongs to that class (i.e., the remainder of the Euclidean division).[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 q and the remainder r of a divided by satisfy the following conditions: \\begin{align}\n&q \\in \\mathbb{Z} \\\\\n&a = n q + r \\quad (n \\neq 0) \\\\\n&|r| < |n|\n\\end{align}</math>"},"3":{"wt":"{{EquationRef|1}}"}},"i":0}}]}'> 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 a or n.[a] Standard Pascal and ALGOL 68, for example, give a positive remainder (or 0) even for negative divisors, and some programming languages, such as C90, leave it to the implementation when either of n or a is negative (see the table under § In programming languages for details). Some systems leave a modulo 0 undefined, though others define it as 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|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|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|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|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 (q) and   remainder (r) as functions of dividend (a), using truncated division Many implementations use truncated division, for which the quotient is defined by where is the integral part function (rounding toward zero), i.e. the truncation to zero significant digits. Thus according to equation (1), the remainder has the same sign as the dividend a so can take 2|n| − 1 values: Quotient and remainder using floored division Donald Knuth[3] promotes floored division, for which the quotient is defined by where is the floor function (rounding down). Thus according to equation (1), the remainder has the same sign as the divisor n: Quotient and remainder using Euclidean division Raymond T. Boute[4] promotes Euclidean division, for which the non-negative remainder is defined by (Emphasis added.) Under this definition, we can say the following about the quotient : where sgn is the sign function, is the floor function (rounding down), and , are rational numbers. Equivalently, one may instead define the quotient as follows: 0 \\\\\n \\left\\lceil\\frac{a}{n}\\right\\rceil & \\text{if } n < 0 \\\\\n\\end{cases}"}}'>0\\\left\lceil {\frac {a}{n}}\right\rceil &{\text{if }}n<0\\\end{cases}}}"/> where is the ceiling function (rounding up). Thus according to equation (1), the remainder is non-negative: Quotient and remainder using rounded division Common Lisp and IEEE 754 use rounded division, for which the quotient is defined by where round is the round function (rounding half to even). Thus according to equation (1), the remainder falls between and , and its sign depends on which side of zero it falls to be within these boundaries: Quotient and remainder using ceiling division Common Lisp also uses ceiling division, for which the quotient is defined by where ⌈⌉ is the ceiling function (rounding up). Thus according to equation (1), the remainder has the opposite sign of that of the divisor: If both the dividend and divisor are positive, then the truncated, floored, and Euclidean definitions agree. If the dividend is positive and the divisor is negative, then the truncated and Euclidean definitions agree. If the dividend is negative and the divisor is positive, then the floored and Euclidean definitions agree. If both the dividend and divisor are negative, then the truncated and floored definitions agree. However, truncated division satisfies the identity .[5][6] Notation [edit] This section is about the binary mod operation. For the (mod m) notation, see congruence relation. Some calculators have a mod() function button, and many programming languages have a similar function, expressed as mod(a, n), for example. Some also support expressions that use "%", "mod", or "Mod" as a modulo or remainder operator, …