I am interested in raising floating point exceptions like Division by zero, Overflow, Underflow, etc.
I am using functions in fenv.h and signal handling. Whenever, any floating point exception occurs,
The problem seems like when inside the function catch_fpe ,the flags set by feraiseexcept somehow gets cleared and no condition is true.
Code:
#define _GNU_SOURCE
#include <fenv.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
void catch_fpe(int sig)
{
//#pragma STDC FENV_ACCESS ON
printf("i am in catch_fpe\n");
if (fetestexcept(FE_OVERFLOW))
printf("OVERFLOW\n");
else if (fetestexcept(FE_UNDERFLOW))
printf("UNDERFLOW\n");
else if (fetestexcept(FE_DIVBYZERO))
printf("DIVBYZERO\n");
else if (fetestexcept(FE_INVALID))
printf("INVALID OPERATION\n");
else if (fetestexcept(FE_INEXACT))
printf("INEXACT RESULT\n");
exit(0);
}
int main()
{
feclearexcept(FE_ALL_EXCEPT);
feenableexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW);
signal(SIGFPE, catch_fpe);
feraiseexcept(FE_DIVBYZERO);
}