Does an odd number always return gender when divided with the remainder?

Can I be sure that an odd number in C ++ should always return the gender of the result if it is divided in such a way that there is a remainder or there are exceptions? I mean:

int x = 5;
x = x/2;
cout<<x;      //2
+5
source share
3 answers

Yes. you can be sure of this in C ++

ISO / IEC N3485 (working draft) says in 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; otherwise, the behavior of both 
a/b and a%b is undefined.
+5
source

Integer division is treated as a floor operation in C / C ++.

In the above example, you will get 2, since the real answer 2.5cannot be presented.

Some more detailed answers here.

+2
source

Yes; division between integer values ​​is always integral division in C ++:

[C++11 5.6/4]:The binary operator /gives the quotient, and the binary operator %gives the remainder of dividing the first expression by the second. If the second operand /or a %zero, behavior is undefined. For integral operands, the operator /gives an algebraic relation with any fractional part discarded; if quotient a/bis represented in the result type, (a/b)*b + a%bequal a.

+2
source

All Articles