feupdateenv() function in C++
Last Updated :
31 Jan, 2022
The feupdateenv() function in C++ first saves currently raised floating-point exceptions. It restores the floating-point environment from the given fenv_t object and then raises the exceptions which were saved previously.
Syntax:
int feupdateenv( fenv_t* envp )
Parameters: It accepts a single mandatory parameter envp which specifies the pointer to the fenv_t object that is set by an earlier call to feholdexcept or fegetenv or is equal to FE_DFL_ENV. The function also accepts 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.
Return Value: The function returns two kinds of value described below:
- It returns zero on success
- It returns non-zero value on failure
Below programs illustrate the above function.
Program 1:
CPP
#include <bits/stdc++.h>
#pragma STDC FENV_ACCESS on
double answer( double y)
{
fenv_t trial;
feholdexcept(&trial);
y = log (y);
feclearexcept(FE_OVERFLOW | FE_DIVBYZERO);
feupdateenv(&trial);
return y;
}
int main()
{
feclearexcept(FE_ALL_EXCEPT);
printf (" log (0.0): %f\n", answer(0.0));
if (!fetestexcept(FE_ALL_EXCEPT)) {
printf ("no exceptions raised");
}
return 0;
}
|
Output:
log(0.0): -inf
no exceptions raised
Program 2:
CPP
#include <bits/stdc++.h>
#pragma STDC FENV_ACCESS on
double answer( double y)
{
fenv_t trial;
feholdexcept(&trial);
y = log (y);
feclearexcept(FE_OVERFLOW | FE_DIVBYZERO);
feupdateenv(&trial);
return y;
}
int main()
{
feclearexcept(FE_ALL_EXCEPT);
printf (" log (10.0): %f\n", answer(10.0));
if (!fetestexcept(FE_ALL_EXCEPT)) {
printf ("no exceptions raised");
}
else
printf ("exceptions raised");
return 0;
}
|
Output:
log(10.0): 2.302585
exceptions raised
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...