/* Orion Lawlor's Short UNIX Examples, olawlor@acm.org 2003/8/18

Shows how to enable "signaling NaN" (Not-a-Number), which 
actually crash as soon as an absurd result is formed.
Normally, the floating point hardware silently inserts
ordinary "quiet NaNs" into your calculations when you 
perform an absurd operation.

WARNING: 
There doesn't seem to be any portable way to do this before C99--
for now, this is glibc specific, and only in glibc 2.2 or greater.  
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <signal.h>
#include <fenv.h> /* C99-specific */

void sigfpe_handler(int sig) {
	printf("Received floating-point exception signal (SIGFPE).  Good.\n");
	exit(0);
}

int main() {
	double x=1.0e300;
	double big=x*x; /* surely INF */
	double bad;
	signal(SIGFPE,sigfpe_handler);
	
	printf("Performing evil operation in default quiet mode:\n");
	bad=big-big; /* infinity minus infinity */
	
	feenableexcept(FE_INVALID);
	printf("Performing evil operation in signaling mode:\n");
	bad=big-big; /* infinity minus infinity, should signal */
	
	printf("Why didn't we crash?\n");
	return 1;
}

/*<@>
<@> ******** Program output: ********
<@> Performing evil operation in default quiet mode:
<@> Performing evil operation in signaling mode:
<@> Why didn't we crash?
<@> */
