feholdexcept
在头文件<fenv.h>中定义  |   |   | 
|---|---|---|
int feholdexcept(fenv_t * envp);  |   | (自C99以来)  | 
首先,将当前的浮点环境保存到envp所指向的对象(类似于fegetenv),然后清除所有浮点状态标志,然后安装不间断模式:将来的浮点异常不会中断执行( 不会陷入),直到通过feupdateenv或fesetenv恢复浮点环境。
这个函数可以用在一个子程序的开始,它必须隐藏它可能从调用者引发的浮点异常。 如果只有一些异常必须被抑制,而其他异常必须被报告,那么在清除不需要的异常之后,通常以不间断模式结束并调用feupdateenv。
参数
envp  | -  | pointer to the object of type fenv_t where the floating-point environment will be stored  | 
|---|
返回值
成功时为0,否则为非零。
例
#include <stdio.h>
#include <fenv.h>
#include <float.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");
}
 
double x2 (double x)   /* times two */
{
    fenv_t curr_excepts;
 
    /* Save and clear current f-p environment. */
    feholdexcept(&curr_excepts);
 
    /* Raise inexact and overflow exceptions. */
    printf("In x2():  x = %f\n", x=x*2.0);
    show_fe_exceptions();
    feclearexcept(FE_INEXACT);   /* hide inexact exception from caller */
 
    /* Merge caller's exceptions (FE_INVALID)        */
    /* with remaining x2's exceptions (FE_OVERFLOW). */
    feupdateenv(&curr_excepts);
    return x;
}
 
int main(void)
{    
    feclearexcept(FE_ALL_EXCEPT);
    feraiseexcept(FE_INVALID);   /* some computation with invalid argument */
    show_fe_exceptions();
    printf("x2(DBL_MAX) = %f\n", x2(DBL_MAX));
    show_fe_exceptions();
 
    return 0;
}输出:
current exceptions raised:  FE_INVALID
In x2():  x = inf
current exceptions raised:  FE_INEXACT FE_OVERFLOW
x2(DBL_MAX) = inf
current exceptions raised:  FE_INVALID FE_OVERFLOW参考
- C11标准(ISO / IEC 9899:2011): 
- 7.6.4.2 feholdexcept功能(p:213-214)
 
 - C99标准(ISO / IEC 9899:1999): 
- 7.6.4.2 feholdexcept功能(p:194-195)
 
 
扩展内容
feupdateenv(C99)  | 恢复浮点环境并引发以前引发的异常(函数)  | 
|---|---|
fegetenvfesetenv(C99)  | 保存或恢复当前的浮点环境(功能)  | 
FE_DFL_ENV(C99)  | 默认浮点环境(宏常量)  | 
| 用于feholdexcept的C ++文档 |
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

