Help catching floating point exceptions

Greg Lindahl gl8f at astsun.astro.Virginia.EDU
Tue May 28 07:37:51 AEST 1991


After perusing the info reader (man -k fp produces a nice list of man
pages which man can't find...), I am attempting to use fp_enable_all
to generate floating point traps. It isn't clear from the manual what
an FP trap is on an RS/6000. Nevertheless, I'm still praying that
there's a way to get my programs to die gracelessly when they hit a
problem.

The program below calls fp_enable_all() and then divides by zero. No
signal is generated, but an explicit test for divide by zero produces
a positive result.

a is 1.000000
b is 0.000000
floating point traps ARE enabled.
divide by zero trap IS enabled.
a/b is 1.000000
divide by zero error.

How do I get AIX to generate a signal? And why isn't this discussed in
the Fortran manual?

Advance thanks. Program below.

-- greg

------------------------------ cut here ------------------------------

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

double foo( double b );

void myfunc( int i )
{
  printf( "myfunc hit with argument %d\n", i );
}

main()
{
  int i;
  fp_enable_all();
  fp_enable( TRP_DIV_BY_ZERO );
  for( i = 1 ; i < 64 ; i++ )   /* catch all signals */
    (void) signal( i, myfunc );

  (void)foo( (double) 0.);
}

double foo( double b )
{
  double a = 1.;

/*  raise( SIGFPE );*/ /* works as expected if you uncomment */

  printf( "a is %f\n", a );
  printf( "b is %f\n", b );

  if( fp_any_enable() )
    printf( "floating point traps ARE enabled.\n" );
  if( fp_is_enabled( TRP_DIV_BY_ZERO ) )
    printf( "divide by zero trap IS enabled.\n" );

  printf( "a/b is %f\n", a/b ); /* should generate exception */
  if( fp_divbyzero() )
    printf( "divide by zero error.\n" ); /* this sees the flag set */

  return( a/b ); /* should generate another exception */
}



More information about the Comp.unix.aix mailing list