c++ - 除以余数时,奇数是否总是返回 floor?

标签 c++

我能否确定 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/

相关文章:

c++ - 如何调用父类类型列表的子类的方法? C++

c++ - 对嵌套范围内经常重复出现的变量类型使用相同的名称

java - 设计音频架构

c++ - 我们在条件三元运算符中使用逗号时发现了什么?

c++ - 是否存在标准调用约定?

C++ 标准对未实例化模板的要求

c++ - 是 (int)(unsigned)-1 == -1 未定义的行为

c++ - wstring 到 wchar_t 的转换

C++ #define 预处理器指令在 "<text>"之后无法识别

c++ - 仅使用C++评分程序获得重复的奇异响应