Almost all languages have operators. Example of these are, “+” – addition, “-” – subtraction, “*” – multiplication, “/” division. There are also more advanced operators such as “Mod”, “Exponent”, and “And”. Let us investigate the mod.

I have seen that there are much misunderstanding about mod. Some claims that it performs a modulo operation, some claims that it is the reminder. Actually it is a reminder, and not a modulo operation.

In this investigation, I chosen Visual Basic as the default language. So, let us try to use mod operation as a modulo operation.

1 2 3 |
'default mod, "reminder" MsgBox(3 - 4 Mod 6) ' result is -1 MsgBox(-1 + 4 Mod 6) ' result is 3 |

I would like to add that there is no problem to get the same answer back, i.e., 3 -4 mod 6 is -1, and then by adding 4 and again mod, will result 3. However, that is not the modulo. To display the modulo, use the function below:

1 2 3 4 |
Function mod_r(ByVal x As Integer, ByVal y As Integer) As Integer 'the "real" modulo operation Return x - y * Int(x / y) End Function |

In arrays, tables, indexes, where the index should be positive, we can use the function above.

1 2 |
MsgBox(mod_r(3 - 4, 6)) ' result is 5 MsgBox(mod_r(5 + 4, 6)) ' result is 3 |

## Leave a Reply