FE_DIVBYZERO、FE_INEXACT、FE_INVALID、FE_OVERFLOW、FE_UNDERFLOW、FE_ALL_EXCEPT
来自 cppreference.cn
定义于头文件 <fenv.h> |
||
#define FE_DIVBYZERO /*implementation defined power of 2*/ |
(since C99) | |
#define FE_INEXACT /*implementation defined power of 2*/ |
(since C99) | |
#define FE_INVALID /*implementation defined power of 2*/ |
(since C99) | |
#define FE_OVERFLOW /*implementation defined power of 2*/ |
(since C99) | |
#define FE_UNDERFLOW /*implementation defined power of 2*/ |
(since C99) | |
#define FE_ALL_EXCEPT FE_DIVBYZERO | FE_INEXACT | \ FE_INVALID | FE_OVERFLOW | \ |
(since C99) | |
所有这些宏常量(除了 FE_ALL_EXCEPT)都展开为不同的 2 的幂的整数常量表达式,这些表达式唯一地标识所有受支持的浮点异常。每个宏仅在其受支持时才被定义。
宏常量 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 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