6.5.15 fetestexcept Function

Determines the state of a specified subset of the currently set floating-point exceptions.

Attention: This function is implemented only by MPLAB XC32 C compilers when using a device with a FPU.

Include

<fenv.h>

Prototype

int fetestexcept(int excepts);

Argument

excepts
a value being the bitwise OR of one or more floating-point exception macros, representing the exceptions to test

Retrun Value

The function returns the bitwise OR of the floating-point exception macro values associated with those exceptions that are specified by the argument and that are currently set.

Example

See the notes at the beginning of this chapter or section for information on using printf() or scanf() (and other functions reading and writing the stdin or stdout streams) in the example code.

#include <fenv.h>
#include <stdio.h>
#include <math.h>

void showExceptions(void)
{
  printf("Exceptions currently raised: ");
  if(fetestexcept(FE_DIVBYZERO))
    printf("FE_DIVBYZERO ");
  if(fetestexcept(FE_INEXACT))
    printf("FE_INEXACT ");
  if(fetestexcept(FE_INVALID))
    printf("FE_INVALID ");
  if(fetestexcept(FE_OVERFLOW))
    printf("FE_OVERFLOW ");
  if(fetestexcept(FE_UNDERFLOW))
    printf("FE_UNDERFLOW ");
  printf("\n");
}

volatile double result = 0.0;

int main(void)
{
  fexcept_t excepts;

  result = 1.0 / result;                        // raise FE_DIVBYZERO
  showExceptions();
  fegetexceptflag(&excepts, FE_ALL_EXCEPT);     // save state
  feraiseexcept(FE_INVALID|FE_OVERFLOW);        // raise exceptions without operation
  showExceptions();
  fesetexceptflag(&excepts, FE_ALL_EXCEPT);     // restore state
  showExceptions();
}

Example Output

Exceptions currently raised: FE_DIVBYZERO 
Exceptions currently raised: FE_DIVBYZERO FE_INEXACT FE_INVALID FE_OVERFLOW 
Exceptions currently raised: FE_DIVBYZERO