非常教程

C参考手册

数值 | Numerics

fegetexceptflag

在头文件<fenv.h>中定义

int fegetexceptflag(fexcept_t * flagp,int excepts);

(1)

(自C99以来)

int fesetexceptflag(const fexcept_t * flagp,int excepts);

(2)

(自C99以来)

1)尝试获取位掩码参数excepts中列出的浮点异常标志的全部内容,这是浮点异常宏的按位或。

2)尝试将浮点异常标志的全部内容复制到浮点环境中。 不会引发任何异常,只会修改标志。

浮点异常标志的全部内容不一定是一个布尔值,指示是否引发或清除异常。 例如,它可能是一个包含布尔状态和触发异常的代码地址的结构。 这些函数获取所有这些内容并以实现定义的格式将其存储在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;
 
    /* 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

参考

  • C11标准(ISO / IEC 9899:2011):
    • 7.6.2.2 fegetexcept标志功能(p:210)
    • 7.6.2.4 fesetexcept标志功能(p:211)
  • C99标准(ISO / IEC 9899:1999):
    • 7.6.2.2 fegetexcept标志功能(p:191)
    • 7.6.2.4 fesetexceptflag函数(p:192)

扩展内容

| 用于fegetexceptflag,fesetexceptflag的C ++文档 |

|:----|

C

C 语言是一门通用计算机编程语言,应用广泛。C 语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。