So there are three values that a modulus operation can give you:
Then:
-7 % 5 = 3 (math, remainder >= 0)
-7 % 5 = -2 (C++)
-7 % (size_t)5 = 4 (C++)
Another example:
-7 % 4 = 1 (math, remainder >= 0)
-7 % 4 = -3 (C++)
-7 % (size_t)4 = 1 (C++)
When the left hand operand is positive, the answer between all three methods are the same. But for negative values they all seem to have their own methods. How is the value of modulus operations on unsigned operands calculated in C++?
The modulus of a negative number is found by ignoring the minus sign. The modulus of a number is denoted by writing vertical lines around the number. Note also that the modulus of a negative number can be found by multiplying it by −1 since, for example, −(−8) = 8. Exercise 1.
Modulo and remainder operators differ with respect to negative values. With a remainder operator, the sign of the result is the same as the sign of the dividend (numerator) while with a modulo operator the sign of the result is the same as the divisor (denominator).
The modulus operator is added in the arithmetic operators in C, and it works between two available operands. It divides the given numerator by the denominator to find a result. In simpler words, it produces a remainder for the integer division. Thus, the remainder is also always an integer number only.
This is what happens when you mix signed and unsigned values — confusion!
[C++14: 5.6/2]:
The operands of*
and/
shall have arithmetic or unscoped enumeration type; the operands of%
shall have integral or unscoped enumeration type. The usual arithmetic conversions are performed on the operands and determine the type of the result.
Now, see the bolded passage below (which assumes your size_t
has the same rank as your int
; this is always true):
[C++14: 5/10]:
Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows:
- If either operand is of scoped enumeration type (7.2), no conversions are performed; if the other operand does not have the same type, the expression is ill-formed.
- If either operand is of type long double, the other shall be converted to long double.
- Otherwise, if either operand is double, the other shall be converted to double.
- Otherwise, if either operand is float, the other shall be converted to float.
- Otherwise, the integral promotions (4.5) shall be performed on both operands.61 Then the following rules shall be applied to the promoted operands:
- If both operands have the same type, no further conversion is needed.
- Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank shall be converted to the type of the operand with greater rank.
- Otherwise, if the operand that has unsigned integer type has rank greater than or equal to the rank of the type of the other operand, the operand with signed integer type shall be converted to the type of the operand with unsigned integer type.
- Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, the operand with unsigned integer type shall be converted to the type of the operand with signed integer type.
- Otherwise, both operands shall be converted to the unsigned integer type corresponding to the type of the operand with signed integer type.
In short, your -7
is becoming std::numeric_limit<size_t>::max() + 1 - 7
(whatever that is on your platform), and the calculation is being performed on that value. Indeed, on my platform, that confirms the result of 1
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With