我是否可以确定C++中的奇数应该总是以这样的方式返回结果的底层,即存在余数或者是否有任何例外?我的意思是:
int x = 5;
x = x/2;
cout<<x; //2
Run Code Online (Sandbox Code Playgroud)
是.你可以在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)