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 数
基础教程推荐
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- CString 到 char* 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 初始化列表*参数*评估顺序 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
