The difference between the modulo (MoD) and the Take-up (REM)

Source: Internet
Author: User
Tags floor function mathematical functions rounds

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.