检查NaN数 [英] check NaN number
问题描述
是否可以检查数字是否 NaN
或不是?
是的,通过使用 NaN
不等于任何其他数字,包括其自身的事实。
当你考虑 NaN
意味着你已经创建了一个不是你的
所以,如果你创建两个数字,你不知道它们是什么,你几乎不能认为它们是相等的。他们可以但是,考虑到可能是(可能是无限的)数字的相当大的可能性,两个是相同的数字的机会是微乎其微的: - )
您可以查找 isnan
(在 math.h $ c $中)的函数C>为C和
cmath
为C ++)或者只使用属性 NaN
值不等于自身例如:
if(myFloat!= myFloat){...}
如果因为一些奇怪的原因,你的C实现没有 isnan
标准委托它),你可以编写自己的,像下面这样:
int isnan_float(float f){return(f!= F); }
Is it possible to check if a number is NaN
or not?
Yes, by use of the fact that a NaN
is not equal to any other number, including itself.
That makes sense when you think about what NaN
means, the fact that you've created a value that isn't really within your power to represent with "normal" floating point values.
So, if you create two numbers where you don't know what they are, you can hardly consider them equal. They may be but, given the rather large possibility of numbers that it may be (infinite in fact), the chances that two are the same number are vanishingly small :-)
You can either look for a function (macro actually) like isnan
(in math.h
for C and cmath
for C++) or just use the property that a NaN
value is not equal to itself with something like:
if (myFloat != myFloat) { ... }
If, for some bizarre reason, your C implementation has no isnan
(it should, since the standard mandates it), you can code your own, something like:
int isnan_float (float f) { return (f != f); }
这篇关于检查NaN数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!