我能否确定 C++ 中的奇数在以存在余数的方式划分时应该始终返回结果的底数,或者是否有任何异常(exception)?我的意思是:
int x = 5;
x = x/2;
cout<<x; //2
最佳答案
是的。你可以在 C++ 中确定这一点
ISO/IEC N3485(working draft)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.
关于c++ - 除以余数时,奇数是否总是返回 floor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16088357/