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 数
基础教程推荐
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 使用从字符串中提取的参数调用函数 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 从 std::cin 读取密码 2021-01-01