Modulus (mod)

Description

The mod operator takes two operands of integral type and calculates the modulus. The result of this operation is of integral type. The modulus operation is defined by Standard Pascal (ISO/IEC 7185) as follows:

A term of the form i mod j shall be an error if j is zero or negative, otherwise, the value of i mod j shall be that value of (i-(k*j)) for integral k such that 0 <= i mod j < j

Irie Pascal implements the mod operator according to the definition given above. What does the definition really mean? Well in the simple case where the operands are positive then the result of the operation is the remainder left over after dividing the left operand by the right operand. This is the most common use of the mod operator, but you should be aware that this does not hold when the left operand is negative. You should also be aware that the right operand can not be negative.

Example

For example if you run the program below:

   program mods(output);
   var
      i : -3..3;
      j : 1..3;
   begin
      for i := -3 to 3 do
         for j := 1 to 3 do
            writeln(i:1, ' mod ', j:1, ' = ', i mod j : 1);
   end.

you will get the following output

  -3 mod 1 = 0
  -3 mod 2 = 1
  -3 mod 3 = 0
  -2 mod 1 = 0
  -2 mod 2 = 0
  -2 mod 3 = 1
  -1 mod 1 = 0
  -1 mod 2 = 1
  -1 mod 3 = 2
   0 mod 1 = 0
   0 mod 2 = 0
   0 mod 3 = 0
   1 mod 1 = 0
   1 mod 2 = 1
   1 mod 3 = 1
   2 mod 1 = 0
   2 mod 2 = 0
   2 mod 3 = 2
   3 mod 1 = 0
   3 mod 2 = 1
   3 mod 3 = 0