C++ Standard Library C++ STL Library

C++ <cfenv> - feclearexcept() Function



The C++ <cfenv> feclearexcept() function attempts to clear the floating-point exceptions specified by excepts. Programs calling this function shall ensure that pragma FENV_ACCESS is enabled for the call.

Syntax

int feclearexcept (int excepts);

Parameters

excepts

Specify bitmask listing the floating-point exception flags to clear.

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_).

Return Value

Returns 0 if all exceptions in excepts were successfully cleared or if excepts is zero. Returns a non-zero value otherwise.

Exception

This function never throws exceptions.

Example:

In the example below, the feclearexcept() function is used to clear floating-point exceptions.

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

#pragma STDC FENV_ACCESS ON

using namespace std;

int main (){
  cout<<"sqrt(-1.0) = "<<sqrt(-1)<<"\n";
  if(fetestexcept(FE_INVALID))
    cout<<"Domain error is reported.\n";
  else
    cout<<"Domain error is not reported.\n";

  //clearing FE_INVALID and FE_INEXACT exceptions
  //bitwise OR is used to combine number of exceptions
  feclearexcept(FE_INVALID | FE_INEXACT);

  cout<<"sqrt(25) = "<<sqrt(25)<<"\n";
  if(fetestexcept(FE_INVALID))
    cout<<"Domain error is reported.\n";
  else
    cout<<"Domain error is not reported.\n";

  return 0;
}

The output of the above code will be:

sqrt(-1.0) = -nan
Domain error is reported.
sqrt(25) = 5
Domain error is not reported.

❮ C++ <cfenv> Library