Modulus (Mod)
What is it?
When you perform integer division on two numbers there will often be a Remainder left over. This remainder is the difference between the original numerator and the quotient. Mod is a function which, when given a numerator and denominator, performs an integer division and returns the remainder.
To illustrate this, let's look at the following example that uses integer division:
13 % 5
This will return 2, as this is the number of times you can split 13 by 5 without using fractions. If we wanted to return the remainder that was left over by this calculation, we could use the Mod function, which would return the value 3. The following diagram shows how the remainder is calculated:

What is the Syntax?
The Mod function’s syntax is as follows:
Mod( Numerator, Denominator );
Why might I want to use this?
Although you may want to obtain a whole number quotient using integer division, you may not want to discard the remaining value entirely.
You may remember the baker we discussed in the section on arithmetic operators. Now that he’s finished baking his scones he has to present them to a tea party, which consists of three tables of individuals who insist the scones be divided evenly. To find out if an equal sharing is possible, modulo may be used to evaluate the numerator (here, the number of scones) and the denominator (the number of tables). If the modulus result is not zero, there will be that many scones left over.
An integer division of the 16 scones (NumberOfScones) by the 3 tables (NumberOfTables) confirms that each table will receive 5 scones. The baker then performs a modulus evaluation of NumberOfScones and NumberOfTables to find the remainder.
LeftoverScones := Mod( NumberOfScones, NumberOfTables );
The above calculation reveals that of the 16 scones, one will be left over, which the baker decides to have for himself as a reward for all his hard work.
<< Previous: Power | Next: Concatenate (Concat) >>