check NaN number(检查 NaN 数)
问题描述
是否可以检查一个数字是否为NaN?
是的,因为 NaN 不等于任何其他数字,包括它自己.
em>
当您考虑 NaN
的含义时,这是有道理的,即您创建了一个实际上无法用正常"浮点值表示的值.p>
因此,如果您创建两个不知道它们是什么的数字,则很难认为它们相等.它们可能是,但是,考虑到它可能是相当大的数字可能性(实际上是无限的),两个相同数字的可能性微乎其微:-)
您可以查找函数(实际上是宏),例如 isnan
(在 math.h
中用于 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 数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:检查 NaN 数


- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- C++ 协变模板 2021-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 近似搜索的工作原理 2021-01-01
- 从python回调到c++的选项 2022-11-16
- 静态初始化顺序失败 2022-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01