fegetexceptflag() function in C/C++

The fegetexceptflag() function in C/C++ is specified in header file fenv.h and gets floating point exception flags. This function store the raised exception in the point specified by flagp .

Syntax:

int fegetexceptflag(fexcept_t* flagp, int excepts)

Parameters: The function accepts two mandatory parameters which are described below:

  • flagp : signifies the pointer to a fexcept_t object where the representation is stored.
  • excepts : signifies the bitmask value.

Macro –> Description:

  1. FE_DIVBYZERO –> Pole error: Division by zero.
  2. FE_INEXACT –> Inexact: The result is not exact.
  3. FE_INVALID –> Domain error: At least one of the arguments is a value for which the function is not defined.
  4. FE_OVERFLOW –> Overflow range error: The result is too large.
  5. FE_UNDERFLOW –> Underflow range error: The result is too small.
  6. FE_ALL_EXCEPT –> All exceptions.

Return value : The function returns two value as below:



  • Zero: on success.
  • Non-zero: on failure

Below programs illustrate the above function:
Program 1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// fegetexceptflag() function
  
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // bitmask value
    fexcept_t excepts;
  
    // divided by zero exception
    feraiseexcept(FE_DIVBYZERO);
  
    // current state is saved
    fegetexceptflag(&excepts, FE_ALL_EXCEPT);
    cout << "Exception raised -> \n";
  
    // print the exception occurred
    if (fetestexcept(FE_ALL_EXCEPT)) {
        if (fetestexcept(FE_DIVBYZERO))
            cout << "FE_DIVBYZERO \n";
        if (fetestexcept(FE_INEXACT))
            cout << "FE_INEXACT \n";
        if (fetestexcept(FE_INVALID))
            cout << "FE_INVALID \n";
        if (fetestexcept(FE_OVERFLOW))
            cout << "FE_OVERFLOW \n";
        if (fetestexcept(FE_UNDERFLOW))
            cout << "FE_UNDERFLOW \n";
        if (fetestexcept(FE_ALL_EXCEPT))
            cout << "FE_ALL_EXCEPT \n";
    }
    else
        cout << "None";
  
    return 0;
}

chevron_right


Output:

Exception raised -> 
FE_DIVBYZERO 
FE_ALL_EXCEPT

Program 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// fegetexceptflag() function
  
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // bitmask value
    fexcept_t excepts;
  
    // raised exception
    feraiseexcept(FE_ALL_EXCEPT);
  
    // current state is saved
    fegetexceptflag(&excepts, FE_ALL_EXCEPT);
    cout << "Exception raised -> \n";
  
    // print the exception occurred
    if (fetestexcept(FE_ALL_EXCEPT)) {
        if (fetestexcept(FE_DIVBYZERO))
            cout << "FE_DIVBYZERO \n";
        if (fetestexcept(FE_INEXACT))
            cout << "FE_INEXACT \n";
        if (fetestexcept(FE_INVALID))
            cout << "FE_INVALID \n";
        if (fetestexcept(FE_OVERFLOW))
            cout << "FE_OVERFLOW \n";
        if (fetestexcept(FE_UNDERFLOW))
            cout << "FE_UNDERFLOW \n";
        if (fetestexcept(FE_ALL_EXCEPT))
            cout << "FE_ALL_EXCEPT \n";
    }
    else
        cout << "None";
  
    return 0;
}

chevron_right


Output:

Exception raised -> 
FE_DIVBYZERO 
FE_INEXACT 
FE_INVALID 
FE_OVERFLOW 
FE_UNDERFLOW 
FE_ALL_EXCEPT


My Personal Notes arrow_drop_up

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 :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.