The Difference between MOD and REMAINDER - Summary.
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.
For positive numbers, or numbers with the same sign, if you the programmer are thinking 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.
1) If you want a MOD, and have numbers with the same sign, use the QB MOD operator, or the Lawgin-Moneo algorithm.
2) If you want a MOD, and have only one negative number, use the Lawgin-Moneo algorithm. Don't use the QB MOD.
3) If you want a REMAINDER, regardless of signs of the numbers, use QB MOD operator, or Rpgfan algorithm below.
Lawgin-Moneo algorithm for computing mathematical MOD:
Replace (a MOD b) with:
The Difference between MOD and REMAINDER - In Detail.
by Rpgfan, June 2007
MOD = The Mathematical modulo of two numbers.
REMAINDER = The remainder of the division of two numbers.
The definitions of "mod" and "rem" are what will be used for the duration of this document.
A = B (mod N)
referencing both the Wikipedia entry "Modulo operation", specifically the following formula represented using mathematical notation in Wikipedia, as well as the mathforum thread to which Moneo provided a URL:
N = A - B * floor(A / B) ...Note: this is the same as the Lawgin-Moneo algorithm, since floor is the same as INT.
N is the result of the operation (in the case of -340 and 60, it was 20)
The mathforum thread used different wording, explaining the the integer division done in Excel rounded toward -infinity (negative infinity) rather than rounding toward zero. Rounding a normal division operation toward -infinity is very commonly known as the floor function. In QBasic and QuickBASIC, this is implemented as the INT function.
A rem B = A - B * fix(A / B)
fix simply truncates a numeric value so that there is no decimal point. Another way to think of it is rounding toward zero. In the case of -340 rem 60, the result is -40. In QBasic and QuickBASIC, fix is represented as the FIX function.
Note that you can also use the QB MOD function (A MOD B) to get the same remainder result.
For simpler numbers -
7 mod -3:
N = 7 - (-3) * floor(7 / (-3)) = 7 - (-3) * (-3) = 7 - 9
N = -2
7 rem -3:
remainder = 7 - (-3) * fix(7 / (-3)) = 7 - (-3) * (-2) = 7 - 6
remainder = 1
-7 mod -3:
N = -7 - (-3) * floor(-7 / (-3)) = -7 - (-3) * 2 = -7 - (-6) = -7 + 6
N = -1
-7 rem -3:
remainder = -7 - (-3) * fix(-7 / (-3)) = -7 - (-3) * 2 = -7 - (-6) = -7 + 6
remainder = -1
-7 mod 3:
N = -7 - 3 * floor(-7 / 3) = -7 - 3 * (-3) = -7 - (-9) = -7 + 9
N = 2
-7 rem 3:
remainder = -7 - 3 * fix(-7 / 3) = -7 - 3 * (-2) = -7 - (-6) = -7 + 6
remainder = -1
7 mod 3:
N = 7 - 3 * floor(7 / 3) = 7 - 3 * 2 = 7 - 6
N = 1
7 rem 3:
remainder = 7 - 3 * fix(7 / 3) = 7 - 3 * 2 = 7 - 6
remainder = 1
Notice how if the signs are the same, A mod B = A rem B. Also note the following:
mod - result takes the sign of B (if B is negative, the result is negative)
rem - result takes the sign of A (if A is positive, the result is positive)
* A mod B is the mathematical modulo operation: A = B (mod N), where N = A - B * floor(A / B); N is the result of A mod B.
* A rem B is the remainder operation commonly referred to as MOD: A rem B = A - B * fix(A / B)
* INT in QBasic and QuickBASIC is the equivalent of the floor function, which rounds a numeric value to the nearest integer toward negative infinity (-OO or -infinity), also referred to as "away from zero."
* FIX in QBasic and QuickBASIC is the equivalent of the fix function, which rounds a numeric value to the nearest integer toward zero.
* If the signs of A and B are the same, A mod B = A rem B.
* The result of A mod B takes the sign of B.
* The result of A rem B takes the sign of A.
The info in above recap should be all the _basic_ information (some level of detail should obviously be provided; I merely restated things previously said or mentioned in a form that is easier to understand for those that can't be bothered with sifting through text, especially when it is mixed with numbers that creates unnecessary confusion for some) needed to explain how they work, with a program or two showing a demo of both and perhaps some examples.
I hope this helps clarify things a bit for those that are interested and just don't understand all of the math behind it. I also hope this may further the discussion as I provided formulas that are nearly identical, which should make them easier to learn and implement.
 - http://en.wikipedia.org/wiki/Modulo_operation
 - http://mathforum.org/library/drmath/view/52343.html
For a history of this post, see http://www.network54.com/Forum/202193/message/1181786500