C++ Standard Library C++ STL Library

C++ <cfenv> - fexcept_t Type



The C++ <cfenv> fenv_t type representing all floating-point status flags collectively, including the active floating-point exceptions along with any additional information the implementation associates with the flags. Its value will be set by calling fegetexceptflag(), and can be applied by calling fesetexceptflag().

Example:

The example below shows the usage of fexcept_t type.

#include <iostream>
#include <cfenv>
#include <cmath>

#pragma STDC FENV_ACCESS ON

using namespace std;

void FE_Exceptions_Message(void) {
  cout<<"Exceptions raised:";
  if(fetestexcept(FE_DIVBYZERO)) 
    cout<<" FE_DIVBYZERO";
  if(fetestexcept(FE_INEXACT))   
    cout<<" FE_INEXACT";
  if(fetestexcept(FE_INVALID))   
    cout<<" FE_INVALID";
  if(fetestexcept(FE_OVERFLOW))  
    cout<<" FE_OVERFLOW";
  if(fetestexcept(FE_UNDERFLOW)) 
    cout<<" FE_UNDERFLOW";
  cout<<endl;
}

int main (){
  //creating fexcept_t object
  fexcept_t excepts; 

  //raising FE_DIVBYZERO exception
  feraiseexcept(FE_DIVBYZERO);
  FE_Exceptions_Message();

  //storing current exception flag
  fegetexceptflag(&excepts,FE_ALL_EXCEPT);

  //clear previous exception flag and 
  //create another exception flags
  feclearexcept(FE_ALL_EXCEPT);
  feraiseexcept(FE_INVALID | FE_INEXACT);
  FE_Exceptions_Message();

  //restoring previous exception flags
  fesetexceptflag(&excepts,FE_ALL_EXCEPT);
  FE_Exceptions_Message();

  return 0;
}

The output of the above code will be:

Exceptions raised: FE_DIVBYZERO
Exceptions raised: FE_INEXACT FE_INVALID
Exceptions raised: FE_DIVBYZERO

❮ C++ <cfenv> Library