Modulo

From Wikipedia, the free encyclopedia
(Redirected from Mod function)
Jump to navigation Jump to search

Script error: No such module "about". Template:Short description 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 aScript error: No such module "Check for unknown parameters". and nScript error: No such module "Check for unknown parameters"., aScript error: No such module "Check for unknown parameters". modulo nScript error: No such module "Check for unknown parameters". (often abbreviated as a mod nScript error: No such module "Check for unknown parameters".) is the remainder of the Euclidean division of aScript error: No such module "Check for unknown parameters". by nScript error: No such module "Check for unknown parameters"., where aScript error: No such module "Check for unknown parameters". is the dividend and nScript error: No such module "Check for unknown parameters". 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 aScript error: No such module "Check for unknown parameters". and nScript error: No such module "Check for unknown parameters". both being integers, many computing systems now allow other types of numeric operands. The range of values for an integer modulo operation of nScript error: No such module "Check for unknown parameters". is 0 to n − 1Script error: No such module "Check for unknown parameters".. aScript error: No such module "Check for unknown parameters". mod 1 is always 0.

When exactly one of aScript error: No such module "Check for unknown parameters". or nScript error: No such module "Check for unknown parameters". is negative, the basic definition breaks down, and programming languages differ in how these values are defined.

Variants of the definition

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 qScript error: No such module "Check for unknown parameters". and the remainder rScript error: No such module "Check for unknown parameters". of aScript error: No such module "Check for unknown parameters". divided by n0 satisfy the following conditions: Template:NumBlk

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 aScript error: No such module "Check for unknown parameters". or nScript error: No such module "Check for unknown parameters"..Template:Efn 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 nScript error: No such module "Check for unknown parameters". or aScript error: No such module "Check for unknown parameters". is negative (see the table under Template:Section link for details). Some systems leave aScript error: No such module "Check for unknown parameters". modulo 0 undefined, though others define it as aScript error: No such module "Check for unknown parameters"..

Template:Bulleted list

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 (a)/b=(a/b)=a/(b).[3][4]

Notation

Script error: No such module "about".

Some calculators have a mod()Script error: No such module "Check for unknown parameters". function button, and many programming languages have a similar function, expressed as mod(a, n)Script error: No such module "Check for unknown parameters"., 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.

For environments lacking a similar function, any of the three definitions above can be used.

Common pitfalls

When the result of a modulo operation has the sign of the dividend (truncated definition), 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 nScript error: No such module "Check for unknown parameters". (the dividend) is negative and odd, nScript error: No such module "Check for unknown parameters". 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):

bool is_odd(int n) {
    return n % 2 != 0;
}

Or with the binary arithmetic:

bool is_odd(int n) {
    return n & 1;
}

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 (assuming xScript error: No such module "Check for unknown parameters". is a positive integer, or using a non-truncating definition):

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

Examples:

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.[5]

Compiler optimizations may recognize expressions of the form expression % constant where constant is a power of two and automatically implement them as expression & (constant-1), 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 unsigned integer type. This is because, if the dividend is negative, the modulo will be negative, whereas expression & (constant-1) will always be positive. For these languages, the equivalence x % 2n == x < 0 ? x | ~(2n - 1) : x & (2n - 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.

Properties (identities)

Script error: No such module "Labelled list hatnote". Some modulo operations can be factored or expanded similarly to other mathematical operations. This may be useful in cryptography proofs, such as the Diffie–Hellman key exchange. The properties involving multiplication, division, and exponentiation generally require that aScript error: No such module "Check for unknown parameters". and nScript error: No such module "Check for unknown parameters". are integers.

  • Identity:
    • (a mod n) mod n = a mod nScript error: No such module "Check for unknown parameters"..
    • nx mod n = 0Script error: No such module "Check for unknown parameters". for all positive integer values of xScript error: No such module "Check for unknown parameters"..
    • If pScript error: No such module "Check for unknown parameters". is a prime number which is not a divisor of bScript error: No such module "Check for unknown parameters"., then abp−1 mod p = a mod pScript error: No such module "Check for unknown parameters"., due to Fermat's little theorem.
  • Inverse:
    • [(−a mod n) + (a mod n)] mod n = 0Script error: No such module "Check for unknown parameters"..
    • b−1 mod nScript error: No such module "Check for unknown parameters". denotes the modular multiplicative inverse, which is defined if and only if bScript error: No such module "Check for unknown parameters". and nScript error: No such module "Check for unknown parameters". are relatively prime, which is the case when the left hand side is defined: [(b−1 mod n)(b mod n)] mod n = 1Script error: No such module "Check for unknown parameters"..
  • Distributive:
    • (a + b) mod n = [(a mod n) + (b mod n)] mod nScript error: No such module "Check for unknown parameters"..
    • ab mod n = [(a mod n)(b mod n)] mod nScript error: No such module "Check for unknown parameters"..
  • Division (definition): Template:Sfrac mod n = [(a mod n)(b−1 mod n)] mod nScript error: No such module "Check for unknown parameters"., when the right hand side is defined (that is when bScript error: No such module "Check for unknown parameters". and nScript error: No such module "Check for unknown parameters". are coprime), and undefined otherwise.
  • Inverse multiplication: [(ab mod n)(b−1 mod n)] mod n = a mod nScript error: No such module "Check for unknown parameters"..

In programming languages

Template:Sticky header

In addition, many computer systems provide a divmod functionality, which produces the quotient and the remainder at the same time. Examples include the x86 architecture's IDIV instruction, the C programming language's div() function, and Python's divmod() function.

Generalizations

Modulo with offset

Sometimes it is useful for the result of Template:Mvar modulo Template:Mvar to lie not between 0 and n − 1Script error: No such module "Check for unknown parameters"., but between some number Template:Mvar and d + n − 1Script error: No such module "Check for unknown parameters".. In that case, Template:Mvar is called an offset and d = 1Script error: No such module "Check for unknown parameters". is particularly common.

There does not seem to be a standard notation for this operation, so let us tentatively use a modd nScript error: No such module "Check for unknown parameters".. We thus have the following definition:[55] x = a modd nScript error: No such module "Check for unknown parameters". just in case dxd + n − 1Script error: No such module "Check for unknown parameters". and x mod n = a mod nScript error: No such module "Check for unknown parameters".. Clearly, the usual modulo operation corresponds to zero offset: a mod n = a mod0 nScript error: No such module "Check for unknown parameters"..

The operation of modulo with offset is related to the floor function as follows:

amoddn=anadn.

To see this, let x=anadn. We first show that x mod n = a mod nScript error: No such module "Check for unknown parameters".. It is in general true that (a + bn) mod n = a mod nScript error: No such module "Check for unknown parameters". for all integers Template:Mvar; thus, this is true also in the particular case when b=adn; but that means that xmodn=(anadn)modn=amodn, which is what we wanted to prove. It remains to be shown that dxd + n − 1Script error: No such module "Check for unknown parameters".. Let Template:Mvar and Template:Mvar be the integers such that ad = kn + rScript error: No such module "Check for unknown parameters". with 0 ≤ rn − 1Script error: No such module "Check for unknown parameters". (see Euclidean division). Then adn=k, thus x=anadn=ank=d+r. Now take 0 ≤ rn − 1Script error: No such module "Check for unknown parameters". and add Template:Mvar to both sides, obtaining dd + rd + n − 1Script error: No such module "Check for unknown parameters".. But we've seen that x = d + rScript error: No such module "Check for unknown parameters"., so we are done.

The modulo with offset a modd nScript error: No such module "Check for unknown parameters". is implemented in Mathematica as Mod[a, n, d] .[55]

Implementing other modulo definitions using truncation

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:

/* Euclidean and Floored divmod, in the style of C's ldiv() */
typedef struct {
  /* This structure is part of the C stdlib.h, but is reproduced here for clarity */
  long int quot;
  long int rem;
} ldiv_t;

/* Euclidean division */
inline ldiv_t ldivE(long numer, long denom) {
  /* 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};
}

/* Floored division */
inline ldiv_t ldivF(long numer, long denom) {
  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.

See also

Notes

Template:Notelist

References

<templatestyles src="Reflist/styles.css" />

  1. Script error: No such module "citation/CS1".
  2. Script error: No such module "citation/CS1".
  3. Script error: No such module "citation/CS1".
  4. Script error: No such module "citation/CS1".
  5. Script error: No such module "citation/CS1".
  6. a b Script error: No such module "citation/CS1".
  7. Script error: No such module "citation/CS1".
  8. a b Script error: No such module "citation/CS1".
  9. Script error: No such module "citation/CS1".
  10. Script error: No such module "citation/CS1".
  11. a b Script error: No such module "citation/CS1".
  12. CoffeeScript operators
  13. Script error: No such module "citation/CS1".
  14. Script error: No such module "citation/CS1".
  15. Script error: No such module "citation/CS1".
  16. Script error: No such module "citation/CS1".
  17. Script error: No such module "citation/CS1".
  18. Script error: No such module "citation/CS1".
  19. Script error: No such module "citation/CS1".
  20. Script error: No such module "citation/CS1".
  21. Script error: No such module "citation/CS1".
  22. Script error: No such module "citation/CS1".
  23. Script error: No such module "citation/CS1".
  24. Script error: No such module "citation/CS1".
  25. Script error: No such module "citation/CS1".
  26. Script error: No such module "citation/CS1".
  27. Script error: No such module "citation/CS1".
  28. Script error: No such module "citation/CS1".
  29. Script error: No such module "citation/CS1".
  30. Script error: No such module "citation/CS1".
  31. Script error: No such module "citation/CS1".
  32. a b Script error: No such module "citation/CS1".
  33. Script error: No such module "citation/CS1".
  34. Script error: No such module "citation/CS1".
  35. Script error: No such module "citation/CS1".
  36. Script error: No such module "citation/CS1".
  37. Script error: No such module "citation/CS1".
  38. Script error: No such module "citation/CS1".
  39. Script error: No such module "citation/CS1".
  40. Script error: No such module "citation/CS1".
  41. Script error: No such module "citation/CS1".
  42. Script error: No such module "citation/CS1".
  43. Script error: No such module "citation/CS1".
  44. Script error: No such module "citation/CS1".
  45. Script error: No such module "citation/CS1".
  46. Script error: No such module "citation/CS1".
  47. a b r6rs.org
  48. Script error: No such module "citation/CS1".
  49. Script error: No such module "citation/CS1".
  50. Script error: No such module "citation/CS1".
  51. Script error: No such module "citation/CS1".
  52. Script error: No such module "citation/CS1".
  53. a b Script error: No such module "citation/CS1".
  54. Script error: No such module "citation/CS1".
  55. a b Script error: No such module "citation/CS1".

Script error: No such module "Check for unknown parameters".

External links

de:Division mit Rest#Modulo