检查 NaN 数

check NaN number(检查 NaN 数)

本文介绍了检查 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 数

基础教程推荐