简体   繁体   中英

Do I always have `(a / b * b) + a % b == a` when b is not zero?

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 to a .

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 equal a ; otherwise, the behavior of both a/b and a%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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM