In a numerical application, I'd like to know if a floating point exception occured after the computation finishes. By default, floating point divisions and invalid operations are ignored silently.
My attempt is to enable FPEs I care about, handle SIGFPE by setting a flag and disabling them again to allow execution to continue:
#include <fenv.h>
#include <signal.h>
#include <stdio.h>
int caught = 0;
struct sigaction old_sa;
/* (2) */ fenv_t fenv_hold;
void sighandler()
{
caught = 1;
printf("Caught in handler, disabling\n");
/* (1) */ fedisableexcept(FE_ALL_EXCEPT);
/* (2) */ feholdexcept(&fenv_hold);
sigaction(SIGFPE, &old_sa, NULL);
}
int main(void)
{
struct sigaction sa;
volatile double a=1, b=0;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = sighandler;
sigaction(SIGFPE, &sa, &old_sa);
feenableexcept(FE_DIVBYZERO);
printf("Dividing by zero..\n");
a/=b;
printf("Continuing\n");
}
I took two approaches, the first one labeled with (1)
, the second one with (2)
. Neither of them works as expected.
Output:
Dividing by zero..
Caught in handler, disabling
Floating point exception (core dumped)
Expected Output:
Dividing by zero..
Caught in handler, disabling
Continuing