C++ 不能修改零吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7370154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't Mod Zero?
提问by Dasaru
Why is X % 0
an invalid expression?
为什么是X % 0
无效表达式?
I always thought X % 0
should equal X. Since you can't divide by zero, shouldn't the answer naturally be the remainder, X (everything left over)?
我一直认为X % 0
应该等于X。既然不能除以零,那么答案自然不应该是余数,X(剩下的所有东西)吗?
采纳答案by Nawaz
The C++ Standard(2003) says in §5.6/4,
C++ 标准(2003)在 §5.6/4 中说,
[...] If the second operand of / or % is zero the behavior is undefined; [...]
[...] 如果 / 或 % 的第二个操作数为零,则行为未定义;[...]
That is, following expressions invoke undefined-behavior(UB):
也就是说,以下表达式调用 undefined-behavior(UB):
X / 0; //UB
X % 0; //UB
Note also that -5 % 2
is NOT equal to -(5 % 2)
(as Petar seems to suggest in his comment to his answer). It's implementation-defined. The spec says (§5.6/4),
另请注意,这-5 % 2
不等于-(5 % 2)
(正如佩塔尔似乎在他对答案的评论中所暗示的那样)。它是实现定义的。规范说(§5.6/4),
[...] If both operands are nonnegative then the remainder is nonnegative; if not, the sign of the remainder is implementation-defined.
[...] 如果两个操作数都为非负,则余数为非负;如果不是,则余数的符号是 implementation-defined。
回答by Mateen Ulhaq
This answer is not for the mathematician. This answer attempts to give motivation (at the cost of mathematical precision).
这个答案不适合数学家。这个答案试图提供动力(以数学精度为代价)。
Mathematicians:See here.
数学家:看这里。
Programmers:Remember that division by 0
is undefined
. Therefore, mod
, which relies on division, is also undefined
.
程序员:记住除法0
是undefined
。因此,mod
依赖除法的 也是undefined
。
This represents division for positive X
and D
; it's made up of the integral part and fractional part:
这代表正X
和的除法D
;它由整数部分和小数部分组成:
(X / D) = integer + fraction
= floor(X / D) + (X % D) / D
Rearranging, you get:
重新排列,你得到:
(X % D) = D * (X / D) - D * floor(X / D)
Substituting 0
for D
:
替换0
为D
:
(X % 0) = 0 * (X / 0) - 0 * floor(X / 0)
Since division by 0
is undefined
:
由于除法0
是undefined
:
(X % 0) = 0 * undefined - 0 * floor(undefined)
= undefined - undefined
= undefined
回答by Petar Ivanov
X % D
is by definitiona number 0 <= R < D
, such that there exists Q
so that
X % D
根据定义是一个数0 <= R < D
,使得存在Q
使得
X = D*Q + R
So if D = 0
, no such number can exists (because 0 <= R < 0
)
因此,如果D = 0
,则不存在这样的数字(因为0 <= R < 0
)
回答by Billy Moon
I think because to get the remainder of X % 0
you need to first calculate X / 0
which yields infinity, and trying to calculate the remainder of infinity is not really possible.
我认为因为要得到X % 0
你的余数需要首先计算X / 0
哪个产生无穷大,而试图计算无穷大的余数是不可能的。
However, the best solution in line with your thinking would be to do something like this
但是,符合您想法的最佳解决方案是做这样的事情
REMAIN = Y ? X % Y : X
回答by Mac
Another way that might be conceptually easy to understand the issue:
另一种可能在概念上容易理解问题的方法:
Ignoring for the moment the issue of argument sign, a % b
could easily be re-written as a - ((a / b) * b)
. The expression a / b
is undefined if b
is zero, so in that case the overall expression must be too.
暂时忽略参数符号的问题,a % b
可以很容易地重写为a - ((a / b) * b)
. a / b
如果b
为零,则表达式未定义,因此在这种情况下,整个表达式也必须如此。
In the end, modulus is effectively a divisive operation, so if a / b
is undefined, it's not unreasonable to expect a % b
to be as well.
最后,模数实际上是一个除法运算,因此如果a / b
未定义,则期望a % b
也是如此也并非不合理。
回答by K-ballo
X % Y gives a result in the integer [ 0, Y ) range. X % 0 would have to give a result greater or equal to zero, and less than zero.
X % Y 给出整数 [ 0, Y ) 范围内的结果。X % 0 必须给出大于或等于零且小于零的结果。
回答by ollj
you can evade the "divivion by 0" case of (A%B) for its type float identity mod(a,b) for float(B)=b=0.0 , that is undefined, or defined differently between any 2 implementations, to avoid logic errors (hard crashes) in favor of arithmetic errors...
您可以避免 (A%B) 的“除以 0”的情况,因为它的类型为 float identity mod(a,b) for float(B)=b=0.0 ,即未定义,或在任何 2 个实现之间定义不同,以避免逻辑错误(硬崩溃),有利于算术错误......
by computing mod([a*b],[b])==b*(a-floor(a))
INSTREAD OF
computing mod([a],[b])
通过计算mod([a*b],[b])==b*(a-floor(a))
INSTREAD OF
计算mod([a],[b])
where [a*b]==your x-axis, over time [b] == the maximum of the seesaw curve (that will never be reached) == the first derivative of the seesaw function
其中 [a*b]== 你的 x 轴,随着时间的推移 [b] == 跷跷板曲线的最大值(永远不会达到)== 跷跷板函数的一阶导数