c ++除以0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4745311/
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
c++ division by 0
提问by Bob
I am running long simulations. I record the results into a vector to compute statistics about the data. I realized that, in theory, those samples could be the result of a division by zero; this is only theoretical, I am pretty sure it's not the case. In order to avoid rerunning the simulation after modifying the code, I was wondering what happens in that case. Would I be able to realize whether a division by 0 has occurred or not? Will I get error messages? (Exceptions are not being handled at the moment).
我正在运行长时间的模拟。我将结果记录到一个向量中以计算有关数据的统计信息。我意识到,理论上,这些样本可能是除以零的结果;这只是理论上的,我很确定事实并非如此。为了避免修改代码后重新运行模拟,我想知道在这种情况下会发生什么。我是否能够意识到是否发生了被 0 的除法?我会收到错误消息吗?(目前不处理异常)。
Thanks
谢谢
回答by etarion
For IEEE floats, division of a finite nonzero float by 0 is well-defined and results in +infinity (if the value was >zero) or -infinity (if the value was less than zero). The result of 0.0/0.0 is NaN. If you use integers, the behaviour is undefined.
对于 IEEE 浮点数,有限非零浮点数除以 0 是明确定义的,结果为 +infinity(如果值 > 零)或 -infinity(如果值小于零)。0.0/0.0 的结果是 NaN。如果使用整数,则行为未定义。
回答by Ог?ен Шоба?и?
Note that C standard says (6.5.5):
请注意,C 标准说 (6.5.5):
The result of the / operator is the quotient from the division of the first operand by the second; the result of the % operator is the remainder. In both operations, if the value of the second operand is zero, the behavior is undefined.
/ 运算符的结果是第一个操作数除以第二个操作数的商;% 运算符的结果是余数。在这两个操作中,如果第二个操作数的值为零,则行为未定义。
So something/0 is undefined (by the standard) both for integral types and Floating points. Nevertheless most implementations have fore mentioned behavior (+-INF or NAN).
因此,对于整数类型和浮点数,something/0 是未定义的(根据标准)。尽管如此,大多数实现都具有前面提到的行为(+-INF 或 NAN)。
回答by Shinnok
If you're talking integers then your program should crash upon division by zero.
如果你在谈论整数,那么你的程序应该在被零除时崩溃。
If you're talking floats then division by zero is allowed and the result to that is INF or -INF. Now it's all up to your code if the program will crash, handle that nicely or continue with undefined/unexpected results.
如果您说的是浮点数,则允许除以零,结果为 INF 或 -INF。现在这完全取决于您的代码,如果程序会崩溃,很好地处理它还是继续出现未定义/意外的结果。
回答by Daniel Gehriger
Depends if you are using integers or floating points numbers. For integer, you'll get a runtime exception. For floating point numbers, the result will be +/- infinity, or NaN
for (0.0/0.0), which you can test using std::isnan()
.
取决于您使用的是整数还是浮点数。对于整数,您将收到运行时异常。对于浮点数,结果将为 +/- 无穷大,或者NaN
对于 (0.0/0.0),您可以使用std::isnan()
.
回答by goblin01
If you use IEEE floats, then it will return 0 or NaN. If the op1 is 0, you will get undefined. If op1 is higher than 0, you will get Infinity. If op1 is lower than 0, then you will get -Infinity. If you use dividing by 0 directly or in integer , you will get error "Floating point exception".
如果您使用 IEEE 浮点数,则它将返回 0 或 NaN。如果 op1 为 0,您将得到未定义。如果 op1 大于 0,您将获得 Infinity。如果 op1 小于 0,那么您将得到 -Infinity。如果直接使用除以 0 或使用整数除法,则会出现错误“浮点异常”。