Modulo and Complementation operation are partially same. This article will expalin their difference.
The way to compute Modulo and Complementation (
m = a mod b or
m = a % b) is as follows:
Step 1: Compute
c = a / b;
Step 2: Compute
m = a - b * c.
Facially, Modulo and Complementation have the same 2-step operation, but there is a little bit difference in Step 1. For Modulo, a
floor() method is used to compute
c, whereas a
fix() method is used to compute c for complementation.
This difference has a reasonable explaination. The result of
a mod b is always positive (bigger or equal to zero), so a
floor() method is required to make sure
c * b is less than
a, otherwise not.
a = -7, b = 2
For modulo operation,
c = fix(a, b) = -4,
m = a - b * c = -7 - (2 * (-4)) = 1.
For complementation operation,
c = fix(a, b) = -3,
m = m = a - b * c = -7 - (2 * (-3)) = -1.
Conclusion: For a and b have the same sign, the result of Modulo and Complementation are the same. For a and b have different signs, the result of Modulo and Complementation are different.