Skip to content
Related Articles

Related Articles

Improve Article

feclearexcept in C++ with Examples

  • Last Updated : 01 Nov, 2018

feclearexcept() clears the supported floating-point exceptions represented by excepts.
Syntax:

int feclearexcept(int excepts);
excepts : Bitmask listing of exception flags to clear

Return value:
The feclearexcept() function returns zero value if all the exceptions were cleared or if excepts is equal to zero.
It returns nonzero if any error occurs.

For the function to work, you should enable FENV_ACCESS, which will give your program to access the Floating point environment to test the exceptions raised.




// Cpp program to demonstrate
// feclearexcept()
#include <fenv.h> /* feclearexcept FE_ALL_EXCEPT, FE_INVALID */
#include <iostream> /* cout */
#include <math.h> /* sqrt */
#pragma STDC FENV_ACCESS on
using namespace std;
  
int main()
{
    feclearexcept(FE_ALL_EXCEPT);
    sqrt(-1);
    if (fetestexcept(FE_INVALID))
        cout << "sqrt(-1) raises FE_INVALID" << endl;
    return 0;
}
Output:
sqrt(-1) raises FE_INVALID
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :