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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.