命名空间
变体
操作

FP_NORMAL, FP_SUBNORMAL, FP_ZERO, FP_INFINITE, FP_NAN

来自 cppreference.cn
< c‎ | numeric‎ | math
 
 
 
常用数学函数
函数
基本运算
(C99)
(C99)
(C99)
(C99)(C99)(C99)(C23)
最大/最小值运算
(C99)
(C99)
指数函数
(C23)
(C99)
(C99)
(C23)
(C23)

(C99)
(C99)(C23)
(C23)
(C23)
幂函数
(C99)
(C23)
(C23)

(C99)
(C23)
(C23)
三角函数和双曲函数
(C23)
(C23)
(C23)
(C23)
(C99)
(C99)
(C99)
最接近整数的浮点数
(C99)(C99)(C99)
(C99)

(C99)(C99)(C99)
(C23)(C23)(C23)(C23)
浮点数操作
(C99)(C99)
(C99)(C23)
(C99)
窄化操作
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
量子和量子指数
十进制重编码函数
全序和有效载荷函数
分类
(C99)
(C99)
(C99)
(C23)
误差和伽玛函数
(C99)
(C99)
(C99)
(C99)
类型
宏常量
特殊浮点数值
(C99)(C23)
参数和返回值
FP_NORMALFP_SUBNORMALFP_ZEROFP_INFINITEFP_NAN
(C99)(C99)(C99)(C99)(C99)
错误处理
快速运算指示符
 
在头文件 <math.h> 中定义
#define FP_NORMAL    /*实现定义*/
(自 C99 起)
#define FP_SUBNORMAL /*实现定义*/
(自 C99 起)
#define FP_ZERO      /*实现定义*/
(自 C99 起)
#define FP_INFINITE  /*实现定义*/
(自 C99 起)
#define FP_NAN       /*实现定义*/
(自 C99 起)

FP_NORMALFP_SUBNORMALFP_ZEROFP_INFINITEFP_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

[编辑] 参考

  • C17 标准 (ISO/IEC 9899:2018)
  • 7.12/6 FP_NORMAL, ... (p: 169-170)
  • C11 标准 (ISO/IEC 9899:2011)
  • 7.12/6 FP_NORMAL, ... (p: 232)
  • C99 标准 (ISO/IEC 9899:1999)
  • 7.12/6 FP_NORMAL, ... (p: 213)

[编辑] 参见

对给定的浮点数值进行分类
(函数宏) [编辑]
C++ 文档 关于 FP_categories