Yesterday, when learning the mathematical functions of MATLAB, the tutorial mentions that modulo (MoD) and take-rest (REM) is different, today on the Web specifically checked:
Usually the modulo operation is called the rest operation, and they return the result as a remainder. The only difference between REM and mod is:
When x and Y are the same sign, two function results are equal, and when the X and Y symbols are not the same, the REM function results in the same sign as the X, and mod and Y.
This is because the two functions are generated differently, the REM function uses the Fix function, and the MoD function takes the floor function (these two functions are used to rounding, the fix function rounds in 0 directions, and the floor function rounds in an infinitely small direction).
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 does not wait for 0 o'clock, N=floor (x./y)
The law of the modulus of two integers (when it is a decimal number is also the rule, which seems to be different from the C language)
Consider two integers as positive, then divide
When the ① can be divisible, the value is 0.
When ② cannot be divisible, its value = divisor x (whole quotient +1)-dividend
Example: MoD (36,-10) =-4
That is: 36 divided by 10 of the integer quotient is 3, plus 1 is 4, and the product of the divisor is 40, and then the difference between the number of (40-36=4), take the divisor of the symbol. So the value is-4.
Example: MoD (9,1.2) = 0.6;
Example:
>> mod (5,2)
ans =1 % "divisor "is positive," remainder "is positive
>> mod ( -5,2)
ans =1
>> mod (5,-2)
ans =-1 % "divisor" is negative, "remainder" is negative
>> mod ( -5,-2)
ans =-1 % when using REM, the "remainder" symbol is the same as the symbol for "dividend"
>> rem (5,2)
, regardless of whether the "divisor" is exactly negative. Ans =1 % "dividend" is positive, "remainder" is positive
>> REM (5 , -2);
ans =1
>> rem ( -5,2)
ans =-1 % "dividend" is negative, "remainder" is negative
>> REM ( -5,-2)
ans =-1
Slowly realize that the two are not the same