For int a, b, I know that when there is exactly one of a and b is negative, the result of a / b and a % b is machine dependent. But do I always have (a / b * b) + a % b == a when b is not zero?
C++11 §5.6[expr.mul]/4 specifies:
If the quotient
a/bis representable in the type of the result,(a/b)*b + a%bis equal toa.
C11 §6.5.5/6 specifies the same with slightly different phrasing:
If the quotient
a/bis representable, the expression(a/b)*b + a%bshall equala; otherwise, the behavior of botha/banda%bis undefined.
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