[comp.lang.c] SIGFPE signal problem

pcb@gator.cacs.usl.edu (Peter C. Bahrs) (03/18/90)

I am trying to detect overflow, 0 divide and, although it may seem
unlikely, underflow.  Doesn't SIGFPE detect this?  If so, why doesn't
the following SMALL code work? 

Why and how in the heck can vendors not provide for underflow detection?
I am working on SUN 3,4 and Val 11780.

#include <stdio.h>
#include <signal.h>
#include <errno.h>

void h();
main()
{
int i=1;
float a=2.3;

signal(SIGFPE,h);
printf ("errno %d\n",errno);
i = i / 0;
a = a / 0.0;
a = 999999999999999999999999999999999999999999999999999999999999999999999999999999999.9;
printf ("%d %f\n",i,a);
}

void h()
{
printf ("testing...1 hour\n");
exit(1);
}

output:
1 inf


/*----------- Thanks in advance... --------------------------------------+
| Peter C. Bahrs                                                         |
| Center For Advanced Computer Studies      INET: pcb@gator.cacs.usl.edu |
| 2 Rex Street                                                           |
| University of Southwestern Louisiana      ...!uunet!dalsqnt!gator!pcb  | 
| Lafayette, LA 70504                                                    |
+-----------------------------------------------------------------------*/

jdc@naucse.UUCP (John Campbell) (03/20/90)

From article <5450@rouge.usl.edu>, by pcb@gator.cacs.usl.edu (Peter C. Bahrs):
: I am trying to detect overflow, 0 divide and, although it may seem
: unlikely, underflow.  Doesn't SIGFPE detect this?  If so, why doesn't
: the following SMALL code work? 
: 
: Why and how in the heck can vendors not provide for underflow detection?
: I am working on SUN 3,4 and Val 11780.
: 
I have at least one example on my ATT3b1 of computing IEEE Inf and not
causing a SIGFPE until the next operation on this value.  My "solution"
was to do some special checking near the computation as well as arming
SIGFPE.  

I'd be interested in hearing a discussion here from others who know more
than I about how this can happen.  Like Peter, I wonder "...can vendors
not provide for " dependable SIGFPE?

-- 
	John Campbell               ...!arizona!naucse!jdc
                                    CAMPBELL@NAUVAX.bitnet
	unix?  Sure send me a dozen, all different colors.

gwyn@smoke.BRL.MIL (Doug Gwyn) (03/20/90)

In article <5450@rouge.usl.edu> pcb@gator.cacs.usl.edu (Peter C. Bahrs) writes:
>I am trying to detect overflow, 0 divide and, although it may seem
>unlikely, underflow.  Doesn't SIGFPE detect this?

SIGFPE is a signal that may (or may not, depending on the system) be
generated upon floating-point exception.  Most UNIX-based C
implementations will generate SIGFPE upon division by zero, some of
them upon other forms of overflow, but few of them upon underflow.
Some implementations generate no SIGFPE at all, e.g. Gould UTX-32,
whose hardware couples integer and floating exceptions and which
relies on benign integer overflow (so it must disable all arithmetic
exceptions, alas).

Some vendors provide ways for your program to control behavior upon
exception, e.g. #pragma, compiler option, or mode-setting library
function.

>a = 999999999999999999999999999999999999999999999999999999999999999999999999999999999.9;

This would be much more likely to generate a compile-time diagnostic
than a run-time overflow exception.