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 and , modulo (often abbreviated as ) is the remainder of the Euclidean division of by , where is the dividend and is the divisor.
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 and both being , many computing systems now allow other types of numeric operands. The range of values for an integer modulo operation of is 0 to . mod 1 is always 0.
When exactly one of or is negative, the basic definition breaks down, and programming languages differ in how these values are defined.
In nearly all computing systems, the quotient and the remainder of divided by satisfy the following conditions:
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 or . 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 or is negative (see the table under for details). Some systems leave modulo 0 undefined, though others define it as .
< |n|. (Emphasis added.)
Under this definition, we can say the following about the quotient :
where is the sign function, is the floor function (rounding down), and , are rational numbers.
Equivalently, one may instead define the quotient as follows: where is the ceiling function (rounding up). Thus according to equation (), the remainder is non-negative:
|Common Lisp and IEEE 754 use rounded division, for which the quotient is defined by where is the Rounding (rounding half to even). Thus according to equation (), the remainder falls between and , and its sign depends on which side of zero it falls to be within these boundaries:
|Common Lisp also uses ceiling division, for which the quotient is defined by where ⌈⌉ is the ceiling function (rounding up). Thus according to equation (), 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 .
For environments lacking a similar function, any of the three definitions above can be used.
For example, to test if an integer is odd, one might be inclined to test if the remainder by 2 is equal to 1:
return n % 2 == 1;
}
But in a language where modulo has the sign of the dividend, that is incorrect, because when (the dividend) is negative and odd, 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):
return n % 2 != 0;
}
Or with the binary arithmetic:
return n & 1;
}
Examples:
In devices and software that implement bitwise operations more efficiently than modulo, these alternative forms can result in faster calculations.
Compiler optimizations may recognize expressions of the form where is a power of two and automatically implement them as , 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), unless the dividend is of an Signedness integer type. This is because, if the dividend is negative, the modulo will be negative, whereas will always be positive. For these languages, the equivalence x % 2<sup>n</sup> == x < 0 ? x | ~(2<sup>n</sup> - 1) : x & (2<sup>n</sup> - 1) 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 constant-divisor optimization.
| + Modulo operators in various programming languages | ||||
| ABAP | Euclidean | |||
| ActionScript | Truncated | |||
| Ada | Floored | |||
| Truncated | ||||
| ALGOL 68 | , , , , '''mod''' | Euclidean | ||
| AMPL | Truncated | |||
| APL | <nowiki></nowiki> | Floored | ||
| AppleScript | Truncated | |||
| AutoLISP | Truncated | |||
| AWK | Truncated (same as in C) | |||
| BASIC | Varies by implementation | |||
| bc | Truncated | |||
| CC++ | , | Truncated | ||
| (C) (C++) | Truncated | |||
| (C) (C++) | Rounded | |||
| C# | Truncated | |||
| Rounded | ||||
| Clarion | Truncated | |||
| Clean | Truncated | |||
| Clojure | Floored | |||
| Truncated | ||||
| COBOL | Floored | |||
| Truncated | ||||
| CoffeeScript | Truncated | |||
| Floored CoffeeScript operators | ||||
| ColdFusion | , | Truncated | ||
| Common Intermediate Language | (signed) | Truncated | ||
| (unsigned) | ||||
| Common Lisp | Floored | |||
| Truncated | ||||
| Crystal | , | Floored | ||
| Truncated | ||||
| CSS | Floored | |||
| Truncated | ||||
| D | Truncated | |||
| Dart | Euclidean | |||
| Truncated | ||||
| Eiffel | Truncated | |||
| Elixir | Truncated | |||
| Floored | ||||
| Elm | Floored | |||
| Truncated | ||||
| Erlang | Truncated | |||
| Truncated (same as C) | ||||
| Euphoria | Truncated | |||
| Floored | ||||
| F# | Truncated (same as C#) | |||
| Rounded | ||||
| Factor | Truncated | |||
| Euclidean | ||||
| FileMaker | Floored | |||
| Forth | Implementation defined | |||
| Floored | ||||
| Truncated | ||||
| Fortran | Truncated | |||
| Floored | ||||
| Frink | Floored | |||
| Full BASIC | Floored | |||
| Truncated | ||||
| GLSL | Undefined | |||
| Floored | ||||
| GameMaker Studio (GML) | , | Truncated | ||
| GDScript (Godot) | Truncated | |||
| Euclidean | ||||
| Truncated | ||||
| Euclidean | ||||
| Go | Truncated | |||
| Truncated | ||||
| Euclidean | ||||
| Truncated | ||||
| Apache Groovy | Truncated | |||
| Haskell | Floored | |||
| Truncated | ||||
| (GHC) | Floored | |||
| Haxe | Truncated | |||
| HLSL | Undefined | |||
| J | <nowiki></nowiki> | Floored | ||
| Java | Truncated | |||
| Floored | ||||
| JavaScriptTypeScript | Truncated | |||
| Julia | Floored | |||
| , | Truncated | |||
| Kotlin | , | Truncated | ||
| Floored | ||||
| KornShell | Truncated (same as POSIX ) | |||
| Truncated | ||||
| LabVIEW | Truncated | |||
| LibreOffice | Floored | |||
| Logo | Floored | |||
| Truncated | ||||
| Lua 5 | Floored | |||
| Lua 4 | Truncated | |||
| Liberty BASIC | Truncated | |||
| Mathcad | Floored | |||
| Maple | (by default), | Euclidean | ||
| Rounded | ||||
| Rounded | ||||
| Mathematica | Floored | |||
| MATLAB | Floored | |||
| Truncated | ||||
| Maxima | Floored | |||
| Truncated | ||||
| Maya Embedded Language | Truncated | |||
| Microsoft Excel | Floored | |||
| Minitab | Floored | |||
| Modula-2 | Floored | |||
| Truncated | ||||
| MUMPS | Floored | |||
| Netwide Assembler (NASM, NASMX) | , (unsigned) | |||
| (signed) | Implementation-defined | |||
| Nim | Truncated | |||
| Oberon | Floored-like | |||
| Objective-C | Truncated (same as C99) | |||
| Object Pascal, Delphi | Truncated | |||
| OCaml | Truncated | |||
| Truncated | ||||
| Occam | Truncated | |||
| Pascal (ISO-7185 and -10206) | Euclidean-like | |||
| Perl | Floored | |||
| Truncated | ||||
| PHP | Truncated | |||
| Truncated | ||||
| PIC BASIC Pro | Truncated | |||
| PL/I | Floored (ANSI PL/I) | |||
| PowerShell | Truncated | |||
| Programming Code (PRC) | Undefined | |||
| Progress | Truncated | |||
| Prolog ( ISO 1995) | Floored | |||
| Truncated | ||||
| PureBasic | , | Truncated | ||
| PureScript | Euclidean | |||
| Pure Data | Truncated (same as C) | |||
| Floored | ||||
| Python | Floored | |||
| Truncated | ||||
| Rounded | ||||
| Q Sharp | Truncated | |||
| R | Floored | |||
| Racket | Floored | |||
| Truncated | ||||
| Raku | Floored | |||
| RealBasic | Truncated | |||
| Reason | Truncated | |||
| Rexx | Truncated | |||
| RPG | Truncated | |||
| Ruby | , | Floored | ||
| Truncated | ||||
| Rust | Truncated | |||
| Euclidean | ||||
| SAS language | Truncated | |||
| Scala | Truncated | |||
| Scheme | Floored | |||
| Truncated | ||||
| Scheme R6RS | Euclidean r6rs.org | |||
| Rounded | ||||
| Euclidean | ||||
| Rounded | ||||
| Scratch | Floored | |||
| Seed7 | Floored | |||
| Truncated | ||||
| SenseTalk | Floored | |||
| Truncated | ||||
| POSIX shell (includes bash, mksh, &c.) | Truncated (same as C) | |||
| Smalltalk | Floored | |||
| Truncated | ||||
| Snap! | Floored | |||
| Spin | Floored | |||
| Solidity | Truncated | |||
| SQL () | Truncated | |||
| SQL () | Truncated | |||
| Standard ML | Floored | |||
| Truncated | ||||
| Truncated | ||||
| Stata | Euclidean | |||
| Swift | Truncated | |||
| Rounded | ||||
| Truncated | ||||
| Tcl | Floored | |||
| Truncated (same as C) | ||||
| tcsh | Truncated | |||
| Torque | Truncated | |||
| Turing | Floored | |||
| Verilog (2001) | Truncated | |||
| VHDL | Floored | |||
| Truncated | ||||
| Vimscript | Truncated | |||
| Visual Basic | Truncated | |||
| WebAssembly | , (unsigned) | |||
| , (signed) | Truncated | |||
| x86 assembly | Truncated | |||
| XBase++ | Truncated | |||
| Floored | ||||
| Zig | , | Truncated | ||
| Floored | ||||
| Z3 theorem prover | , | Euclidean |
In addition, many computer systems provide a functionality, which produces the quotient and the remainder at the same time. Examples include the x86 architecture's instruction, the C programming language's function, and Python's function.
There does not seem to be a standard notation for this operation, so let us tentatively use . We thus have the following definition: just in case and . Clearly, the usual modulo operation corresponds to zero offset: .
The operation of modulo with offset is related to the floor function as follows:
To see this, let . We first show that . It is in general true that for all integers ; thus, this is true also in the particular case when ; but that means that , which is what we wanted to prove. It remains to be shown that . Let and be the integers such that with (see Euclidean division). Then , thus . Now take and add to both sides, obtaining . But we've seen that , so we are done.
The modulo with offset is implemented in Mathematica as .
/* Euclidean division */
inline ldiv_t ldivE(long numer, long denom) {
/* Floored division */
inline ldiv_t ldivF(long numer, long denom) {
/* This structure is part of the C stdlib.h, but is reproduced here for clarity */
long int quot;
long int rem;
} ldiv_t;
/* 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};
}
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};
}
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.
|
|