Namespaces
Variants

fegetexceptflag, fesetexceptflag

From cppreference.net
< c ‎ | numeric ‎ | fenv
定义于头文件 <fenv.h>
int fegetexceptflag ( fexcept_t * flagp, int excepts ) ;
(1) (C99 起)
int fesetexceptflag ( const fexcept_t * flagp, int excepts ) ;
(2) (C99 起)

1) 尝试获取浮点异常标志的完整内容,这些标志列于位掩码参数 excepts 中,该参数是 浮点异常宏 的按位或运算结果。

2) 尝试将 excepts 中列出的浮点异常标志完整内容从 flagp 复制到浮点环境中。不会触发任何异常,仅修改标志位。

浮点异常标志的完整内容不一定只是表示异常是否被触发或清除的布尔值。例如,它可能是一个结构体,包含布尔状态以及触发异常的代码地址。这些函数以实现定义的格式获取所有此类内容,并将其获取/存储到 flagp 中。

目录

参数

flagp - 指向 fexcept_t 对象的指针,用于存储或读取异常标志
excepts - 列出要获取/设置的异常标志的位掩码

返回值

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;
    /* 设置一组"当前"异常标志 */
    feraiseexcept(FE_INVALID);
    show_fe_exceptions();
    /* 保存当前异常标志 */
    fegetexceptflag(&excepts,FE_ALL_EXCEPT);
    /* 临时触发另外两个异常 */
    feclearexcept(FE_ALL_EXCEPT);
    feraiseexcept(FE_OVERFLOW | FE_INEXACT);
    show_fe_exceptions();
    /* 恢复之前的异常标志 */
    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

参考文献

  • C11 标准 (ISO/IEC 9899:2011):
  • 7.6.2.2 fegetexceptflag 函数 (页: 210)
  • 7.6.2.4 fesetexceptflag 函数 (页: 211)
  • C99 标准 (ISO/IEC 9899:1999):
  • 7.6.2.2 fegetexceptflag 函数 (页: 191)
  • 7.6.2.4 fesetexceptflag 函数 (页: 192)

参考

C++ 文档 for fegetexceptflag , fesetexceptflag