C++ Standard Library C++ STL Library

C++ <cfenv> - feupdateenv() Function



The C++ <cfenv> feupdateenv() function attempts to establish the state of the floating-point environment from the object pointed to by envp. Then, it then attempts to raise the floating-point exceptions that were saved.

This function can be used to end the non-stop mode established by an earlier call to feholdexcept.

Syntax

int feupdateenv(const fenv_t* envp);

Parameters

envp Specify pointer to the object of type fenv_t set by an earlier call to fegetenv() or feholdexcept() or equal to FE_DFL_ENV macro constant.

Return Value

Returns 0 on success. A non-zero value otherwise.

Exception

This function never throws exceptions.

Example:

The example below shows the usage of feupdateenv() function.

#include <iostream>
#include <cfenv>
#include <cmath>

#pragma STDC FENV_ACCESS ON

using namespace std;

void FE_Exceptions_Message(void) {
  cout<<"Exceptions raised:";
  if(fetestexcept(FE_DIVBYZERO)) 
    cout<<" FE_DIVBYZERO";
  if(fetestexcept(FE_INEXACT))   
    cout<<" FE_INEXACT";
  if(fetestexcept(FE_INVALID))   
    cout<<" FE_INVALID";
  if(fetestexcept(FE_OVERFLOW))  
    cout<<" FE_OVERFLOW";
  if(fetestexcept(FE_UNDERFLOW)) 
    cout<<" FE_UNDERFLOW";
  if(fetestexcept(FE_ALL_EXCEPT) == 0) 
    cout<<" None";
  cout<<endl;
}

void Rounding_Direction_Message(void) {
  cout<<"Current rounding method: ";
  switch(fegetround()) {
    case FE_DOWNWARD: 
      cout<<"DOWNWARD"; break;
    case FE_TONEAREST:   
      cout<<"TO-NEAREST"; break;
    case FE_TOWARDZERO:   
      cout<<"TOWARD-ZERO"; break;
    case FE_UPWARD:  
      cout<<"UPWARD"; break;
    default:
      cout<<"unknown";
  }
  cout<<endl;
}

void FE_Environment_Message(void) {
  FE_Exceptions_Message();
  Rounding_Direction_Message();
  cout<<endl;
}  

int main (){
  //creating fenv_t object
  fenv_t curr_env; 

  feraiseexcept(FE_INEXACT);
  //displaying the current environment
  FE_Environment_Message();

  //saving and clearing the current environment
  feholdexcept(&curr_env);

  //changing the current environment
  //and displaying it
  sqrt(-1);
  fesetround(FE_DOWNWARD);
  FE_Environment_Message();

  //updating current environment with the
  //previous environment and displaying it
  feupdateenv(&curr_env);
  FE_Environment_Message();

  return 0;
}

The output of the above code will be:

Exceptions raised: FE_INEXACT
Current rounding method: TO-NEAREST

Exceptions raised: FE_INVALID
Current rounding method: DOWNWARD

Exceptions raised: FE_INEXACT FE_INVALID
Current rounding method: TO-NEAREST

❮ C++ <cfenv> Library