前言

在数据处理输入输出时,极有可能遇到数据读入空值(极大、极小)、运算中分母为0或0.0,对0取对数等操作,这将产生nan或inf的产生。这篇博文旨在分析C/C++产生nan和inf的操作及判断是否有nan或inf产生。

NAN的产生原因

nan: not a number,表示“无效数字”。image.png
注意:nan是无序的(unordered),无法对其进行逻辑运算。它不大于、小于或等于任何数(包括它自己),将<,>,<=,和>=作用于nan产生一个exception。得到nan时就查看是否有非法操作,如果表达式中含有nan,那么表达式的结果为nan。

INF的产生原因

INF:infinite,表示“无穷大”。
超出浮点数的表示范围(溢出,即阶码部分超过其能表示的最大值)。
image.png
注意:+inf大于任何数(除了它自己和nan),-inf小于任何数(除了它自己和nan),得到inf时就查看是否有溢出或者除以0。inf在C语言表达式中就表示数学里无限的概念,如1.0/inf等于0.0,并可以与其他浮点数进行比较的(可以参与<=、>+、==、!=等运算)。

nan和inf的判断

下面几个宏即包含在math.h头文件,可用于判断一个表达式的结果是否为inf、nan或其他。使用时包括include

  1. include<math.h>
  2. int isfinite(x);
  3. int isnormal(x);
  4. int isnan(x);
  5. int isinf(x);

打开math.h可以看到定义:

  1. #define isnormal(x) \
  2. ( sizeof(x) == sizeof(float) ? __inline_isnormalf((float)(x)) \
  3. : sizeof(x) == sizeof(double) ? __inline_isnormald((double)(x)) \
  4. : __inline_isnormall((long double)(x)))
  5. #define isfinite(x) \
  6. ( sizeof(x) == sizeof(float) ? __inline_isfinitef((float)(x)) \
  7. : sizeof(x) == sizeof(double) ? __inline_isfinited((double)(x)) \
  8. : __inline_isfinitel((long double)(x)))
  9. #define isinf(x) \
  10. ( sizeof(x) == sizeof(float) ? __inline_isinff((float)(x)) \
  11. : sizeof(x) == sizeof(double) ? __inline_isinfd((double)(x)) \
  12. : __inline_isinfl((long double)(x)))
  13. #define isnan(x) \
  14. ( sizeof(x) == sizeof(float) ? __inline_isnanf((float)(x)) \
  15. : sizeof(x) == sizeof(double) ? __inline_isnand((double)(x)) \
  16. : __inline_isnanl((long double)(x)))

使用方法,及结果:

  1. int isfinite(x) ,判断x是否有限,是返回1,其它返回0
  2. int isnormal(x),判断x是否为一个数(非infnan),是返回1,其它返回0
  3. int isnan(x),当xnan返回1,其它返回0
  4. int isinf(x) ,当x是正无穷是返回1,当x是负无穷时返回-1,其它返回0。有些编译器不区分。

测试

产生nan或inf的操作,使用库函数(宏)判断

  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4. int main(int argc, char *argv[])
  5. {
  6. //nan
  7. cout << "nan: " << sqrt(-1) << endl;
  8. cout << "nan: " << log(-1.0) << endl;
  9. cout << "nan: " << 0.0 / 0.0 << endl;
  10. cout << "nan: " << 0.0 * sqrt(-1) << endl;
  11. cout << "nan: " << sqrt(-1) / sqrt(-1) << endl;
  12. cout << "nan: " << sqrt(-1) - sqrt(-1) << endl;
  13. //inf
  14. cout << "inf: " << 1 / 0.0 << endl;
  15. cout << "-inf: " << -1 / 0.0 << endl;
  16. cout << "inf: " << 0.0 + 1 / 0.0 << endl;
  17. cout << "-inf: " << log(0) << endl;
  18. cout << "isfinite: 0" << isfinite(0.0 / 0.0) << endl;
  19. cout << "isfinite: 0" << isfinite(1 / 0.0) << endl;
  20. cout << "isfinite: 1" << isfinite(1.1) << endl;
  21. cout << "isnormal: 0" << isnormal(0.0 / 0.0) << endl;
  22. cout << "isnormal: 0" << isnormal(1 / 0.0) << endl;
  23. cout << "isnormal: 1" << isnormal(1.1) << endl;
  24. cout << "isnan: 1" << isnan(0.0 / 0.0) << endl;
  25. cout << "isnan: 0" << isnan(1 / 0.0) << endl;
  26. cout << "isnan: 0" << isnan(1.1) << endl;
  27. cout << "isinf: 0" << isinf(0.0 / 0.0) << endl;
  28. cout << "isinf: 1" << isinf(1 / 0.0) << endl;
  29. cout << "isinf: 0" << isinf(1.1) << endl;
  30. return 0;
  31. }

参考

http://www.gnu.org/software/libc/manual/html_node/Infinity-and-NaN.html
http://www.cnblogs.com/dosrun/p/3908617.html