NaN 是 Not a Number 的缩写.它是一个数值类型值,通常在浮点计算中,表示未定义或无法表示的值.而且,不能直接使用相等运算符 (==) 检查 NaN.由于在程序中,nan == nan (C/C++/Python) 或 nan is nan (Python) 总是返回 0 或 False.因此,除了采用库函数外,往往可以利用这个性质检查某个数值是否为 NaN.下面介绍如何采用库函数检查 NaN 值:

C/C++ 实现

在 C/C++ 中,采用 math.h 标准函数库中的 isnan 宏或函数检查 nan 值,具体示例代码如下:

C 代码 test-nan.c

/* isnan example */
#include <stdio.h> /* printf */
#include <math.h> /* isnan, sqrt */ int main()
{
printf ("isnan(0.0) : %d\n",isnan(0.0));
printf ("isnan(1.0/0.0) : %d\n",isnan(1.0/0.0));
printf ("isnan(-1.0/0.0) : %d\n",isnan(-1.0/0.0));
printf ("isnan(sqrt(-1.0)): %d\n",isnan(sqrt(-1.0)));
return ;
}

编译和运行结果,如下所示

$ gcc test-nan.c -lm
$ ./a.out
isnan(0.0) :
isnan(1.0/0.0) :
isnan(-1.0/0.0) :
isnan(sqrt(-1.0)):

C++ 代码 test-nan.cpp

/* isnan example */
#include <cmath> /* isnan, sqrt */
#include <iostream> using namespace std; int main()
{
cout << "isnan(0.0) : " << isnan(0.0) << endl;
cout << "isnan(1.0/0.0) : " << isnan(1.0/0.0) << endl;
cout << "isnan(-1.0/0.0) : " << isnan(-1.0/0.0) << endl;
cout << "isnan(sqrt(-1.0)): " << isnan(sqrt(-1.0)) << endl; return ;
}

编译和运行结果,如下所示

$ g++ test-nan.cpp
$ ./a.out
isnan(0.0) :
isnan(1.0/0.0) :
isnan(-1.0/0.0) :
isnan(sqrt(-1.0)):

如果在编译时增加 -std=c++11 ,采用C++ 2011标准编译程序,可能会出现如下错误:

$ g++ test-nan.cpp -std=c++
...
error: call of overloaded ‘isnan(double)’ is ambiguous
...

一个简单的解决方法是在所有的 isnan 宏或函数前,增加域操作符( :: ),修改后的示例代码如下:

/* isnan example */
#include <cmath> /* isnan, sqrt */
#include <iostream> using namespace std; int main()
{
cout << "isnan(0.0) : " << ::isnan(0.0) << endl;
cout << "isnan(1.0/0.0) : " << ::isnan(1.0/0.0) << endl;
cout << "isnan(-1.0/0.0) : " << ::isnan(-1.0/0.0) << endl;
cout << "isnan(sqrt(-1.0)): " << ::isnan(sqrt(-1.0)) << endl; return ;
}

保存后,重新编译运行即可.

Python 实现

Python 采用 numpy 数值数学库函数 np.isnan 检查 nan 值,示例代码 test-nan.py 如下:

#!/usr/bin/env python
# -*- coding: utf8 -*-
# author: klchang
from __future__ import print_function import numpy as np print ("isnan(0.0) : ", np.isnan(0.0))
print ("isnan(1.0/0.0) : ", np.isnan(np.true_divide(1.0, 0.0)))
print ("isnan(-1.0/0.0) : ", np.isnan(np.true_divide(-1.0, 0.0)))
print ("isnan(sqrt(-1.0)): ", np.isnan(np.sqrt(-1.0)))

运行输出结果,如下:

$ python test-nan.py
isnan(0.0) : False
...: RuntimeWarning: divide by zero encountered in true_divide
print ("isnan(1.0/0.0) : ", np.isnan(np.true_divide(1.0, 0.0)))
isnan(1.0/0.0) : False
...: RuntimeWarning: divide by zero encountered in true_divide
print ("isnan(-1.0/0.0) : ", np.isnan(np.true_divide(-1.0, 0.0)))
isnan(-1.0/0.0) : False
...: RuntimeWarning: invalid value encountered in sqrt
print ("isnan(sqrt(-1.0)): ", np.isnan(np.sqrt(-1.0)))
isnan(sqrt(-1.0)): True

参考资料

1. isnan macro/function - <cmath> reference. http://www.cplusplus.com/reference/cmath/isnan/

2. NaN - Wikipedia, the free encyclopedia. https://en.wikipedia.org/wiki/NaN

3. numpy isnan - NumPy Manual. https://docs.scipy.org/doc/numpy/reference/generated/numpy.isnan.html

4. Why is isnan ambigous and how to avoid it? - stackoverflow. https://stackoverflow.com/questions/33770374/why-is-isnan-ambiguous-and-how-to-avoid-it

最新文章

  1. 实战使用Axure设计App,使用WebStorm开发(6) – 迈向后端
  2. .net高级技术(class0515)
  3. KafkaSpout: PartitionManager的行为分析
  4. Java中一些常用的代码
  5. Linux一些命令
  6. maven学习之1
  7. C#使用Xamarin开发可移植移动应用终章(11.获取设备信息与常用组件,开源一个可开发模版.)
  8. Scala:函数和闭包
  9. NodeJs之文件上传
  10. vscode添加prettier格式化自动加分号问题
  11. SpringMVC接收json数组对象
  12. Day039--HTML
  13. 是否能设计一种DNN的特定网络结构来改善DNN,使得其学习起来更加高效
  14. Linux atop 监控系统状态
  15. [OC] Delegate的使用
  16. SpringBoot Laravel(artisan serve) MIXPHP简单性能测试
  17. python变量存储和深浅拷贝
  18. wshShell.SendKeys模拟键盘操作
  19. hdu 4055 Number String (基础dp)
  20. postgres主从配置

热门文章

  1. spring boot快速入门 3: controller的使用
  2. zend studio 连PHP自带系统函数 常量都不提示
  3. ServiceLoader解读
  4. 033-JsonUtils 工具类模板
  5. Oracle 创建表空间和用户
  6. PHP之mb_strpos使用
  7. Gradle中的SourceSet理解
  8. HDU 2191 悼念512汶川大地震遇难同胞
  9. HTML基础-常用标签及图片
  10. WPF: RenderTransform特效