C library - feraiseexcept() function



The C fenv library feraiseexcept() function is used to raised floating-point exceptions specified in the 'excepts' argument. This argument is a bitwise OR combination of the floating-point exception macros, such as FE_DIVBYZERO, FE_INEXACT, FE_INVALID, FE_OVERFLOW, and FE_UNDERFLOW.

If the excepts include FE_OVERFLOW or FE_UNDERFLOW when raising exceptions, the function may also raise FE_INEXACT. Because the order in which the exceptions are raised is not specified, except that FE_OVERFLOW and FE_UNDERFLOW are always raised before FE_INEXACT.

Syntax

Following is the C library syntax of feraiseexcept() function −

int feraiseexcept( int excepts );

Parameters

This function accepts a single parameter −

  • excepts − It represent a bitmask listing of the exception flags to raise.

Return Value

This function returns 0 if all listed exceptions were raised, non-zero value otherwise.

Example 1

Following is the basic c program to demonstrate the use of feraiseexcept() function.

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

int main() {
   // Enable exceptions for division by zero and overflow
   if (feraiseexcept(FE_DIVBYZERO | FE_OVERFLOW) != 0) {
      printf("Failed to raise the specified floating-point exceptions.\n");
      return 1;
   } else {
      printf("Successfully raised the specified floating-point exceptions.\n");
   }
   return 0;
}

Output

Following is the output −

Successfully raised the specified floating-point exceptions.

Example 2

The following C program raises the 'FE_INEXACT' floating-point exception using feraiseexcept() function.

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

int main() {
   // Raise the FE_INEXACT floating-point exception
   if (feraiseexcept(FE_INEXACT) != 0) {
      printf("Failed to raise the FE_INEXACT floating-point exception.\n");
      return 1;
   } else {
      printf("Successfully raised the FE_INEXACT floating-point exception.\n");
   }
   return 0;
}

Output

Following is the output −

Successfully raised the FE_INEXACT floating-point exception.

Example 3

The example below raise the FE_OVERFLOW and FE_UNDERFLOW floating-point exceptions. Additionally, it checks and reports which exceptions have been raised, demonstrating that FE_INEXACT may also be raised when FE_OVERFLOW or FE_UNDERFLOW are included.

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

int main() {
   // Raise the FE_OVERFLOW and FE_UNDERFLOW floating-point exceptions
   if (feraiseexcept(FE_OVERFLOW | FE_UNDERFLOW) != 0) {
      printf("Failed to raise the specified floating-point exceptions.\n");
      return 1;
   } else {
      printf("Successfully raised the specified floating-point exceptions.\n");
   }

   // Check which exceptions are currently raised
   int raised_excepts = fetestexcept(FE_ALL_EXCEPT);
   if (raised_excepts & FE_OVERFLOW) {
      printf("FE_OVERFLOW is raised.\n");
   }
   if (raised_excepts & FE_UNDERFLOW) {
      printf("FE_UNDERFLOW is raised.\n");
   }
   if (raised_excepts & FE_INEXACT) {
      printf("FE_INEXACT is also raised.\n");
   }
   return 0;
}

Output

Following is the output −

Successfully raised the specified floating-point exceptions.
FE_OVERFLOW is raised.
FE_UNDERFLOW is raised.
c_library_fenv_h.htm
Advertisements