C++ Standard Library C++ STL Library

C++ <cfenv> - FE_ALL_EXCEPT



The FE_ALL_EXCEPT macro expands to the bitwise OR of all possible floating-point exceptions defined in <cfenv>. If no floating-point exceptions are supported by the implementation, this macro is defined as 0 (zero).

It can be used with functions that expect a bitmask of possible floating point exceptions as one of its arguments: feclearexcept(), fegetexceptflag(), feraiseexcept(), fesetexceptflag(), or fetestexcept().

Definition in the <cfenv> header file is:

#define FE_ALL_EXCEPT  FE_DIVBYZERO | FE_INEXACT | FE_INVALID 
                       | FE_OVERFLOW | FE_UNDERFLOW   

The details about all floating-point exception macros are listed below:

MacrosDescription
FE_DIVBYZERO Pole error exception occurred in an earlier floating-point operation.
FE_INEXACT Inexact result exception occurred in an earlier floating-point operation (rounding was necessary to store the result).
FE_INVALID Invalid argument exception occurred (domain error occurred) in an earlier floating-point operation.
FE_OVERFLOW Overflow range error exception occurred in an earlier floating-point operation (result was too large to be representable).
FE_UNDERFLOW Underflow range error exception occurred in an earlier floating-point operation (result was subnormal with a loss of precision).
FE_ALL_EXCEPT Bitwise OR of all supported floating-point exceptions.

Certain library implementations may define additional macro constants in <cfenv> to identify additional floating-point exceptions (with their corresponding macros also beginning with FE_).

See math_errhandling for more details.

Example:

The example below shows the usage of FE_ALL_EXCEPT macro.

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

#pragma STDC FENV_ACCESS ON

//volatile not needed if FENV_ACCESS is supported
volatile double zero = 0.0; 
volatile double three = 3.0; 

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";
  
  feclearexcept(FE_ALL_EXCEPT);
  cout<<endl;
}

int main (){
  cout<<"1/0.0 = "<<(1/zero)<<"\n";
  FE_Exceptions_Message();

  cout<<"1.0/3.0 = "<<(1.0/three)<<"\n";
  FE_Exceptions_Message();

  cout<<"sqrt(-1.0) = "<<sqrt(-1)<<"\n";
  FE_Exceptions_Message();

  cout<<"DBL_MAX*2 = "<<(DBL_MAX*2)<<"\n";
  FE_Exceptions_Message();

  cout<<"nextafter(DBL_MIN/pow(2.0,52),0.0) = "
      <<(nextafter(DBL_MIN/pow(2.0,52),0.0))<<"\n";
  FE_Exceptions_Message();
  return 0;
}

The output of the above code will be:

1/0.0 = inf
Exceptions raised: FE_DIVBYZERO
1.0/3.0 = 0.333333
Exceptions raised: FE_INEXACT
sqrt(-1.0) = -nan
Exceptions raised: FE_INVALID
DBL_MAX*2 = inf
Exceptions raised: FE_INEXACT FE_OVERFLOW
nextafter(DBL_MIN/pow(2.0,52),0.0) = 0
Exceptions raised: FE_INEXACT FE_UNDERFLOW

❮ C++ <cfenv> Library