The Difference Between Modulo and Remainder Operations
Category Programming Techniques
Usually, the modulo operation is also called the remainder operation. Both return the remainder .rem
and mod
. The only difference is:
When the signs of x and y are the same, the results of the two functions are the same; when the signs of x and y are different, the result of the rem function has the same sign as x, while the result of mod has the same sign as y.
This is due to the different mechanisms of these two functions. The rem function uses the fix function, while the mod function uses the floor function (both of these functions are used for rounding, the fix function rounds towards 0, and the floor function rounds towards negative infinity).
The rem(x, y) command returns x-n.y, if y is not equal to 0, where n = fix(x./y), and mod(x,y) returns x-n.y, when y is not equal to 0, n=floor(x./y).
The pattern of modulo and remainder values for two integers with different signs (this pattern also applies when they are decimals, which seems to be different from C language)
First, consider both integers as positive, then perform the division operation:
- When it is divisible, the value is 0.
- When it is not divisible, the value = divisor × (quotient + 1) - dividend.
Example: mod(36,-10)=-4
That is: The integer quotient of 36 divided by 10 is 3, plus 1 is 4; its product with the divisor is 40; the difference with the dividend is (40-36=4); take the sign of the divisor. So the value is -4.
Example: mod(9,1.2)=0.6;
Example:
>> mod(5,2)
ans =1 % The divisor is positive, the remainder is positive
>> mod(-5,2)
ans =1
>> mod(5,-2)
ans =-1 % The divisor is negative, the remainder is negative
>> mod(-5,-2)
ans =-1 % When using rem, no matter whether the divisor is positive or negative, the sign of the remainder is the same as that of the dividend
>> rem(5,2)
ans =1 % The dividend is positive, the remainder is positive
>> rem(5,-2);
ans =1
>> rem(-5,2)
ans =-1 % The dividend is negative, the remainder is negative
>> rem(-5,-2)
ans =-1
Take your time to appreciate it, they are indeed different.
#
-
** cds
* 123**[email protected]
- Calculate the integer quotient:
c=a/b
- Calculate the integer quotient:
- Calculate the modulo or remainder:
r=a-(c*b)
- Calculate the modulo or remainder:
The modulo operation and the remainder operation differ in the first step.
The remainder operation discards the decimal part towards 0.
The modulo operation discards the decimal part towards negative infinity.
For example: 4/(-3) is approximately -1.3
In the remainder operation, the decimal part is discarded towards 0 as -1.
In the modulo operation, the decimal part is discarded towards negative infinity as -2.
So
4rem(-3)=1
4mod(-3)=-2
** cds
* 123**[email protected]
-
** ddfd
* 123**[email protected]
The modulo operation is more common in the field of computer science, while the remainder operation is generally used in the field of mathematics.
The calculation steps of the modulo operation (remainder operation):
a. Find the integer quotient
c=a/b
b. Find the modulo (remainder)
r=a-c*b
The difference between the two: The modulo operation rounds c towards negative infinity, while the remainder operation rounds c towards 0.
Conclusion: When a is a positive integer, the results of the modulo operation and the remainder operation are the same; when a is a negative integer, the results of the two are different.
** ddfd
**