C
数值 | Numerics

FP_INFINITE

FP_NORMAL, FP_SUBNORMAL, FP_ZERO, FP_INFINITE, FP_NAN

在头文件中定义
#define FP_NORMAL / *实现定义* /(自C99以来)
#define FP_SUBNORMAL / *实现定义* /(自C99以来)
#define FP_ZERO / *实现定义* /(自C99以来)
#define FP_INFINITE / *实现定义* /(自C99以来)
#define FP_NAN / *实现定义* /(自C99以来)

FP_NORMAL,FP_SUBNORMAL,FP_ZERO,FP_INFINITE,FP_NAN宏分别表示不同类别的浮点数。 它们都扩展为整型常量表达式。

常量说明
FP_NORMAL表示该值是正常的,即不是无穷大,低于正常值,非数值或零
FP_SUBNORMAL表示该值是低于正常的
FP_ZERO表示该值为正值或负值为零
FP_INFINITE表示该值不能由潜在类型表示(正或负无穷)
FP_NAN表示该值不是数字(NaN)

#include <stdio.h> #include <math.h> #include <float.h> const char *show_classification(double x) { switch(fpclassify(x)) { case FP_INFINITE: return "Inf"; case FP_NAN: return "NaN"; case FP_NORMAL: return "normal"; case FP_SUBNORMAL: return "subnormal"; case FP_ZERO: return "zero"; default: return "unknown"; } } int main(void) { printf("1.0/0.0 is %s\n", show_classification(1/0.0) printf("0.0/0.0 is %s\n", show_classification(0.0/0.0) printf("DBL_MIN/2 is %s\n", show_classification(DBL_MIN/2) printf("-0.0 is %s\n", show_classification(-0.0) printf(" 1.0 is %s\n", show_classification(1.0) }

输出:

1.0/0.0 is Inf 0.0/0.0 is NaN DBL_MIN/2 is subnormal -0.0 is zero 1.0 is normal

参考

  • C11标准(ISO / IEC 9899:2011):

扩展内容

fpclassify (C99)classifies the given floating-point value (function)

| FP_categories的C ++文档 |