Possible Duplicate:
C++ operator % guarantees
In c++ 98/03
5.6-4
The binary / operator yields the quotient, and the binary % operator yields the remainder from the division of the first expression by the second. If the second operand of / or % is zero the behavior is undefined; otherwise (a/b)*b + a%b is equal to a. If both operands are nonnegative then the remainder is nonnegative; if not, the sign of the remainder is implementation-defined.
In c++ 11:
5.6 -4
The binary / operator yields the quotient, and the binary % operator yields the remainder from the division of the first expression by the second. If the second operand of / or % is zero the behavior is undefined. For integral operands the / operator yields the algebraic quotient with any fractional part discarded;81 if the quotient a/b is representable in the type of the result, (a/b)*b + a%b is equal to a.
As you can see the implementation-defined for the sign bit is missing, what happens to it ?
Double Modulus Operator This kind of mod operator does not exist in C or C++ where the mod operator only works with int operands. The evaluated result is a double value. For example, 8.27 % 2 evaluates to 0.27 since the division is 4 with a remainder of 0.27.
If either or both operands of the mod operator have type double, then evaluating it produces the remainder.
The modulo division operator produces the remainder of an integer division. produces the remainder when x is divided by y. Return Value: If y completely divides x, the result of the expression is 0.
When both the divisor and dividend are negative. If both the divisor and dividend are negative, then both truncated division and floored division return the negative remainder.
The behaviour of %
was tightened in C++11, and is now fully specified (apart from division by 0
).
The combination of truncation towards zero and the identity (a/b)*b + a%b == a
implies that a%b
is always positive for positive a
and negative for negative a
.
The mathematical reason for this is as follows:
Let ÷
be mathematical division, and /
be C++ division.
For any a and b, we have a÷b = a/b + f
(where f is the fractional part), and from the standard, we also have (a/b)*b + a%b == a
.
a/b
is known to truncate towards 0
, so we know that the fractional part will always be positive if a÷b
is positive, and negative is a÷b
is negative:
sign(f) == sign(a)*sign(b)
a÷b = a/b + f
can be rearranged to give a/b = a÷b - f
. a
can be expanded as (a÷b)*b
:
(a/b)*b + a%b == a
=> (a÷b - f)*b+a%b == (a÷b)*b
.
Now the left hand side can also be expanded:
(a÷b)*b - f*b + a%b == (a÷b)*b
a%b == f*b
Recall from earlier that sign(f)==sign(a)*sign(b)
, so:
sign(a%b) == sign(f*b) == sign(a)*sign(b)*sign(b) == sign(a)
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