SIGFPE
What's the deal with SIGFPE?
I tried to install a signal handler to catch
things like divide-by-zero, or sqrt(-1), but no SIGFPE is raised - on cygwin,
sparc, ppc64. I tried to search this on google, but could not find
anything useful.
What is the way to handle this? In C. Portably.
[pratn@ps00 ~]$ cat fpe.c
/*
* Filename: fpe.c
* Author: prasun
* Created: Wed Feb 28 19:15:35 200 7
* $Id$
*/
#include <stdio.h>
#include <signal.h>
#include <math.h>
#include <string.h>
void fpe (int x)
{
fprintf (stderr, "Error occurred %d\n", x);
return ;
}
int main ()
{
float x, y;
struct sigaction sa;
memset (&sa, 0, sizeof (struct sigaction));
sa.sa_handler = fpe;
sigaction (SIGFPE, &sa, 0);
sa.sa_flags = SA_SIGINFO | SA_NODEFER;
x = -1;
y = 0;
printf ("%f\n", (x/y));
return 0;
}
/* vim: set tw=80 ts=2 sw=2 expandtab: * /
[pratn@ps00 ~]$ uname -p
ppc64
[pratn@ps00 ~]$ gcc -Wall -g fpe.c
[pratn@ps00 ~]$ ./a.out
-inf
[pratn@ps00 ~]$
I tried to install a signal handler to catch
things like divide-by-zero, or sqrt(-1), but no SIGFPE is raised - on cygwin,
sparc, ppc64. I tried to search this on google, but could not find
anything useful.
What is the way to handle this? In C. Portably.
[pratn@ps00 ~]$ cat fpe.c
/*
* Filename: fpe.c
* Author: prasun
* Created: Wed Feb 28 19:15:35 200
* $Id$
*/
#include <stdio.h>
#include <signal.h>
#include <math.h>
#include <string.h>
void fpe (int x)
{
fprintf (stderr, "Error occurred %d\n", x);
return ;
}
int main ()
{
float x, y;
struct sigaction sa;
memset (&sa, 0, sizeof (struct sigaction));
sa.sa_handler = fpe;
sigaction (SIGFPE, &sa, 0);
sa.sa_flags = SA_SIGINFO | SA_NODEFER;
x = -1;
y = 0;
printf ("%f\n", (x/y));
return 0;
}
/* vim: set tw=80 ts=2 sw=2 expandtab: *
[pratn@ps00 ~]$ uname -p
ppc64
[pratn@ps00 ~]$ gcc -Wall -g fpe.c
[pratn@ps00 ~]$ ./a.out
-inf
[pratn@ps00 ~]$
