Facebook Page Twitter Page LinkedIn Page
× C Standard Library


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

If successful, the function changes the current state of the floating-point environment, setting the requested exception flags, but without actually raising the exceptions.

Syntax

int fesetexceptflag (const fexcept_t* flagp, int excepts);

Parameters

flagp Specify pointer to an fexcept_t object from where the flags will be read.
excepts Specify bitmask listing the floating-point exception flags to set.

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

Return Value

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

Exception

This function never throws exceptions.

Example:

The below example shows the usage of fesetexceptflag() 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