feholdexcept() in C/C++

The feholdexcept() function in C/C++ first saves the current floating-point environment in the object of fenv_t and it then resets the current values of all the floating point status flags. The feholdexcept() function is defined in cfenv header file in C++ and in fenv.h header file in C.
Syntax:

int feholdexcept( fenv_t* envp )

Parameters: The function accepts a single mandatory parameter envp which is a pointer to an object
of type fenv_t that stores the current status of the floating point environment.

Return Value: The function returns a int value on two conditions:

  • If function completed succesfully it returns 0.
  • On failure, it returns a nonzero integer.

Below programs illustrate the above function.

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// feholdexcept() function
#include <bits/stdc++.h>
#pragma STDC FENV_ACCESS on
  
// function to divide
double divide(double x, double y)
{
    // environment variable
    fenv_t envp;
  
    // do the devision
    double ans = x / y;
  
    // use the function feholdexcept
    feholdexcept(&envp);
  
    // clears exception
    feclearexcept(FE_OVERFLOW | FE_DIVBYZERO);
  
    return ans;
}
  
int main()
{
    // It is a combination of all of
    // the possible floating-point exception
    feclearexcept(FE_ALL_EXCEPT);
    double x = 10;
    double y = 0;
  
    // it returns the division of x and y
    printf("x/y = %f\n", divide(x, y));
  
    // the function does not throw
    // any exception on division by 0
    if (!fetestexcept(FE_ALL_EXCEPT)) {
        printf("No exceptions raised");
    }
    return 0;
}

chevron_right


Output:

x/y = inf
No exceptions raised

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// feholdexcept() function
#include <bits/stdc++.h>
#pragma STDC FENV_ACCESS on
using namespace std;
  
// function to print raised exceptions
void raised_exceptions()
{
    cout << "Exceptions raised are : ";
  
    if (fetestexcept(FE_DIVBYZERO))
        cout << " FE_DIVBYZERO\n";
    else if (fetestexcept(FE_INVALID))
        cout << " FE_INVALID\n";
    else if (fetestexcept(FE_OVERFLOW))
        cout << " FE_OVERFLOW\n";
    else if (fetestexcept(FE_UNDERFLOW))
        cout << " FE_UNDERFLOW\n";
    else
        cout << " No exception found\n";
  
    return;
}
  
// Driver code
int main()
{
    // environment variable
    fenv_t envp;
  
    // raise certain exceptions
    feraiseexcept(FE_DIVBYZERO);
    // print the raised exception
    raised_exceptions();
  
    // saves and clears current
    // exceptions by feholexcept function
    feholdexcept(&envp);
    // no exception found
    raised_exceptions();
  
    // restores the previously
    // saved exceptions
    feupdateenv(&envp);
    raised_exceptions();
  
    return 0;
}

chevron_right


Output:

Exceptions raised are :  FE_DIVBYZERO
Exceptions raised are :  No exception found
Exceptions raised are :  FE_DIVBYZERO


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.



Improved By : nidhi_biet



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.