<cfenv> (fenv.h)

function
<cfenv>

fetestexcept

int fetestexcept (int excepts);
Test for floating-point exceptions
Returns the exceptions currently set, among those specified by excepts.

The value returned is the bitwise OR representation of the subset of excepts that are currently set in the floating point environment. Or zero, if none of the exceptions in excepts are currently set.

Programs calling this function shall ensure that pragma FENV_ACCESS is enabled for the call.

Parameters

excepts
Bitmask value: A combination (with bitwise OR) of any number of floating-point exception values supported by the implementation:
macro valuedescription
FE_DIVBYZEROPole error: division by zero, or some other asymptotically infinite result (from finite arguments).
FE_INEXACTInexact: the result is not exact.
FE_INVALIDDomain error: At least one of the arguments is a value for which the function is not defined.
FE_OVERFLOWOverflow range error: The result is too large in magnitude to be represented as a value of the return type.
FE_UNDERFLOWUnderflow range error: The result is too small in magnitude to be represented as a value of the return type.
FE_ALL_EXCEPTAll exceptions (selects all of the exceptions supported by the implementation).
Certain library implementations may support additional floating-point exception values (with their corresponding macros also beginning with FE_).
Libraries may define in <fenv.h> only the macro values above they support (the others may not be defined).
At least all of the above macro values are defined in <cfenv> (even if not supported by the implementation).

Return Value

Zero, if none of the exceptions in excepts are set.
Otherwise, the exceptions (among those of excepts) currently set.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/* fetestexcept example */
#include <stdio.h>      /* puts */
#include <fenv.h>       /* feraiseexcept, fetestexcept, FE_* */
#pragma STDC FENV_ACCESS on

double fn (double x) {
  /* some function for which zero is a domain and range error */
  if (x==0.0) feraiseexcept(FE_INVALID|FE_OVERFLOW);
  return x;
}

int main ()
{
  int fe;

  feclearexcept (FE_ALL_EXCEPT);
  fn (0.0);

  /* testing for single exception: */
  if (fetestexcept(FE_OVERFLOW)) puts ("FE_OVERFLOW is set");

  /* testing multiple exceptions: */
  fe = fetestexcept (FE_ALL_EXCEPT);

  puts ("The following exceptions are set:");
  if (fe & FE_DIVBYZERO) puts ("FE_DIVBYZERO");
  if (fe & FE_INEXACT)   puts ("FE_INEXACT");
  if (fe & FE_INVALID)   puts ("FE_INVALID");
  if (fe & FE_OVERFLOW)  puts ("FE_OVERFLOW");
  if (fe & FE_UNDERFLOW) puts ("FE_UNDERFLOW");

  return 0;
}


Possible output:

FE_OVERFLOW is set
The following exceptions are set:
FE_INVALID
FE_OVERFLOW

Data races

Each thread maintains a separate floating-point environment with its own state. Spawning a new thread copies the current state. [This applies to C11 and C++11 implementations]

Exceptions

No-throw guarantee: this function never throws exceptions.
Note that C floating-point exceptions are not C++ exceptions, and thus are not caught by try/catch blocks.
Calling this function with pragma FENV_ACCESS off causes undefined behavior.

See also