奇数总是在用剩余部分划分时返回楼层吗?

Str*_*tfw 5 c++

我是否可以确定C++中的奇数应该总是以这样的方式返回结果的底层,即存在余数或者是否有任何例外?我的意思是:

int x = 5;
x = x/2;
cout<<x;      //2
Run Code Online (Sandbox Code Playgroud)

Kou*_*tty 5

是.你可以在c ++中确定这一点

ISO/IEC N3485(工作草案)在5.6.4中说

The binary / operator yields the quotient, and the binary % operator yields 
the remainder from the division of the ?rst expression by the second.
   If the second  operand of / or % is zero the behavior is unde?ned. 
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 unde?ned.
Run Code Online (Sandbox Code Playgroud)


Guv*_*nte 2

在 C/C++ 中,整数除法作为地板运算处理。

你会得到2上面的例子,因为真正的答案2.5无法被表示。

这里有一些更详细的答案