fegetenv() and fesetenv() functions in C++
In this tutorial, we will learn about fegetenv() and fesetenv() functions in C++. These are inbuilt functions and are defined in <cfenv> header file.
fegetenv()
The syntax of this function is as follows:
int fegetenv( fenv_t* envp)
As given in the syntax, the function accepts one parameter which is a pointer to an object of type fenv_t that stores the status of the floating-point environment. It returns 0 on success and a non-zero value on failure. The fegetenv() function attempts to store the current status of the floating-point environment in a fenv_t object.
Have a look at the following C++ program to understand the working of the fegetenv() function.
#include <iostream> #include <cfenv> using namespace std; void print_env() { cout << "Raised Exception: "; if (fetestexcept(FE_ALL_EXCEPT)) { if (fetestexcept(FE_DIVBYZERO)) cout << "FE_DIVBYZERO " << endl; if (fetestexcept(FE_INEXACT)) cout << "FE_INEXACT " << endl; if (fetestexcept(FE_INVALID)) cout << "FE_INVALID " << endl; if (fetestexcept(FE_OVERFLOW)) cout << "FE_OVERFLOW " << endl; if (fetestexcept(FE_UNDERFLOW)) cout << "FE_UNDERFLOW " << endl; } else cout << "None" << endl; cout << "Rounding direction: "; switch (fegetround()) { case FE_TONEAREST: cout << "FE_TONEAREST"; break; case FE_DOWNWARD: cout << "FE_DOWNWARD"; break; case FE_UPWARD: cout << "FE_UPWARD"; break; case FE_TOWARDZERO: cout << "FE_TOWARDZERO"; break; default: cout << "unknown"; }; cout << "\n"; } int main() { fenv_t envp; cout << "Initial Environment:--" << endl; print_env(); fegetenv(&envp); feraiseexcept(FE_INEXACT); fesetround(FE_DOWNWARD); cout << "Final Environment:--" << endl; print_env(); return 0; }
Output:
Initial Environment:-- Raised Exception: None Rounding direction: FE_TONEAREST Final Environment:-- Raised Exception: FE_INEXACT Rounding direction: FE_DOWNWARD
fesetenv()
The syntax of this function is as follows:
int fesetenv( fenv_t* envp)
As given in the syntax, the function accepts one parameter which is a pointer to an object of type fenv_t that stores the status of the floating-point environment. Similar to fegetenv() function, it returns 0 on success and a non-zero value on failure. The fesetenv() function attempts to set the current status of the floating-point environment from a fenv_t object.
In the above example program, we can retrieve the initial environment using fesetenv() function. See the code given below.
#include <iostream> #include <cfenv> using namespace std; void print_env() { cout << "Raised Exception: "; if (fetestexcept(FE_ALL_EXCEPT)) { if (fetestexcept(FE_DIVBYZERO)) cout << "FE_DIVBYZERO " << endl; if (fetestexcept(FE_INEXACT)) cout << "FE_INEXACT " << endl; if (fetestexcept(FE_INVALID)) cout << "FE_INVALID " << endl; if (fetestexcept(FE_OVERFLOW)) cout << "FE_OVERFLOW " << endl; if (fetestexcept(FE_UNDERFLOW)) cout << "FE_UNDERFLOW " << endl; } else cout << "None" << endl; cout << "Rounding direction: "; switch (fegetround()) { case FE_TONEAREST: cout << "FE_TONEAREST"; break; case FE_DOWNWARD: cout << "FE_DOWNWARD"; break; case FE_UPWARD: cout << "FE_UPWARD"; break; case FE_TOWARDZERO: cout << "FE_TOWARDZERO"; break; default: cout << "unknown"; }; cout << "\n"; } int main() { fenv_t envp; cout << "Initial Environment:--" << endl; print_env(); fegetenv(&envp); feraiseexcept(FE_INEXACT); fesetround(FE_DOWNWARD); cout << "Final Environment:--" << endl; print_env(); cout << "Retrieving Initial Environment using fesetenv():--" << endl; fesetenv(&envp); print_env(); return 0; }
Output:
Initial Environment:-- Raised Exception: None Rounding direction: FE_TONEAREST Final Environment:-- Raised Exception: FE_INEXACT Rounding direction: FE_DOWNWARD Retrieving Initial Environment using fesetenv():-- Raised Exception: None Rounding direction: FE_TONEAREST
Also read: fesetround() and fegetround() in C++
Leave a Reply