Difference Between Modulo and Complementation Operation

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.

For example:

a = -7, b = 2

For modulo operation,

Step 1: c = fix(a, b) = -4,

Step 2: m = a - b * c = -7 - (2 * (-4)) = 1.

For complementation operation,

Step 1: c = fix(a, b) = -3,

Step 2: 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.

发表评论

电子邮件地址不会被公开。 必填项已用*标注

8 − 6 =

3 + 1 =