在头文件中定义int feupdateenv(const fenv_t * envp);(自C99以来)
首先,记住当前浮点异常,然后从envp指向的对象(类似于fesetenv)恢复浮点环境,然后引发已保存的浮点异常。
这个功能可以用来结束早期调用feholdexcept所建立的不间断模式。
参数
envp-pointer to the object of type fenv_t set by an earlier call to feholdexcept or fegetenv or equal to FE_DFL_ENV
返回值
成功时为0,否则为非零。
例
#include #include #include
#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_OVERFLOWx2(DBL_MAX) = inf
current exceptions raised: FE_INVALID FE_OVERFLOW
参考
C11标准(ISO / IEC 9899:2011):7.6.4.4 feupdateenv函数(p:214-215)
C99标准(ISO / IEC 9899:1999):7.6.4.4 feupdateenv函数(p:195-196)
扩展内容
feholdexcept(C99)保存环境,清除所有状态标志并忽略所有将来的错误(功能)fegetenvfesetenv(C99)保存或恢复当前的浮点环境(功能)
FE_DFL_ENV(C99)默认浮点环境(宏常量)
| feupdateenv的C ++文档 |