Namespaces
Variants

fpclassify

From cppreference.net
< c ‎ | numeric ‎ | math
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99) (C99) (C99) (C23)
Maximum/minimum operations
Exponential functions
Power functions
Trigonometric and hyperbolic functions
Nearest integer floating-point
(C99) (C99) (C99)
(C23) (C23) (C23) (C23)
Floating-point manipulation
Narrowing operations
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
Quantum and quantum exponent
Decimal re-encoding functions
Total order and payload functions
Classification
fpclassify
(C99)
(C99)
(C99)
(C99)
(C23)
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Types
Macro constants
Special floating-point values
Arguments and return values
Error handling
Fast operation indicators
定义于头文件 <math.h>
#define fpclassify(arg) /* 由实现定义 */
(C99 起)

将浮点值 arg 分类到以下类别:零、次正规数、正规数、无穷大、NAN 或实现定义的类别。该宏返回一个整型值。

FLT_EVAL_METHOD 会被忽略:即使参数以比其类型更高的范围和精度进行计算,也会首先转换为其语义类型,分类基于该类型:一个正常的 long double 值在转换为 double 时可能变为次正规数,在转换为 float 时可能变为零。

目录

参数

arg - 浮点数值

返回值

以下之一: FP_INFINITE FP_NAN FP_NORMAL FP_SUBNORMAL FP_ZERO 或实现定义的类型,用于指定 arg 的类别。

示例

#include <float.h>
#include <math.h>
#include <stdio.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

参考文献

  • C23 标准 (ISO/IEC 9899:2024):
  • 7.12.3.1 fpclassify 宏 (p: TBD)
  • C17 标准 (ISO/IEC 9899:2018):
  • 7.12.3.1 fpclassify 宏 (p: TBD)
  • C11 标准 (ISO/IEC 9899:2011):
  • 7.12.3.1 fpclassify 宏 (页: 235)
  • C99标准(ISO/IEC 9899:1999):
  • 7.12.3.1 fpclassify宏(页:216)

参见

检查给定数值是否为有限值
(函数宏)
(C99)
检查给定数值是否为无穷大
(函数宏)
(C99)
检查给定数值是否为 NaN
(函数宏)
检查给定数值是否为规范数
(函数宏)
C++ documentation for fpclassify