Modulo

From testwiki
Jump to navigation Jump to search

Template: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 Template:Math and Template:Math, Template:Math modulo Template:Math (often abbreviated as Template:Math) is the remainder of the Euclidean division of Template:Math by Template:Math, where Template:Math is the dividend and Template:Math 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 Template:Math and Template:Math both being integers, many computing systems now allow other types of numeric operands. The range of values for an integer modulo operation of Template:Math is 0 to Template:Math. Template:Math mod 1 is always 0, assuming Template:Math is an integer.

When exactly one of Template:Math or Template:Math 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 Template:Math and the remainder Template:Math of Template:Math divided by n≠0 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 Template:Math or Template:Math.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 Template:Math or Template:Math is negative (see the table under Template:Section link for details). Some systems leave Template:Math modulo 0 undefined, though others define it as Template:Math.

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

Template:About

Some calculators have a Template:Math function button, and many programming languages have a similar function, expressed as Template:Math, for example. Some also support expressions that use "%", "mod", or "Mod" as a modulo or remainder operator, such as Template:Code or Template:Code.

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 Template:Math (the dividend) is negative and odd, Template:Math 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 Template:Math is a positive integer, or using a non-truncating definition):

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

Examples:

Template:Code
Template:Code
Template:Code

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 Template:Code where Template:Code is a power of two and automatically implement them as Template:Code, 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 Template:Code 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)

Template:See also 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 Template:Math and Template:Math are integers.

In programming languages

Template:Sticky header

In addition, many computer systems provide a Template:Code functionality, which produces the quotient and the remainder at the same time. Examples include the x86 architecture's Template:Code and Template:Code instructions, the C programming language's Template:Code function, and Python's Template:Code function.

Generalizations

Modulo with offset

Sometimes it is useful for the result of Template:Mvar modulo Template:Mvar to lie not between 0 and Template:Math, but between some number Template:Mvar and Template:Math. In that case, Template:Mvar is called an offset and Template:Math is particularly common.

There does not seem to be a standard notation for this operation, so let us tentatively use Template:Math. We thus have the following definition:[119] Template:Math just in case Template:Math and Template:Math. Clearly, the usual modulo operation corresponds to zero offset: Template:Math.

The operation of modulo with offset is related to the floor function as follows: amoddn=a−n⌊a−dn⌋.

To see this, let x=a−n⌊a−dn⌋. We first show that Template:Math. It is in general true that Template:Math for all integers Template:Mvar; thus, this is true also in the particular case when b=−⌊a−dn⌋; but that means that xmodn=(a−n⌊a−dn⌋)modn=amodn, which is what we wanted to prove. It remains to be shown that Template:Math. Let Template:Mvar and Template:Mvar be the integers such that Template:Math with Template:Math (see Euclidean division). Then ⌊a−dn⌋=k, thus x=a−n⌊a−dn⌋=a−nk=d+r. Now take Template:Math and add Template:Mvar to both sides, obtaining Template:Math. But we've seen that Template:Math, so we are done.

The modulo with offset Template:Math is implemented in Mathematica as Template:Code .[119]

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

Template:Reflist

  • Modulorama, animation of a cyclic representation of multiplication tables (explanation in French)

de:Division mit Rest#Modulo

  1. ↑ Template:Cite web
  2. ↑ Template:Cite web
  3. ↑ Template:Cite web
  4. ↑ Template:Cite web
  5. ↑ Template:Cite web
  6. ↑ Template:Cite web
  7. ↑ Template:Cite web
  8. ↑ 8.0 8.1 Template:Cite book
  9. ↑ Template:Cite book
  10. ↑ Template:Cite web
  11. ↑ Template:Cite web
  12. ↑ Template:Cite book
  13. ↑ Template:Cite book
  14. ↑ Template:Cite web
  15. ↑ 15.0 15.1 Template:Cite web
  16. ↑ Template:Cite web
  17. ↑ Template:Cite web
  18. ↑ 18.0 18.1 Template:Cite book
  19. ↑ 19.0 19.1 Template:Cite web
  20. ↑ Template:Cite book
  21. ↑ 21.0 21.1 Template:Cite web
  22. ↑ Template:Cite web
  23. ↑ Template:Cite web
  24. ↑ Template:Cite web
  25. ↑ Template:Cite web
  26. ↑ Template:Cite web
  27. ↑ Template:Cite web
  28. ↑ Template:Cite web
  29. ↑ Template:Cite web
  30. ↑ Template:Cite web
  31. ↑ Template:Cite web
  32. ↑ Template:Cite web
  33. ↑ Template:Cite web
  34. ↑ Template:Cite web
  35. ↑ Template:Cite web
  36. ↑ Template:Cite web
  37. ↑ Template:Cite web
  38. ↑ Template:Cite web
  39. ↑ Template:Cite web
  40. ↑ Template:Cite web
  41. ↑ Template:Cite book
  42. ↑ Template:Cite book
  43. ↑ Template:Cite web
  44. ↑ Template:Cite web
  45. ↑ Template:Cite web
  46. ↑ Template:Cite web
  47. ↑ Template:Cite web
  48. ↑ Template:Cite web
  49. ↑ Template:Cite web
  50. ↑ Template:Cite web
  51. ↑ Template:Cite web
  52. ↑ Template:Cite web
  53. ↑ 53.0 53.1 Template:Cite web
  54. ↑ Template:Cite web
  55. ↑ Template:Cite web
  56. ↑ Template:Cite web
  57. ↑ Template:Cite web
  58. ↑ Template:Cite web
  59. ↑ Template:Cite web
  60. ↑ Template:Cite web
  61. ↑ Template:Cite web
  62. ↑ Template:Cite web
  63. ↑ Template:Cite web
  64. ↑ Template:Cite web
  65. ↑ Template:Cite web
  66. ↑ Template:Cite web
  67. ↑ Template:Cite web
  68. ↑ Template:Cite web
  69. ↑ Template:Cite web
  70. ↑ Template:Cite web
  71. ↑ Template:Cite web
  72. ↑ Template:Cite web
  73. ↑ Template:Cite web
  74. ↑ Template:Cite web
  75. ↑ Template:Cite web
  76. ↑ Template:Cite web
  77. ↑ Template:Cite web
  78. ↑ Template:Cite web
  79. ↑ Template:Cite web
  80. ↑ Template:Cite web
  81. ↑ Template:Cite web
  82. ↑ Template:Cite web
  83. ↑ Template:Cite web
  84. ↑ Template:Cite web
  85. ↑ Template:Cite web
  86. ↑ Template:Cite web
  87. ↑ Template:Cite book
  88. ↑ Template:Cite web
  89. ↑ Template:Cite web
  90. ↑ Template:Cite web
  91. ↑ Template:Cite web
  92. ↑ Template:Cite web
  93. ↑ Template:Cite web
  94. ↑ Template:Cite web
  95. ↑ Template:Cite web
  96. ↑ Template:Cite web
  97. ↑ Template:Cite web
  98. ↑ Template:Cite web
  99. ↑ Template:Cite web
  100. ↑ Template:Cite web
  101. ↑ Template:Cite web
  102. ↑ Template:Cite web
  103. ↑ Template:Cite web
  104. ↑ Template:Cite web
  105. ↑ Template:Cite web
  106. ↑ Template:Cite web
  107. ↑ Template:Cite web
  108. ↑ 108.0 108.1 r6rs.org
  109. ↑ Template:Cite web
  110. ↑ 110.0 110.1 Template:Cite book
  111. ↑ Template:Cite web
  112. ↑ Template:Cite web
  113. ↑ Template:Cite web
  114. ↑ Template:Cite web
  115. ↑ Template:Cite web
  116. ↑ Template:Cite web
  117. ↑ 117.0 117.1 Template:Cite web
  118. ↑ Template:Cite web
  119. ↑ 119.0 119.1 Template:Cite web