C
数值 | Numerics

FE_INVALID

FE_DIVBYZERO, FE_INEXACT, FE_INVALID, FE_OVERFLOW, FE_UNDERFLOW, FE_ALL_EXCEPT

在头文件中定义
#define FE_DIVBYZERO /*implementation defined power of 2*/(自C99)
#define FE_INEXACT /*implementation defined power of 2*/(自 C99)
#define FE_INVALID /*implementation defined power of 2*/(自 C99)
#define FE_OVERFLOW /*implementation defined power of 2*/(自C99)
#define FE_UNDERFLOW /*implementation defined power of 2*/(自C99)
#define FE_ALL_EXCEPT FE_DIVBYZERO | FE_INEXACT | \ FE_INVALID | FE_OVERFLOW | \ FE_UNDERFLOW(自 C99)

所有这些宏常量(FE_ALL_EXCEPT除外)都展开为不同幂的2的整型常量表达式,它们唯一标识所有受支持的浮点异常。 每个宏只在被支持时才被定义。

扩展为所有其他FE_ *的按位OR的宏常量FE_ALL_EXCEPT始终定义,如果实现不支持浮点异常,则该常量将为零。

常量说明
FE_DIVBYZERO极点错误发生在较早的浮点运算中
FE_INEXACT不精确的结果:舍入对于存储较早浮点运算的结果是必要的
FE_INVALID在早期的浮点操作中发生域错误
FE_OVERFLOW较早浮点运算的结果太大而无法表示
FE_UNDERFLOW早期浮点运算的结果是低于正常的,并且精度下降
FE_ALL_EXCEPT按位或所有受支持的浮点异常

该实现可以在<fenv.h>中定义附加的宏常量来标识附加的浮点异常。 所有这些常量都以FE_开头,后跟至少一个大写字母。

有关更多详细信息,请参阅math_errhandling。

#include <stdio.h> #include <math.h> #include <float.h> #include <fenv.h> #pragma STDC FENV_ACCESS ON void show_fe_exceptions(void) { printf("exceptions raised:" if(fetestexcept(FE_DIVBYZERO)) printf(" FE_DIVBYZERO" if(fetestexcept(FE_INEXACT)) printf(" FE_INEXACT" if(fetestexcept(FE_INVALID)) printf(" FE_INVALID" if(fetestexcept(FE_OVERFLOW)) printf(" FE_OVERFLOW" if(fetestexcept(FE_UNDERFLOW)) printf(" FE_UNDERFLOW" feclearexcept(FE_ALL_EXCEPT printf("\n" } int main(void) { printf("MATH_ERREXCEPT is %s\n", math_errhandling & MATH_ERREXCEPT ? "set" : "not set" printf("0.0/0.0 = %f\n", 0.0/0.0 show_fe_exceptions( printf("1.0/0.0 = %f\n", 1.0/0.0 show_fe_exceptions( printf("1.0/10.0 = %f\n", 1.0/10.0 show_fe_exceptions( printf("sqrt(-1) = %f\n", sqrt(-1) show_fe_exceptions( printf("DBL_MAX*2.0 = %f\n", DBL_MAX*2.0 show_fe_exceptions( printf("nextafter(DBL_MIN/pow(2.0,52),0.0) = %.1f\n", nextafter(DBL_MIN/pow(2.0,52),0.0) show_fe_exceptions( }

可能的输出:

MATH_ERREXCEPT is set 0.0/0.0 = nan exceptions raised: FE_INVALID 1.0/0.0 = inf exceptions raised: FE_DIVBYZERO 1.0/10.0 = 0.100000 exceptions raised: FE_INEXACT sqrt(-1) = -nan exceptions raised: FE_INVALID DBL_MAX*2.0 = inf exceptions raised: FE_INEXACT FE_OVERFLOW nextafter(DBL_MIN/pow(2.0,52),0.0) = 0.0 exceptions raised: FE_INEXACT FE_UNDERFLOW

参考

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

扩展内容

math_errhandlingMATH_ERRNOMATH_ERREXCEPT(C99)(C99)(C99)定义了常用数学函数(宏常量)使用的错误处理机制,

| 用于浮点异常宏的C ++文档 |