名稱空間
變體
操作

feupdateenv

來自 cppreference.com
< c‎ | 數值‎ | fenv
定義於標頭檔案 <fenv.h>
int feupdateenv( const fenv_t* envp );
(C99 起)

首先,記住當前已引發的浮點異常,然後從 envp 指向的物件中恢復浮點環境(類似於 fesetenv),然後引發已儲存的浮點異常。

此函式可用於結束透過先前呼叫 feholdexcept 建立的非停止模式。

目錄

[編輯] 引數

envp - 指向由先前呼叫 feholdexceptfegetenv 設定的 fenv_t 型別物件的指標,或等於 FE_DFL_ENV

[編輯] 返回值

成功時返回 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.4 feupdateenv 函式 (p: 214-215)
  • C99 標準 (ISO/IEC 9899:1999)
  • 7.6.4.4 feupdateenv 函式 (p: 195-196)

[編輯] 參閱

儲存環境,清除所有狀態標誌並忽略所有未來錯誤
(函式) [編輯]
儲存或恢復當前浮點環境
(函式) [編輯]
預設浮點環境
(宏常量) [編輯]
C++ 文件 for feupdateenv