Name

feholdexcept

Synopsis

Saves the current floating-point environment and switches to nonstop mode

#include <fenv.h>
intfeholdexcept( fenv_t *envp );

Like fegetenv(), the feholdexcept() function saves the current floating-point environment in the object pointed to by the pointer argument. However, feholdexcept() also clears the floating-point status flags and switches the floating-point environment to a nonstop mode, meaning that after any floating-point exception, normal execution continues uninterrupted by signals or traps. The function returns 0 if it succeeds in switching to nonstop floating-point processing; otherwise, the return value is nonzero.

Example

/*
 * Compute the hypotenuse of a right triangle, avoiding intermediate
 * overflow or underflow.
 *
 * (This example ignores the case of one argument having great magnitude
 * and the other small, causing both overflow and underflow!)
 */
double hypotenuse(double sidea, double sideb)
{
#pragma STDC FENV_ACCESS ON
  double  sum, scale, ascaled, bscaled, invscale;
  fenv_t  fpenv;
  int     fpeflags;

  if ( signbit( sidea ) ) sidea = fabs( sidea );
  if ( signbit( sideb ) ) sideb = fabs( sideb );feholdexcept(&fpenv); // Save previous environment, clear exceptions, // switch to nonstop processing. invscale = 1.0; sum = sidea * sidea + sideb * sideb; // First try whether a^2 + b^2 // causes any exceptions. fpeflags = fetestexcept(FE_UNDERFLOW | FE_OVERFLOW); // Did it? if ( fpeflags & FE_OVERFLOW && sidea > 1.0 && sideb > 1.0 ) { /* a^2 + b^2 caused an ...

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.