// This example shows how to use libfpe.a // from within a C++ program. Refer to the // handle_sigfpes(3C) man page for additional // information. // // CC fpe_handler4.c -o fpe_handler4 -lfpe // fpe_handler4 // // UNDERFLOW occurred // returned value for the UNDERFLOW is 10.000000 // // OVERFLOW occurred // returned value for the OVERFLOW is 20.000000 // // DIVIDE BY ZERO occurred // returned value for the DIVIDE BY ZERO is // 30.000000 // // INVALID occurred // #include #include #include #include void handler_rout(int exception[5], float ret_val[2]) { // This module determines what fpe occurred // and sets the result of the fpe // accordingly. // switch(exception[_EXCEPTION_TYPE]) { case _UNDERFL : printf("UNDERFLOW occurred\n"); ret_val[0] = 10.0; break; case _OVERFL : printf("OVERFLOW occurred\n"); ret_val[0] = 20.0; break; case _DIVZERO : printf("DIVIDE BY ZERO occurred\n"); ret_val[0] = 30.0; break; case _INVALID : // There is currently a bug in the // libfpe.a library that prevents a // replacement value from being // assigned to `ret_val' in the case // of INVALID FPE. For now just // recognize INVALID FPE. printf("INVALID occurred\n"); break; } } main() { int i; float res,min,max,zero,ten; // // The elements of the sigfpe_ array are // interpreted as follows: // sigfpe_[0] is ignored. // sigfpe_[1] is the structure for _UNDERFL // sigfpe_[2] is the structure for _OVERFL // sigfpe_[3] is the structure for _DIVZERO // sigfpe_[4] is the structure for _INVALID - // Each structure contains the following: // struct sigfpe_template // { // int repls; The replacement value // int count; The count limit // int trace; The trace limit // int abort; The abort limit // int exit ; The exit limit // }; // // Initialize the sigfpe_ array to cause // floating point exceptions to be handled by // the process. The replacement value for the // result will be determined in handler_rout. // for (i = 1; i < _N_EXCEPTION_TYPES+1 ; i++ ) { sigfpe_[i].repls = _USER_DETERMINED; } // Establish handler_rout() to be the module // to handle SIGFPEs handle_sigfpes (_ON, _EN_UNDERFL | \ _EN_OVERFL | _EN_DIVZERO | _EN_INVALID,\ (void (*)(unsigned int *, int \ *))handler_rout, 0, 0); // Lets generate some floating point // exceptions min = FLT_MIN; max = FLT_MAX; ten = 10.0; zero = 0.0; res = min/ten; printf(" returned value for the UNDERFLOW \ is %f\n\n",res); res = max*ten; printf(" returned value for the OVERFLOW \ is %f\n\n",res); res = ten/zero; printf(" returned value for the DIVIDE BY \ ZERO is %f\n\n",res); // Cause and INVALID fpe. res = zero/zero; }