The Difference between MOD and REMAINDER.
by Moneo, June 2007

The QB manual says:
"Modulo arithmetic is denoted by the modulus operator MOD. Modulo arithmetic provides the REMAINDER, rather than the quotient, of an integer division."

The confusion is, that in the QB manual, the MOD operator is called "MOD", but actualy provides the REMAINDER. This gives some programmers the false impression that the MOD operator always provides a mathematical MOD.

What is a mathematical MOD?
A mathematical MOD function is defined as the amount by which a number exceeds the largest integer multiple of the divisor that is not greater than that number.

When do I need to use a mathematical MOD?
My experience has been when trying to implement other people's algorithms into QB code. You suddenly encounter a MOD function in the algorithm and don't know whether to use a QB MOD or not. If you use the QB MOD and have problems, then most probably a mathematical MOD was required.

For positive numbers, or numbers with the same sign, if you the programmer are thinking mathematical MOD or thinking REMAINDER, you'll get the desired result by using the QB MOD operator.

However, if only one of the numbers is negative, the QB MOD operator continues to provide a REMAINDER, as stated in the manual, and NOT a mathematical MOD as we were led to believe when we used numbers having the same sign. This is the crux of the problem, that is, if one of the numbers is negative, the result of a QB MOD will not equal the result of a mathematical MOD.

SUMMARY:
1) If you want a MOD, and have numbers with the same sign, use the QB MOD operator, or the MathMOD algorithm.
2) If you want a REMAINDER, regardless of signs of the numbers, use the QB MOD operator only.
3) If you want a mathematical MOD, and have only one negative number, use the MathMOD algorithm only.

MathMOD algorithm for computing a mathematical MOD:
Replace (a MOD b) with:
a-b*INT(a/b)

EXAMPLES:
 7, 3 ... QB MOD = 1 ... MathMOD = 1
-7,-3 ... QB MOD =-1 ... MathMOD =-1
 7,-3 ... QB MOD = 1 ... MathMOD =-2
-7, 3 ... QB MOD =-1 ... MathMOD = 2

*****
