BookRags.com Literature Guides Literature
Guides
Criticism & Essays Criticism &
Essays
Questions & Answers Questions &
Answers
Lesson Plans Lesson
Plans
My Bibliography Periodic Table U.S. Presidents Shakespeare Sonnet Shake-Up
Research Anything:        
History | Encyclopedias | Films | News | Create a Bibliography | More... Login | Register | Help
Not What You Meant?  There are 32 definitions for Mod.  Also try: REM or Modulus.

Modulo operation

Print-Friendly
About 4 pages (1,059 words)

Bookmark and Share Questions on this topic? Just ask!

In computing, the modulo operation finds the remainder of division of one number by another. Given two numbers, a (the dividend) and n (the divisor), a modulo n (abbreviated as a mod n) is the remainder, on division of a by n. For instance, the expression "7 mod 3" would evaluate to 1, while "9 mod 3" would evaluate to 0. Although typically performed with a and n both being integers, many computing systems allow other types of numeric operands. See modular arithmetic for an older and related convention applied in number theory.

Contents

Remainder calculation for the modulo operation

Modulo operators in various programming languages
Language Operator Result has the same sign as
ActionScript % Dividend
Ada mod Divisor
rem Dividend
ASP Mod Not defined
C (ISO 1990) % Not defined
C (ISO 1999) % Dividend
C++ % Not defined[1]
C# % Dividend
ColdFusion MOD Dividend
Common Lisp mod Divisor
rem Dividend
Eiffel \\ Dividend
Fortran mod Dividend
modulo Divisor
Game_Maker_Language mod Dividend
div Divisor
J |~ Divisor
Java % Dividend
JavaScript % Dividend
Lua % Divisor
MATLAB mod Divisor
rem Dividend
MySQL MOD
%
Dividend
Objective Caml mod Not defined
Pascal (Delphi) mod Dividend
Perl % Divisor[1]
PHP % Dividend
PL/I mod Divisor (ANSI PL/I)
Prolog (ISO 1995) mod Divisor
Python % Divisor
RPG %REM Dividend
Ruby % Divisor
SenseTalk modulo Divisor
rem Dividend
Verilog (2001) % Dividend
VHDL mod Divisor
rem Dividend
Visual Basic Mod Dividend

There are various ways of defining a remainder, and computers and calculators have various ways of storing and representing numbers, so what exactly constitutes the result of a modulo operation depends on the programming language and/or the underlying hardware. In nearly all computing systems, the quotient resulting from the division is constrained to the set of integers, and the remainder r is typically constrained by either <math>0 \leq r < |n|</math> or <math>-|n| < r \leq 0</math>. The choice between the two possible remainders depends on the signs of a and n and the programming language being used.[2] Some programming languages, such as C89, don't even define a result if either of n or a is negative. See the table for details. a modulo 0 is undefined in the majority of systems, although some do define it to be a. If the definition is consistent with the division algorithm, then n = 0 implies <math> 0 \leq r < 0</math>, which is a contradiction (i.e., the usual remainder does not exist in this case). The remainder can be calculated by using equations, in terms of other functions. Differences may arise according to the scope of the variables, which in common implementations is broader than in the definition just given. One useful equation for calculating the remainder r is

<math>r = a - n \left\lfloor {a \over n} \right\rfloor</math>

where <math>\lfloor x\rfloor</math> is the floor function of x. See e.g. [3], [4], [5]. Raymond T. Boute[2] analyzed several definitions of integer division and modulo, and he introduces the “Euclidean” definition. Let q be the integer quotient of a and n, then:

<math>q \in \mathbb{Z}</math>
<math>a = n \times q + r</math>
<math>0 \leq r < |n|.</math>

Two corollaries are that

<math>n > 0 \to q = \left\lfloor a \div n \right\rfloor</math>
<math>n < 0 \to q = \left\lceil a \div n \right\rceil.</math>

As described by Leijen,[3]

Boute argues that Euclidean division is superior to the other ones in terms of regularity and useful mathematical properties, although floored division, promoted by Knuth, is also a good definition. Despite its widespread use, truncated division is shown to be inferior to the other definitions.

Modulo operation expression

Some calculators have a mod() function button, and many programming languages have a mod() function or similar, expressed as mod(a,n), 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

Performance issues

Modulo operations might be implemented such that division with remainder is calculated each time. For special cases, there are faster alternatives on some hardware. For example, the modulus of powers of 2 can alternatively be expressed as a bitwise AND operation:

x % 2n == x & (2n - 1)

Examples (assuming x is an 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. In the C Programming Language, compiling with heavy speed optimizations will automatically convert modulo operations to bitwise AND in the assembly file. In some compilers, the modulo operation is implemented as mod(a, n) = a - n * floor(a / n). When performing both modulo and division on the same numbers, one can get the same result somewhat more efficiently by avoiding the actual modulo operator, and using the formula above on the result, avoiding an additional division operation.

See also

Notes

References

  1. ^ ISO/IEC 14882:2003 : Programming languages -- C++, 5.6.4: ISO, IEC, 2003. "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".
  2. ^ Boute, Raymond T. (April 1992). "The Euclidean definition of the functions div and mod". ACM Transactions on Programming Languages and Systems (TOPLAS) 14 (2): 127 – 144. ACM Press (New York, NY, USA).
  3. ^ Leijen, Daan (December 3, 2001). Division and Modulus for Computer Scientists (PDF). Retrieved on 2006-08-27.

View More Summaries on Modulo operation
 
Ask any question on Modulo operation and get it answered FAST!
Answer questions in BookRags Q&A and earn points toward
discounted or even FREE Study Guides and other BookRags products!
Learn more about BookRags Q&A
Copyrights
Modulo operation from Wíkipedia. ©2006 by Wíkipedia. Licensed under the GNU Free Documentation License. View a list of authors or edit this article.

Article Navigation
Join BookRagslearn moreJoin BookRags




About BookRags | Customer Service | Report an Error | Terms of Use | Privacy Policy