Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

operator modulo change in c++ 11? [duplicate]

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 ?

like image 577
RoundPi Avatar asked Oct 27 '12 13:10

RoundPi


People also ask

Can you use mod on doubles in C?

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.

Does modulo work with double?

If either or both operands of the mod operator have type double, then evaluating it produces the remainder.

What does the modulo operator (%) do?

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.

What happens when you use the modulo operator with negative numbers?

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.


1 Answers

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)

like image 195
Mankarse Avatar answered Sep 21 '22 15:09

Mankarse