| FazBrowse Wikipedia Viewer | Main Page | | |
| Viewing: https://en.wikipedia.org/wiki/Modulo_operator |
| 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|
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.
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; }
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
| Language | Operator | Integer | Floating-point | Definition | |||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ABAP | Yes | Euclidean | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| ActionScript | Yes | Truncated | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Ada | No | Floored | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| No | Truncated | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ALGOL 68 | %×, No | Euclidean | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| AMPL | No | Truncated | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| APL | | | mod | rem | % | fmod in C) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| BASIC | No | Varies by implementation | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| bc | Yes | Truncated | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| C div |
fmod (C) No |
Yes | Rounded | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| C# | Yes | Truncated | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Yes | Rounded | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Clarion | No | Truncated | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Clean | No | Truncated | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Clojure | No | Floored | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| No | Truncated | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| COBOL | No | Floored | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Yes | Truncated | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| CoffeeScript | No | Truncated (same as JavaScript) | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| No | Floored | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ColdFusion | Yes | Yes | Yes | mod | rem | %, Yes | Floored | ||||||||||||||||||||||||||||||||||||||||||||||||
| Yes | Truncated | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| CSS | Yes | Floored | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Yes | Truncated | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| D | Yes | Truncated | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Dart | Yes | Euclidean | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Yes | Truncated | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| dc | Yes | Truncated | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Eiffel | No | Truncated | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Elixir | No | Truncated | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| No | Floored | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Elm | No | Floored | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| No | Truncated | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Erlang | No | Truncated | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Yes | Truncated (same as C) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Euphoria | Yes | Truncated | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Yes | Floored | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| F# | Yes | Truncated (same as C#) | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Yes | Rounded | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Factor | Yes | Truncated | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Yes | Euclidean | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| FileMaker | No | Floored | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Forth | No | Implementation defined | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| No | Floored | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| No | Truncated | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Fortran | Yes | Truncated | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Yes | Floored | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Frink | Yes | Yes | Yes | Yes | No | % | % | posmod | fmod | fposmod | % | math.Mod | big.Int.Mod | big.Int.Rem | % | mod | rem | Data.Fixed.mod' (GHC) | % | % | Yes | Yes | Long.remainderUnsigned | —StrictMath.ceilMod | Math.floorMod, No | Floored | |||||||||||||||||||||||||||||
| No | Yes | Yes | BigDecimal) | TypeScript | Yes | Truncated | |||||||||||||||||||||||||||||||||||||||||||||||||
| Julia | Yes | Floored | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Yes | rem | mod | % | sh) | |||||||||||||||||||||||||||||||||||||||||||||||||||
| Yes | Truncated | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| LabVIEW | Yes | Truncated | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| LibreOffice | Yes | Floored | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Logo | No | Floored | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| No | Truncated | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Lua 5 | Yes | Floored | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Lua 4 | Yes | Truncated | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Liberty BASIC | No | Truncated | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Mathcad | No | Floored | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Maple | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | No | Yes | Yes | Yes | Yes | Yes | div (unsigned) | —Yes | Yes | Yes | Yes | Yes | Yes | Int.rem | mod_float, Yes | Truncated | ||||||||||||||||||||||||||||||
| Occam | No | Truncated | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Pascal (ISO-7185 and -10206) | No | Euclidean-like | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Perl | No | Floored | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Yes | Truncated (same as C) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| PHP | No | Truncated | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Yes | Truncated | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| PIC BASIC Pro | No | Truncated | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| PL/I | No | Floored (ANSI PL/I) | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| PowerShell | No | Truncated | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Programming Code (PRC) | No | Undefined | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Progress | No | Truncated | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Prolog | No | Floored | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| No | Truncated | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| PureBasic | No | Truncated | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Yes | Truncated | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| PureScript | No | Euclidean | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Pure Data | No | Truncated (same as C) | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| No | Floored | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Python | Yes | Floored | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Yes | Truncated | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Yes | Rounded | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Q# | No | Truncated | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| R | Yes | Floored | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Racket | Some | Floored | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Some | Truncated | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Raku | Yes | Floored | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Reason | No | Truncated | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Rexx | Yes | Truncated | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| RPG | No | Truncated | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| Ruby | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | No | No | Yes | Yes | Yes | Yes | Yes | % | \\ | rem: | mod | // | % | mod(x,y) | % | mod | Int.rem | Real.rem | mod(x,y) | % | remainder(dividingBy:) | truncatingRemainder(dividingBy:) | expr a % n, ::tcl::mathop::% ''a'' ''n''</code>"}},"i":0}}]}'>::tcl::mathop::% a n | expr fmod(a, n), ::tcl::mathfunc::fmod ''a'' ''n''</code>"}},"i":0}}]}'>::tcl::mathfunc::fmod a n | % | % | mod | % | mod | rem | % | Mod | i32.rem_u, No | {{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>"]}'>—i64.rem_s (signed) | DIV (unsigned) | —Yes | No | No | Yes | Yes | Yes | @rem | @mod | div, No | Euclidean |
In addition, many computer systems provide a IDIV instructions, the C programming language's a modulo d and d = 1 is particularly common.
There does not seem to be a standard notation for this operation, so let us tentatively use ''d''</sub> ''n''"}},"i":0}}]}'/>a modd n. We thus have the following definition: ''d''</sub> ''n''"}},"i":0}}]}'/>x = a modd n just in case a mod n = a mod0 n.
The operation of modulo with offset is related to the floor function as follows: x mod n = a mod n. It is in general true that b=−⌊a−dn⌋; but that means that k and 0 ≤ r ≤ n − 1 (see Euclidean division). Then 0 ≤ r ≤ n − 1 and add Mod[a, n, d] .
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:
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.
| View original on Wikipedia | Back | FazBrowse Home |