[comp.lang.c] problem with matherr

mike@unix.cis.pitt.edu (Mike Elliot) (04/30/91)

/*
 * I am having some difficulties trapping math errors. I have been
 * using matherr as it seems to be the only portable way of trapping
 * them (sun's, hp's, apollo's, ibm's). I can trap domain errors, but
 * I am unable to trap overflow and underflow. I have included my short
 * test program and its output. Any ideas?
 *
 * Mike Elliot
 * mike@unix.cis.pitt.edu
 */

#include <math.h>
#include <stdio.h>

void GenerateNaN()
{
	float nan;

	nan = (sqrt(-1.0));
	(void) fprintf(stderr, "%f\n", nan);
}

void GenerateOverflow()
{
	float overflow;

#ifdef sun
	for (overflow = 10.0; ;)
	{
		overflow *= overflow;
		(void) printf("%f\n", overflow);
		if (overflow == infinity())
			break;
	}
#else
	int count;

	for (count = 0, overflow = 10.0; count < 25; count ++)
	{
		overflow *= overflow;
		(void) printf("%f\n", overflow);
	}
#endif
}

void GenerateUnderflow()
{
	float underflow;

	for (underflow = 1.0; ; )
	{
		underflow /= 10.0;
		(void) printf("%f\n", underflow);
		if (underflow == 0.0)
			break;
	}
}

int matherr(x)
	register struct exception *x;
{
	switch (x->type) {
		case DOMAIN:
			(void) fprintf(stderr, "%s(), DOMAIN error\n", x->name);
			break;
		case OVERFLOW:
			(void) fprintf(stderr, "%s(), OVERFLOW error\n", x->name);
			break;
		case SING:
			(void) fprintf(stderr, "%s(), SING error\n", x->name);
			break;
		case UNDERFLOW:
			(void) fprintf(stderr, "%s(), UNDERFLOW error\n", x->name);
			break;
		default:
			(void) fprintf(stderr, "%s(), Uknown math error\n", x->name);
			break;
	}
	return(0);
}

main()
{
	GenerateNaN();
	GenerateOverflow();
	GenerateUnderflow();
}

/* Output form matherr run on a sun4/260 */

#if 0
sqrt(), DOMAIN error
sqrt: DOMAIN error
NaN
100.000000
10000.000000
100000000.000000
10000000272564224.000000
100000003318135351409612647563264.000000
Inf
0.100000
0.010000
0.001000
0.000100
0.000010
0.000001
0.000000
[38 duplicate lines deleted]
0.000000
#endif