fpclassify
来自cppreference.com
| 定义于头文件 <math.h>
|
||
| #define fpclassify(arg) /* implementation defined */ |
(C99 起) | |
将浮点值arg分类到下列类别:零、非正规、正规、无穷大、NaN,或实现定义的类别。该宏返回整数值。
FLT_EVAL_METHOD被忽略:即使参数求值结果大于其类型的范围和精度,它首先还是会被转换到其语义类型,而分类基于该类型:一个正规的long double值可能在转换到double时变得非正规,而且在转换到float时变成零。
目录 |
[编辑] 参数
| arg | - | 浮点值 |
[编辑] 返回值
FP_INFINITE、 FP_NAN、 FP_NORMAL、 FP_SUBNORMAL、 FP_ZERO或实现定义的类型之一,指定arg的类别。
[编辑] 示例
运行此代码
#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

