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/b
is representable in the type of the result,(a/b)*b + a%b
is equal toa
.
C11 §6.5.5/6 specifies the same with slightly different phrasing:
If the quotient
a/b
is representable, the expression(a/b)*b + a%b
shall equala
; otherwise, the behavior of botha/b
anda%b
is 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