|
|||||||||||
|
Technical Support On-Line Manuals Libraries and Floating Point Support Guide |
Libraries and Floating Point Support GuideExample of a custom exception handler
NoteThe following functionality requires you to select a floating-point
model that supports exceptions, such as Example 19 shows a custom exception handler. Suppose you are converting some Fortran code into C. The Fortran numerical standard requires 0 divided by 0 to be 1, whereas IEEE 754 defines 0 divided by 0 to be an Invalid Operation and so by default it returns a quiet NaN. The Fortran code is likely to rely on this behavior, and rather than modifying the code, it is probably easier to make 0 divided by 0 return 1. When compiling, you must select a floating-point model that
supports exceptions, for example After the handler is installed, dividing 0.0 by 0.0 returns 1.0. Example 19. Custom exception handler
#include <fenv.h>
#include <signal.h>
#include <stdio.h>
__softfp __ieee_value_t myhandler(__ieee_value_t op1, __ieee_value_t op2,
__ieee_edata_t edata)
{
__ieee_value_t ret;
if ((edata & FE_EX_FN_MASK) == FE_EX_FN_DIV)
{
if ((edata & FE_EX_INTYPE_MASK) == FE_EX_INTYPE_FLOAT)
{
if (op1.f == 0.0 && op2.f == 0.0)
{
ret.f = 1.0;
return ret;
}
}
if ((edata & FE_EX_INTYPE_MASK) == FE_EX_INTYPE_DOUBLE)
{
if (op1.d == 0.0 && op2.d == 0.0)
{
ret.d = 1.0;
return ret;
}
}
}
/* For all other invalid operations, raise SIGFPE as usual */
raise(SIGFPE);
}
int main(void)
{
float i, j, k;
fenv_t env;
fegetenv(&env);
env.statusword |= FE_IEEE_MASK_INVALID;
env.invalid_handler = myhandler;
fesetenv(&env);
i = 0.0;
j = 0.0;
k = i/j;
printf("k is %f\n", k);
}
| ||||||||||
|
|||||||||||