Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c

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?

like image 934
updogliu Avatar asked Sep 02 '12 01:09

updogliu


1 Answers

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.

like image 59
James McNellis Avatar answered Nov 07 '22 03:11

James McNellis