C++ <cfenv> - fetestexcept() Function
The C++ <cfenv> fetestexcept() function returns the floating point exceptions currently set, among those specified by excepts. The argument excepts is a bitwise OR of the floating point exception macros and the value returned by the function is the bitwise OR representation of the subset of excepts that are currently set in the floating point environment.
Syntax
int fetestexcept (int excepts);
Parameters
excepts |
Specify bitmask listing the floating-point exception flags to test.
Certain library implementations may define additional macro constants in <cfenv> to identify additional floating-point exceptions (with their corresponding macros also beginning with FE_). |
Return Value
Returns 0 if none of the exceptions in excepts are set. Otherwise returns the exceptions (among those of excepts) currently set.
Exception
This function never throws exceptions.
Example:
In the example below, the fetestexcept() function is used to clear floating-point exceptions.
#include <iostream> #include <cfenv> #pragma STDC FENV_ACCESS ON using namespace std; void Show_Exceptions() { if(fetestexcept(FE_DIVBYZERO)) cout<<"Exceptions raised: FE_DIVBYZERO \n"; if(fetestexcept(FE_INEXACT)) cout<<"Exceptions raised: FE_INEXACT \n"; if(fetestexcept(FE_INVALID)) cout<<"Exceptions raised: FE_INVALID \n"; if(fetestexcept(FE_OVERFLOW)) cout<<"Exceptions raised: FE_OVERFLOW \n"; if(fetestexcept(FE_UNDERFLOW)) cout<<"Exceptions raised: FE_UNDERFLOW \n"; if(fetestexcept(FE_ALL_EXCEPT) == 0) cout<<"Exceptions raised: None \n"; } int main (){ cout<<"On startup: \n"; Show_Exceptions(); //raising FE_INVALID and FE_INEXACT exceptions feraiseexcept(FE_INVALID | FE_INEXACT); cout<<"\nBefore restoration:\n"; Show_Exceptions(); //resetting to default environment fesetenv(FE_DFL_ENV); cout<<"\nAfter reset to default:\n"; Show_Exceptions(); return 0; }
The output of the above code will be:
On startup: Exceptions raised: None Before restoration: Exceptions raised: FE_INEXACT Exceptions raised: FE_INVALID After reset to default: Exceptions raised: None
❮ C++ <cfenv> Library