?
? ????? PHP ??? ???? ??? ?? ??
在頭文件<fenv.h>中定義 | ||
---|---|---|
int feholdexcept(fenv_t * envp); | (自C99以來) |
首先,將當(dāng)前的浮點(diǎn)環(huán)境保存到envp所指向的對(duì)象(類似于fegetenv),然后清除所有浮點(diǎn)狀態(tài)標(biāo)志,然后安裝不間斷模式:將來的浮點(diǎn)異常不會(huì)中斷執(zhí)行( 不會(huì)陷入),直到通過feupdateenv或fesetenv恢復(fù)浮點(diǎn)環(huán)境。
這個(gè)函數(shù)可以用在一個(gè)子程序的開始,它必須隱藏它可能從調(diào)用者引發(fā)的浮點(diǎn)異常。 如果只有一些異常必須被抑制,而其他異常必須被報(bào)告,那么在清除不需要的異常之后,通常以不間斷模式結(jié)束并調(diào)用feupdateenv。
envp | - | pointer to the object of type fenv_t where the floating-point environment will be stored |
---|
成功時(shí)為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_OVERFLOWx2(DBL_MAX) = inf current exceptions raised: FE_INVALID FE_OVERFLOW
C11標(biāo)準(zhǔn)(ISO / IEC 9899:2011):
7.6.4.2 feholdexcept功能(p:213-214)
C99標(biāo)準(zhǔn)(ISO / IEC 9899:1999):
7.6.4.2 feholdexcept功能(p:194-195)
feupdateenv(C99) | 恢復(fù)浮點(diǎn)環(huán)境并引發(fā)以前引發(fā)的異常(函數(shù)) |
---|---|
fegetenvfesetenv(C99) | 保存或恢復(fù)當(dāng)前的浮點(diǎn)環(huán)境(功能) |
FE_DFL_ENV(C99) | 默認(rèn)浮點(diǎn)環(huán)境(宏常量) |
| 用于feholdexcept的C ++文檔 |