Differences between modulus and remainder

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

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

 

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.