What are the rules for the modulo operation in C++?
In C++, the modulus operator (%) calculates the remainder after dividing two numbers. The specific rule is as follows:
- For integer types: the modulus operator returns the remainder after dividing two operands.
- For floating point numbers: the modulus operator converts the floating point number to an integer, then performs integer modulus operation.
For example:
int a = 10;
int b = 3;
int result = a % b; // result的值为1,因为10除以3等于3余1
It is important to note that the modulus operator cannot be applied to floating-point data types, or else a compilation error will occur.