When I learned the mathematical functions of MATLAB yesterday, I mentioned in the tutorial that MoD and REM are different. I checked them on the internet today:
A modulo operation is also called a remainder operation. The only difference between REM and mod is that they return the remainder:
When the positive and negative signs of X and Y are the same, the results of the two functions are the same. When the symbols of X and Y are different, the symbols of the result of the REM function are the same as those of X, moD is the same as Y.
This is because the two functions have different generation mechanisms. The rem function uses the fix function, while the MOD function uses the floor function (these two functions are used to take an integer, And the fix function is rounded to 0, the floor function is rounded to an infinitely small direction ).
The REM (x, y) command returns X-n. * Y. If y is not equal to 0, n = fix (X. /Y), while Mod (x, y) returns X-n. * Y. When Y is not equal to 0, n = floor (X. /y)
The modulo value of two different integers (this operation rule is also used when the decimal number is used, which seems to be different from the C language)
Consider two integers as positive numbers before division.
① When division is possible, its value is 0
② When division is not allowed, its value = divisor × (whole quotient + 1)-divisor
For example, MOD (36,-10) =-4
That is, 36 is divided by 10 and the integer quotient is 3. After 1 is added, it is 4. Its product is 40, and the difference from the number is (40-36 = 4 ); returns the divisor. So the value is-4.
For example, MOD (9, 1.2) = 0.6
> Mod (5, 2)
"Divisor" is positive, and "remainder" is positive
> Mod (-5, 2)
Ans = 1
> Mod (5,-2)
% "Divisor" is negative, "remainder" is negative"
> Mod (-5,-2)
Ans =-1% when REM is used, regardless of whether the "divisor" is positive, the "remainder" symbol is the same as the "dividend" symbol.
> REM (5, 2)
Ans = 1% "divisor" is positive, and "remainder" is positive
> REM (5,-2)
Ans = 1
> REM (-5, 2)
% "Dividend" is negative, and "remainder" is negative
Ans =-1