C Standard Library

C <fenv.h> - fegetexceptflag() Function



The C <fenv.h> fegetexceptflag() function attempts to store the floating-point exception flags indicated by excepts into the fexcept_t object pointed by flagp. Programs calling this function shall ensure that pragma FENV_ACCESS is enabled for the call.

Syntax

int fegetexceptflag (fexcept_t* flagp, int excepts);

Parameters

flagp Specify pointer to an fexcept_t object where the flags will be stored.
excepts

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

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 <fenv.h> to identify additional floating-point exceptions (with their corresponding macros also beginning with FE_).

Return Value

Returns 0 if the flags are successfully stored. Returns a non-zero value otherwise.

Exception

This function never throws exceptions.

Example:

The example below shows the usage of fegetexceptflag() function.

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

#pragma STDC FENV_ACCESS ON

void FE_Exceptions_Message(void) {
  printf("Exceptions 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");
}

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 <fenv.h> Library