Dietmar Kuehl wrote:
I have a doubt here. which among these causes undefined behavior?
Because i was relying on these :-((
negetive % negetive.
negetive % positive.
positive % negetive
I have the following assumption about % and /
When b != 0.
1. abs(a/b) == abs(a) / abs(b)
2. sign of a/b is positive if a and b have the same sign.negative
otherwise.
3. abs(a%b) == abs(a) % abs(b)
4. sign of a %b == sign of a.
5. (a/b) * b + a%b == a
From C Standard May 6,2005 draft Page 82
section 6.5.5 Mulplicative operators
5 The result of the / operator is the quotient from the division of the
first operand by the
second; the result of the % operator is the remainder. In both
operations, if the value of
the second operand is zero, the behavior is undefined.
6 When integers are divided, the result of the / operator is the
algebraic quotient with any
fractional part discarded(88) If the quotient a/b is representable, the
expression
(a/b)*b + a%b shall equal a.
foot note 88) This is often called ''truncation toward zero''.
How many of my assumptions are valid? Also what the C++ standard says
about this?