fegetenv() function in C/C++

The fegetenv() function in C/C++ is specified in header file cfenv.h and attempts to store the current state of the floating-point environment in the object pointed by envp. The floating point environment is a set of status flags and control modes which includes both floating point exception and the rounding direction mode.

Syntax:

int fegetenv( fenv_t* envp )

Parameter : The function accepts one mandatory parameter envp which specifies an object where the state of the floating-point environment is stored.



Return value: The function returns two values as below:

  • On success, It returns zero.
  • On failure, It returns nonzero.

Below programs illustrate the above function:
Program 1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// fegetenv() function
  
#include <bits/stdc++.h>
using namespace std;
  
// rounding direction mode
void rounding_mode()
{
    cout << "Rounding mode is ->";
    switch (fegetround()) {
    case FE_TONEAREST:
  
        // Round to nearest
        cout << "FE_TONEAREST" << endl;
        break;
    case FE_DOWNWARD:
  
        // Round downward
        cout << "FE_DOWNWARD" << endl;
        break;
    case FE_UPWARD:
  
        // Round upward
        cout << "FE_UPWARD" << endl;
        break;
    case FE_TOWARDZERO:
  
        // Round toward zero
        cout << "FE_TOWARDZERO" << endl;
        break;
    default:
        cout << "unknown" << endl;
    };
}
  
int main(void)
{
    fenv_t envp;
  
    // initial environment
    cout << "Initial environment :" << endl;
  
    // print the exception raised initially
    cout << "Exception raised -> \n";
    if (fetestexcept(FE_ALL_EXCEPT)) {
        if (fetestexcept(FE_DIVBYZERO))
            cout << "FE_DIVBYZERO " << endl;
        if (fetestexcept(FE_INEXACT))
            cout << "FE_INEXACT " << endl;
        if (fetestexcept(FE_INVALID))
            cout << "FE_INVALID " << endl;
        if (fetestexcept(FE_OVERFLOW))
            cout << "FE_OVERFLOW " << endl;
        if (fetestexcept(FE_UNDERFLOW))
            cout << "FE_UNDERFLOW " << endl;
    }
    else
        cout << "None" << endl;
  
    // print the rounding direction mode
    rounding_mode();
  
    // Current environment
    fegetenv(&envp);
    feraiseexcept(FE_INVALID);
  
    // Set rounding direction mode
    fesetround(FE_DOWNWARD);
  
    // after environment is change
    cout << endl
         << "Final environment :" << endl;
  
    // print the exception raised
    cout << "Exception raised -> \n";
    if (fetestexcept(FE_ALL_EXCEPT)) {
        if (fetestexcept(FE_DIVBYZERO))
            cout << "FE_DIVBYZERO " << endl;
        if (fetestexcept(FE_INEXACT))
            cout << "FE_INEXACT " << endl;
        if (fetestexcept(FE_INVALID))
            cout << "FE_INVALID " << endl;
        if (fetestexcept(FE_OVERFLOW))
            cout << "FE_OVERFLOW " << endl;
        if (fetestexcept(FE_UNDERFLOW))
            cout << "FE_UNDERFLOW " << endl;
    }
    else
        cout << "None" << endl;
  
    // print the rounding direction mode
    rounding_mode();
  
    return 0;
}

chevron_right


Output:

Initial environment :
Exception raised -> 
None
Rounding mode is ->FE_TONEAREST

Final environment :
Exception raised -> 
FE_INVALID 
Rounding mode is ->FE_DOWNWARD

Program 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// fegetenv() function
  
#include <bits/stdc++.h>
using namespace std;
  
// rounding direction mode
void rounding_mode()
{
    cout << "Rounding mode is ->";
    switch (fegetround()) {
    case FE_TONEAREST:
  
        // Round to nearest
        cout << "FE_TONEAREST" << endl;
        break;
    case FE_DOWNWARD:
  
        // Round downward
        cout << "FE_DOWNWARD" << endl;
        break;
    case FE_UPWARD:
  
        // Round upward
        cout << "FE_UPWARD" << endl;
        break;
    case FE_TOWARDZERO:
  
        // Round toward zero
        cout << "FE_TOWARDZERO" << endl;
        break;
    default:
        cout << "unknown" << endl;
    };
}
  
int main(void)
{
    fenv_t envp;
  
    // initial environment
    cout << "Initial environment :" << endl;
  
    // print the exception raised initially
    cout << "Exception raised -> \n";
    if (fetestexcept(FE_ALL_EXCEPT)) {
        if (fetestexcept(FE_DIVBYZERO))
            cout << "FE_DIVBYZERO " << endl;
        if (fetestexcept(FE_INEXACT))
            cout << "FE_INEXACT " << endl;
        if (fetestexcept(FE_INVALID))
            cout << "FE_INVALID " << endl;
        if (fetestexcept(FE_OVERFLOW))
            cout << "FE_OVERFLOW " << endl;
        if (fetestexcept(FE_UNDERFLOW))
            cout << "FE_UNDERFLOW " << endl;
    }
    else
        cout << "None" << endl;
  
    // print the rounding direction mode
    rounding_mode();
  
    // Current environment
    fegetenv(&envp);
    feraiseexcept(FE_ALL_EXCEPT);
  
    // Set rounding direction mode
    fesetround(FE_DOWNWARD);
  
    // after environment is change
    cout << endl
         << "Final environment :" << endl;
  
    // print the exception raised
    cout << "Exception raised -> \n";
    if (fetestexcept(FE_ALL_EXCEPT)) {
        if (fetestexcept(FE_DIVBYZERO))
            cout << "FE_DIVBYZERO " << endl;
        if (fetestexcept(FE_INEXACT))
            cout << "FE_INEXACT " << endl;
        if (fetestexcept(FE_INVALID))
            cout << "FE_INVALID " << endl;
        if (fetestexcept(FE_OVERFLOW))
            cout << "FE_OVERFLOW " << endl;
        if (fetestexcept(FE_UNDERFLOW))
            cout << "FE_UNDERFLOW " << endl;
    }
    else
        cout << "None" << endl;
  
    // print the rounding direction mode
    rounding_mode();
  
    return 0;
}

chevron_right


Output:

Initial environment :
Exception raised -> 
None
Rounding mode is ->FE_TONEAREST

Final environment :
Exception raised -> 
FE_DIVBYZERO 
FE_INEXACT 
FE_INVALID 
FE_OVERFLOW 
FE_UNDERFLOW 
Rounding mode is ->FE_DOWNWARD


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.