feupdateenv
You can use the feupdateenv function to restore the floating-point environment previously saved with feholdexcept.
void feupdateenv (const fenv_t *envp);
envp
- A pointer to the word containing the environment to be restored.
DESCRIPTION
The feupdateenv function, which takes a saved environment as argument, does the following:
- It temporarily saves the exception flags (raised by the current function).
- It restores the environment received as an argument.
- It signals the temporarily saved exceptions.
The feupdateenv function facilitates writing subroutines that appear to their callers to be atomic operations (such as addition, square root, and others). Atomic operations pass extra information back to their callers by signaling exceptions; however, they hide internal exceptions, which might be irrelevant or misleading. Thus, exceptions signaled between the feholdexcept and feupdateenv functions are hidden from the calling function unless the exceptions remain raised when the feupdateenv procedure is called.
EXAMPLES
/* NumFcn signals underflow if its result is denormalized,
overflow if its result is INFINITY, and inexact always, but hides
spurious exceptions occurring from internal computations. */
long double NumFcn(void)
{
fenv_t e; /* local environment storage */
enum NumKind c; /* for class inquiry */
fexcept_t * flagp;
long double result;
feholdexcept(&e); /* save caller's environment and
clear exceptions */
/* internal computation */
c = fpclassify(result); /* class inquiry */
feclearexcept(FE_ALL_EXCEPT); /* clear all exceptions */
feraiseexcept(FE_INEXACT); /* signal inexact */
if (c == FP_INFINITE)
feraiseexcept(FE_OVERFLOW);
else if (c == FP_SUBNORMAL)
feraiseexcept(FE_UNDERFLOW);
feupdateenv(&e);
/* restore caller's environment, and then signal
exceptions raised by NumFcn */
return(result);
}