HP-UX Floating-Point Guide
162 Chapter 6
Floating-Point Trap Handling
Handling Traps
Sample Program: overflow_sig.c
/*************************************************************/
#include <stdio.h>
#include <math.h>
#include <fenv.h>
#include <signal.h>
int traps;
fexcept_t flags;
/* signal handler for floating-point exceptions */
void handle_sigfpe(int signo, siginfo_t *siginfo,
ucontext_t *ucontextptr)
{
void print_flags(int);
void print_traps(int);
printf("Raised signal %d -- floating point error\n", signo);
printf("code is %d\n", siginfo->si_code);
printf("fr0L is %08x\n", ucontextptr->uc_mcontext.ss_frstat);
print_traps(traps);
fegetexceptflag(&flags, FE_ALL_EXCEPT);
print_flags(flags);
exit(-1);
}
int main(void)
{
void print_flags(int);
void print_traps(int);
double x, y, z;
struct sigaction act;
act.sa_sigaction = (void (*)(int, siginfo_t *,
void *))&handle_sigfpe;
act.sa_flags = SA_SIGINFO;
/* establish the signal handler */
sigaction(SIGFPE, &act, NULL);
fesettrapenable(FE_OVERFLOW);
traps = fegettrapenable();
print_traps(traps);
x = 1.79e308;
y = 2.2e-308;
z = x / y; /* divide very big by very small */
printf("%g / %g = %g\n", x, y, z);
} /* continued */
/*************************************************************/