feclearexcept in C++ with Examples

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}
chevron_right

Output:
sqrt(-1) raises FE_INVALID

Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
C++
Practice Tags :