C++ Standard Library C++ STL Library

C++ <cfenv> - FE_DFL_ENV



The FE_DFL_ENV macro expands to a pointer to fenv_t which points to a full copy of the default floating-point environment. It can be used to select the default environment for fesetenv and feupdateenv functions.

Definition in the <cfenv> header file is:

#define FE_DFL_ENV  /*implementation defined*/   

Example:

The example below shows the usage of FE_DFL_ENV macro.

#include <iostream>
#include <cfenv>

#pragma STDC FENV_ACCESS ON

using namespace std;

void Show_Environment() {
  if(fetestexcept(FE_DIVBYZERO)) 
    cout<<"Exceptions raised: FE_DIVBYZERO \n";
  if(fetestexcept(FE_INEXACT))   
    cout<<"Exceptions raised: FE_INEXACT \n";
  if(fetestexcept(FE_INVALID))   
    cout<<"Exceptions raised: FE_INVALID \n";
  if(fetestexcept(FE_OVERFLOW))  
    cout<<"Exceptions raised: FE_OVERFLOW \n";
  if(fetestexcept(FE_UNDERFLOW)) 
    cout<<"Exceptions raised: FE_UNDERFLOW \n";

  cout<<"Current rounding method: ";
  switch(fegetround()) {
    case FE_DOWNWARD: 
      cout<<"DOWNWARD"; break;
    case FE_TONEAREST:   
      cout<<"TO-NEAREST"; break;
    case FE_TOWARDZERO:   
      cout<<"TOWARD-ZERO"; break;
    case FE_UPWARD:  
      cout<<"UPWARD"; break;
    default:
      cout<<"unknown";
  }
  cout<<endl;
}

int main (){
  cout<<"On startup: \n";
  Show_Environment();

  //raising FE_INVALID and FE_INEXACT exceptions
  feraiseexcept(FE_INVALID | FE_INEXACT);
  //changing to upward rounding direction mode
  fesetround(FE_UPWARD);

  cout<<"\nBefore restoration:\n";
  Show_Environment();

  //resetting to default environment
  fesetenv(FE_DFL_ENV);

  cout<<"\nAfter reset to default:\n";
  Show_Environment();

  return 0;
}

The output of the above code will be:

On startup: 
Current rounding method: TO-NEAREST

Before restoration:
Exceptions raised: FE_INEXACT 
Exceptions raised: FE_INVALID 
Current rounding method: UPWARD

After reset to default:
Current rounding method: TO-NEAREST

❮ C++ <cfenv> Library

5