Modulo and remainder, modulo Remainder

Source: Internet
Author: User
Tags modulus rounds

Modulo and remainder, modulo Remainder
For integer a and B, the modulo or remainder operations are all performed: 1. evaluate the integer quotient: c = a/B; 2. modulus or remainder: r = a-c * B. the modulo operation and the remainder operation are different in the first step: When the remainder operation is used to obtain the value of c, it rounds to 0 (the fix () function ); while the modulo operation calculates the value of c and rounds the value to the negative infinity (floor () function ). For example: Calculate-8% 3 so: a =-8; B = 3; Step 1: Calculate the integer quotient c, (float) c = a/B =-2.67, for example, perform the modulo operation c =-3 (round to the negative infinity direction) and perform the remainder c =-2 (round to the 0 direction). Step 2: the formula for calculating the modulus and the remainder is the same, but because of the different values of c, r = 1 for the modulus and r =-2 for the remainder. Induction: When the symbols a and B are consistent, the result is consistent because the modulo operation and the result of the remainder operation are the same. When the symbols are inconsistent, the results are different. The symbols of the result of the modulus operation are the same as those of B, and the symbols of the result of the remainder operation are the same as those of. In each environment, the % operator has different meanings. For example, c/c ++, java is the remainder operator, and python is the modulo operator. In terms of mathematics, both operators are correct. Many languages simply propose two functions, namely the remainder rem and mod. In fact, a mod B = floor (float) c) = floor (a/B) = floor (-2.67) =-3; a rem B = a mod B + 1; floor indicates downgrading. Corresponding, rem can also be implemented with a fix.

 

Related Article

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.