FE_DIVBYZERO, FE_INEXACT, FE_INVALID, FE_OVERFLOW, FE_UNDERFLOW, FE_ALL_EXCEPT

来自cppreference.com
< c‎ | numeric‎ | fenv


在标头 <fenv.h> 定义
#define FE_DIVBYZERO    /* 由实现定义的二的幂 */
(C99 起)
#define FE_INEXACT      /* 由实现定义的二的幂 */
(C99 起)
#define FE_INVALID      /* 由实现定义的二的幂 */
(C99 起)
#define FE_OVERFLOW     /* 由实现定义的二的幂 */
(C99 起)
#define FE_UNDERFLOW    /* 由实现定义的二的幂 */
(C99 起)
#define FE_ALL_EXCEPT  FE_DIVBYZERO | FE_INEXACT | \

                       FE_INVALID | FE_OVERFLOW |  \

                       FE_UNDERFLOW
(C99 起)

所有这些宏常量(除了 FE_ALL_EXCEPT)都展开成二的不同次幂的整数常量表达式,唯一地鉴别所有受支持浮点数异常。每个宏仅若受支持才得到定义。

宏常量 FE_ALL_EXCEPT 展开成所有其他 FE_* 的逐位或,且始终有定义,若实现不支持浮点数异常,则为零。

常量 解释
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 ONvoid 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):
  • 7.6/6 Floating-point environment <fenv.h> (第 207 页)
  • C99 标准(ISO/IEC 9899:1999):
  • 7.6/5 Floating-point environment <fenv.h> (第 188 页)

[编辑]参阅

定义用于常用数学函数的错误处理机制
(宏常量)[编辑]
浮点数异常宏C++ 文档
close