當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C++ feupdateenv()用法及代碼示例


C++中的feupdateenv()函數首先保存當前引發的浮點異常。它從給定的fenv_t對象還原浮點環境,然後引發先前保存的異常。

用法:

int feupdateenv( fenv_t* envp )

參數:它接受單個強製性參數envp,該參數指定指向fenv_t對象的指針,該指針由先前對feholdexcept或fegetenv的調用設置,或者等於FE_DFL_ENV。該函數還接受類型為fenv_t的指針作為其參數,該指針保存先前通過使用feholdexcept或fegetenv設置的浮點環境,並將該浮點環境與當前環境一起恢複。


返回值:該函數返回以下兩種值:

  • 成功返回零
  • 失敗時返回非零值

以下示例程序旨在說明上述函數。

示例1:

// C++ program to illustrate the 
// feupdateenv() function 
#include <bits/stdc++.h> 
#pragma STDC FENV_ACCESS on 
  
// Function to use the fucntion 
double answer(double y) 
{ 
  
    // struct defined 
    fenv_t trial; 
  
    // use the function feholdexcept 
    feholdexcept(&trial); 
  
    // find log valye 
    y = log(y); 
  
    // clears exception 
    feclearexcept(FE_OVERFLOW | FE_DIVBYZERO); 
  
    // call the function for succes or not 
    feupdateenv(&trial); 
    return y; 
} 
  
int main() 
{ 
    // It is a combination of all of 
    // the possible floating-point exception 
    feclearexcept(FE_ALL_EXCEPT); 
  
    // it returns the log value 
    // if it is to be found 
    printf("log(0.0): %f\n", answer(0.0)); 
  
    // the function does not throws any exception 
    if (!fetestexcept(FE_ALL_EXCEPT)) { 
        printf("no exceptions raised"); 
    } 
    return 0; 
}

輸出:

log(0.0): -inf
no exceptions raised

示例2:

// C++ program to illustrate the 
// feupdateenv() function 
#include <bits/stdc++.h> 
#pragma STDC FENV_ACCESS on 
  
// Function to use the fucntion 
double answer(double y) 
{ 
  
    // struct defined 
    fenv_t trial; 
  
    // use the function feholdexcept 
    feholdexcept(&trial); 
  
    // find log valye 
    y = log(y); 
  
    // clears exception 
    feclearexcept(FE_OVERFLOW | FE_DIVBYZERO); 
  
    // call the function for succes or not 
    feupdateenv(&trial); 
    return y; 
} 
  
int main() 
{ 
    // It is a combination of all of 
    // the possible floating-point exception 
    feclearexcept(FE_ALL_EXCEPT); 
  
    // it returns the log value 
    // if it is to be found 
    printf("log(10.0): %f\n", answer(10.0)); 
  
    // the function does not throws any exception 
    if (!fetestexcept(FE_ALL_EXCEPT)) { 
        printf("no exceptions raised"); 
    } 
    else
        printf("exceptions raised"); 
  
    return 0; 
}

輸出:

log(10.0): 2.302585
exceptions raised


相關用法


注:本文由純淨天空篩選整理自AmanSrivastava1大神的英文原創作品 feupdateenv() function in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。