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++?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…