检查double(或float)是否为C ++中的NaN [英] Checking if a double (or float) is NaN in C++

查看:1330
本文介绍了检查double(或float)是否为C ++中的NaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有isnan()函数?

Is there an isnan() function?

PS:我在

PS.: I'm in MinGW (if that makes a difference).

我使用isnan()从< math 。,

I had this solved by using isnan() from <math.h>, which doesn't exist in <cmath>, which I was #includeing at first.

推荐答案

根据IEEE标准,NaN值具有奇怪的属性, em>始终 false。也就是说,对于浮点f,如果f是NaN,则 f!= f 将仅为真。

According to the IEEE standard, NaN values have the odd property that comparisons involving them are always false. That is, for a float f, f != f will be true only if f is NaN.

请注意,正如以下一些注释所指出的,并非所有编译器在优化代码时都会遵循这一点。

Note that, as some comments below have pointed out, not all compilers respect this when optimizing code.

对于声称使用IEEE浮点的任何编译器,此技巧应该工作。但我不能保证 在实践中工作。如果有疑问,请与您的编译器联系。

For any compiler which claims to use IEEE floating point, this trick should work. But I can't guarantee that it will work in practice. Check with your compiler, if in doubt.

这篇关于检查double(或float)是否为C ++中的NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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