Product Code Database
Example Keywords: final fantasy -jacket $4-158
barcode-scavenger
   » » Wiki: Modulo Operation
Tag Wiki 'Modulo Operation'.
Tag
In , the modulo operation finds the after division of one number by another (sometimes called modulus).

Given two positive numbers, (the dividend) and (the ), modulo (abbreviated as ) is the remainder of the Euclidean division of by . For example, the expression "5 mod 2" would evaluate to 1 because 5 divided by 2 leaves a of 2 and a remainder of 1, while "9 mod 3" would evaluate to 0 because the division of 9 by 3 has a quotient of 3 and leaves a remainder of 0; there is nothing to subtract from 9 after multiplying 3 times 3. (Note that doing the division with a calculator will not show the result referred to here by this operation; the quotient will be expressed as a decimal fraction.)

Although typically performed with and both being integers, many computing systems allow other types of numeric operands. The range of numbers for an integer modulo of is 0 to . ( mod 1 is always 0; is undefined, possibly resulting in a division by zero error in programming languages.) See modular arithmetic for an older and related convention applied in .

When either or is negative, the naive definition breaks down and programming languages differ in how these values are defined.


Remainder calculation for the modulo operation
+ Integer modulo operators in various programming languages
MODPositive always
%Dividend
AdamodDivisor
remDividend
ALGOL 68÷×, modPositive always
modDividend
APL<nowiki></nowiki>Divisor
modDividend
(rem d n)Remainder
%Dividend
ModUndefined
bash%Dividend
bc%Dividend
C (ISO 1990)%Implementation-defined
divDividend
C++ (ISO 1998)%Implementation-defined. "the binary % operator yields the remainder from the division of the first expression by the second. .... If both operands are nonnegative then the remainder is nonnegative; if not, the sign of the remainder is implementation-defined".
divDividend
C (ISO 1999)%, divDividend open-std.org, section 6.5.5
C++ (ISO 2011)%, divDividend
C#%Dividend
Clarion%Dividend
modDivisor
remDividend
FUNCTION&nbsp;MODDivisor
%Dividend
%%Divisor CoffeeScript operators
%, MODDividend
modDivisor
remDividend
Construct 2%
D%Dividend
Dart%Positive always
remainder()Dividend
Eiffel\\Dividend
ErlangremDividend
EuphoriamodDivisor
remainderDividend
F#%Dividend
ModDivisor
Forthmodimplementation defined
modDividend
moduloDivisor
FrinkmodDivisor
(GML)mod, %Dividend
GDScript%Dividend
Go%Dividend
HaskellmodDivisor
remDividend
%Dividend
J<nowiki></nowiki>Divisor
Java%Dividend
Math.floorModDivisor
%Dividend
JuliamodDivisor
remDividend
modDividend
=MOD()Divisor
Lua 5%Divisor
Lua 4mod(x,y)Divisor
MODDividend
mod(x,y)Divisor
Maplee mod mPositive always
Mod[a, b]Divisor
modDivisor
remDividend
MaximamodDivisor
remainderDividend
Maya Embedded Language%Dividend
=MOD()Divisor
MODDivisor
%Dividend
Modula-2MODDivisor
REMDividend
#Divisor
Netwide Assembler (NASM, NASMX)%Modulo operator unsigned
%%Modulo operator signed
OberonMODDivisor
, DelphimodDividend
modDividend
Occam\Dividend
Pascal (ISO-7185 and -10206)modPositive always
%Divisor
%Dividend
PIC Pro\\Dividend
PL/ImodDivisor (ANSI PL/I)
%Dividend
ProgressmoduloDividend
(ISO 1995)modDivisor
remDividend
%,Mod(x,y)Dividend
Python%Divisor
math.fmodDividend
RacketremainderDividend
MODDividend
R%%Divisor
//Dividend
RPG%REMDividend
Ruby%, modulo()Divisor
remainder()Dividend
Rust%Dividend
Scala%Dividend
SchememoduloDivisor
remainderDividend
Scheme R6RSmodPositive always r6rs.org
mod0Nearest to zero
Seed7modDivisor
remDividend
moduloDivisor
remDividend
\\Divisor
rem:Dividend
Spin//Divisor
()mod(x,y)Dividend
SQL (SQL:2012)%Dividend
modDivisor
Int.remDividend
mod(x,y)Positive always
Swift%Dividend
%Divisor
Torque%Dividend
TuringmodDivisor
(2001)%Dividend
modDivisor
remDividend
%Dividend
ModDividend
x86 assemblyIDIVDividend
XBase++%Dividend
Mod()Divisor
Z3 theorem proverdiv, modPositive always
+ Floating-point modulo operators in various programming languages
MODPositive always
C (ISO 1990)fmodDividend "The fmod function returns the value x - i * y, for some integer i such that, if y is nonzero, the result as the same sign as x and magnitude less than the magnitude of y.".
C (ISO 1999)fmodDividend
remainderNearest to zero
C++ (ISO 1998)std::fmodDividend
C++ (ISO 2011)std::fmodDividend
std::remainderNearest to zero
C#%Dividend
modDivisor
remDividend
D%Dividend
Dart%Positive always
remainder()Dividend
F#%Dividend
modDividend
moduloDivisor
Gomath.ModDividend
Haskell (GHC)Data.Fixed.mod'Divisor
Java%Dividend
%Dividend
modDividend
=MOD()Divisor
mod_floatDividend
POSIX::fmodDividend
Perl6%Divisor
fmodDividend
Python%Divisor
math.fmodDividend
//Dividend
Ruby%, modulo()Divisor
remainder()Dividend
Scheme R6RSflmodPositive always
flmod0Nearest to zero
Real.remDividend
SwifttruncatingRemainder(dividingBy:)Dividend
XBase++%Dividend
Mod()Divisor
In , the result of the modulo operation is the remainder of the Euclidean division. 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 and the remainder of divided by satisfy

However, this still leaves a sign ambiguity if the remainder is nonzero: two possible choices for the remainder occur, one negative and the other positive, and two possible choices for the quotient occur. Usually, in number theory, the positive remainder is always chosen, but programming languages choose depending on the language and the signs of or . Standard Pascal and ALGOL 68 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 for details. modulo 0 is undefined in most systems, although some do define it as .

As described by Leijen,


Common pitfalls
When the result of a modulo operation has the sign of the dividend, it can lead to surprising mistakes.

For example, to test if an integer is odd, one might be inclined to test if the remainder by 2 is equal to 1:

bool is_odd(int n) {

   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 it is not 0 (because remainder 0 is the same regardless of the signs):

bool is_odd(int n) {

   return n % 2 != 0;
     
}

Or, by understanding in the first place that for any odd number, the modulo remainder may be either 1 or −1:

bool is_odd(int n) {

   return n % 2 == 1 || n % 2 == -1;
     
}


Notation
Some calculators have a function button, and many programming languages have a similar function, expressed as , for example. Some also support expressions that use "%", "mod", or "Mod" as a modulo or remainder operator, such as
a % n
or
a mod n
or equivalent, for environments lacking a function (note that 'int' inherently produces the truncated value of )
a - (n * int(a/n))


Performance issues
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:
x % 2<sup>n</sup> == x & (2<sup>n</sup> - 1)

Examples (assuming is a positive integer):

x % 2 == x & 1
x % 4 == x & 3
x % 8 == x & 7

In devices and software that implement bitwise operations more efficiently than modulo, these alternative forms can result in faster calculations.

Optimizing may recognize expressions of the form expression % constant where constant is a power of two and automatically implement them as expression & (constant-1). This can allow writing clearer code without compromising performance. This 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 integer type. This is because, if the dividend is negative, the modulo will be negative, whereas expression &amp; (constant-1) will always be positive.


Equivalencies
Some modulo operations can be factored or expanded similar to other mathematical operations. This may be useful in proofs, such as the Diffie–Hellman key exchange.
  • Identity:
    • .
    • for all positive integer values of .
    • If is a which is not a of , then , due to Fermat's little theorem.
  • Inverse:
    • .
    • denotes the modular multiplicative inverse, which is defined if and only if and are , which is the case when the left hand side is defined: .
  • Distributive:
    • .
    • .
  • Division (definition): , when the right hand side is defined (that is when and are ). Undefined otherwise.
  • Inverse multiplication: .


See also
  • Modulo (disambiguation) and modulo (jargon) – many uses of the word modulo, all of which grew out of Carl F. Gauss's introduction of modular arithmetic in 1801.
  • Modular exponentiation


Notes
  • Perl usually uses arithmetic modulo operator that is machine-independent. For examples and exceptions, see the Perl documentation on multiplicative operators. Perl documentation
  • Mathematically, these two choices are but two of the infinite number of choices available for the inequality satisfied by a remainder.
  • Divisor must be positive, otherwise undefined.
  • As implemented in ACUCOBOL, Micro Focus COBOL, and possible others.
  • Argument order reverses, i.e., α|ω computes \omega\bmod\alpha, the remainder when dividing ω by α.

Page 1 of 1
1
Page 1 of 1
1

Account

Social:
Pages:  ..   .. 
Items:  .. 

Navigation

General: Atom Feed Atom Feed  .. 
Help:  ..   .. 
Category:  ..   .. 
Media:  ..   .. 
Posts:  ..   ..   .. 

Statistics

Page:  .. 
Summary:  .. 
1 Tags
10/10 Page Rank
5 Page Refs
1s Time