如何除以零而无误 [英] How to divide by zero without error

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

问题描述

我需要获得浮点数NaN和无穷大,但是我不能使用这样的结构

$ $ p $ code> 0。 / 0.
1. / 0.

因为它会导致编译时间


$ b


错误C2124:将零分或零分给

编辑,我可以得到这个数字的答案是很酷的(每个答案是+1),但是可以除以零吗?

解决方案

您可以简单地返回一个NaN或一个无穷大,例如:

 返回std :: numeric_limits< float> :: quiet_NaN(); 

  return std :: numeric_limits< float> :: infinity(); 

请参阅 std :: numeric_limits ,from header < limits>


i need to obtain float NaN and infinity, but i can't use such constructions

0. / 0.
1. / 0.

because it cause compile time

error C2124: divide or mod by zero

EDIT, it is cool to have answers for where i can get this numbers (+1 for every), but is it possible to divide by zero?

解决方案

You can simply return a NaN or an infinity, for example:

return std::numeric_limits<float>::quiet_NaN();

or

return std::numeric_limits<float>::infinity();

See std::numeric_limits, from header <limits>.

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

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