c ++除以0 [英] c++ division by 0

查看:443
本文介绍了c ++除以0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进行长时间模拟。我将结果记录到向量中以计算有关数据的统计信息。我意识到,在理论上,这些样本可能是零除法的结果;这只是理论,我很确定不是这样。为了避免在修改代码后重新运行模拟,我想知道在这种情况下会发生什么。我能够实现是否发生0除法?我会收到错误消息吗? (目前未处理异常)。

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).

感谢

推荐答案

对于IEEE浮点,有限非零float by 0是明确定义的,并导致+无穷大(如果值>零)或-infinity(如果值小于零)。 0/0的结果是NaN。如果使用整数,行为是未定义的。

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 is NaN. If you use integers, the behaviour is undefined.

这篇关于c ++除以0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆