# Modulus

### From ENIGMA

Line 1: | Line 1: | ||

- | Modulus is an | + | Modulus is an arithmetic operator that is useful in many common algorithms. The modulus expression [code]a % b[/code] gives the remainder after performing the division [code]a / b[/code]. More technically, it returns [code]c[/code] such that [code]a = K*b + c[/code], where [code]K[/code] is an integer. As an example, [code]10 % 3[/code] returns [code]1[/code], as [code]10 / 3[/code] is [code]9[/code] with a remainder of [code]1[/code]. |

- | + | An algorithmic example of the use of modulus is computing the time a certain number of hours after a given time. Take, for example, computing the time five hours in the future. If the current time is 11:00, adding five hours yields 4:00. | |

+ | |||

<source lang="edl"> | <source lang="edl"> | ||

show_message("5 hours from 11:00 is " + string((11 + 5) % 12) + ":00"); | show_message("5 hours from 11:00 is " + string((11 + 5) % 12) + ":00"); | ||

Line 9: | Line 10: | ||

</source> | </source> | ||

- | + | For this simple example, you could compute this using a simple conditional statement, but modulus allows doing it in the general case. For instance, if the time is 23:00, but you want the time in five hours on a twelve-hour clock, the modulus example will still work, while simply conditionally subtracting twelve would give you 16:00 (28 − 12 = 16, but 28 % 12 is 4). | |

+ | |||

+ | Another simple example is determining whether a number is even or odd. | ||

<source lang="edl"> | <source lang="edl"> | ||

if (number % 2 == 0) { | if (number % 2 == 0) { | ||

Line 18: | Line 21: | ||

</source> | </source> | ||

- | It may also be useful to perform some operation every nth iteration of a loop. The following loop will display every | + | It may also be useful to perform some operation every nth iteration of a loop. The following loop will display every third number between zero and fifty: |

<source lang="edl"> | <source lang="edl"> | ||

- | for (i = | + | for (i = 1; i < 50; i++) { |

- | if (i % 3 == | + | if (i % 3 == 0) { |

show_message(string(i)); | show_message(string(i)); | ||

} | } | ||

} | } | ||

</source> | </source> |

## Revision as of 05:45, 27 December 2014

Modulus is an arithmetic operator that is useful in many common algorithms. The modulus expression [code]a % b[/code] gives the remainder after performing the division [code]a / b[/code]. More technically, it returns [code]c[/code] such that [code]a = K*b + c[/code], where [code]K[/code] is an integer. As an example, [code]10 % 3[/code] returns [code]1[/code], as [code]10 / 3[/code] is [code]9[/code] with a remainder of [code]1[/code].

An algorithmic example of the use of modulus is computing the time a certain number of hours after a given time. Take, for example, computing the time five hours in the future. If the current time is 11:00, adding five hours yields 4:00.

// the following can be done for a 24 hour clock

show_message("5 hours from 22:00 is " + string((22 + 5) % 24) + ":00");

For this simple example, you could compute this using a simple conditional statement, but modulus allows doing it in the general case. For instance, if the time is 23:00, but you want the time in five hours on a twelve-hour clock, the modulus example will still work, while simply conditionally subtracting twelve would give you 16:00 (28 − 12 = 16, but 28 % 12 is 4).

Another simple example is determining whether a number is even or odd.

// even

} else {

// odd

}

It may also be useful to perform some operation every nth iteration of a loop. The following loop will display every third number between zero and fifty:

if (i % 3 == 0) {

show_message(string(i));

}

}