fegetexceptflag, fesetexceptflag
来自 cppreference.cn
定义于头文件 <fenv.h> |
||
int fegetexceptflag( fexcept_t* flagp, int excepts ); |
(1) | (C99 起) |
int fesetexceptflag( const fexcept_t* flagp, int excepts ); |
(2) | (C99 起) |
1) 尝试获取位掩码参数 excepts
中列出的浮点异常标志的完整内容,excepts
是 浮点异常宏 的按位或。
2) 尝试将 excepts
中列出的浮点异常标志的完整内容从 flagp
复制到浮点环境。 不会引发任何异常,仅修改标志。
浮点异常标志的完整内容不一定是布尔值,指示异常是否被引发或清除。 例如,它可以是一个结构体,包括布尔状态和触发异常的代码地址。 这些函数获取所有此类内容,并以实现定义的格式在 flagp
中获取/存储它。
内容 |
[编辑] 参数
flagp | - | 指向 fexcept_t 对象的指针,标志将被存储或从中读取 |
excepts | - | 位掩码,列出要获取/设置的异常标志 |
[编辑] 返回值
0 成功时返回 0,否则返回非零值。
[编辑] 示例
运行此代码
#include <stdio.h> #include <fenv.h> #pragma STDC FENV_ACCESS ON void show_fe_exceptions(void) { printf("current 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"); if(fetestexcept(FE_ALL_EXCEPT)==0) printf(" none"); printf("\n"); } int main(void) { fexcept_t excepts; /* Setup a "current" set of exception flags. */ feraiseexcept(FE_INVALID); show_fe_exceptions(); /* Save current exception flags. */ fegetexceptflag(&excepts,FE_ALL_EXCEPT); /* Temporarily raise two other exceptions. */ feclearexcept(FE_ALL_EXCEPT); feraiseexcept(FE_OVERFLOW | FE_INEXACT); show_fe_exceptions(); /* Restore previous exception flags. */ fesetexceptflag(&excepts,FE_ALL_EXCEPT); show_fe_exceptions(); return 0; }
输出
current exceptions raised: FE_INVALID current exceptions raised: FE_INEXACT FE_OVERFLOW current exceptions raised: FE_INVALID