C++ feupdateenv()

The feupdateenv() function first saves currently raised floating-point exceptions, restores the floating-point environment from the given fenv_t object, then raises the exceptions saved previously.

The feupdateenv() function is defined in <cfenv> header file.

feupdateenv() prototype

int feupdateenv( fenv_t* envp );

The feupdateenv() function takes a pointer of type fenv_t as its argument which holds a floating point environment previously set by using feholdexcept or fegetenv and restores that floating point environment along with the current environment.


feupdateenv() Parameters

  • envp: Pointer to the fenv_t object that is set by an earlier call to feholdexcept or fegetenv or is equal to FE_DFL_ENV.

feupdateenv() Return value

  • On success, the feupdateenv() function returns 0.
  • On failure, it returns nonzero.

Example: How feupdateenv() function works?

#include <iostream>
#include <cmath>
#include <cfenv>
#pragma STDC FENV_ACCESS ON
using namespace std;

void print_exceptions()
{
	cout << "Raised exceptions: ";
	if(fetestexcept(FE_ALL_EXCEPT))
	{
		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 ";
	}
	else
		cout << "None";

	cout << endl;
}

int main()
{
	fenv_t envp;

	/* raise certain exceptions */
	feraiseexcept(FE_INVALID|FE_DIVBYZERO);
	print_exceptions();
	
	/* saves and clears current exceptions */
	feupdateenv(&envp);
	print_exceptions();
	
	/* restores saved exceptions */
	feupdateenv(&envp);
	print_exceptions();
	
	return 0;
}

When you run the program, the output will be:

Raised exceptions: FE_DIVBYZERO FE_INVALID
Raised exceptions: None
Raised exceptions: FE_DIVBYZERO FE_INVALID