Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Added fp-error info, including link to FLP03

...

Code Block
bgColor#ccccff
double x;
double result;

if (isless(x, 0)){
  /* handle domain error */
}

result = sqrt(x);

...

Error Checking

...

and Detection

Wiki Markup
The exact treatment of error conditions from math functions is quite complicated.  C99 Section 7.12.1 defines the following behavior for floating point overflow \[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\]

...

For functions where argument validation is difficult, including pow(), erfc(), lgamma(), and tgamma(), one can employ the following approach. This approach uses C99 standard functions for floating point errors.

Code Block
bgColor#ccccff
#include <math.h>
#if defined(math_errhandling) \
  && (math_errhandling & MATH_ERREXCEPT)
#include <fenv.h>
#endif

/* ... */

#if defined(math_errhandling) \
  && (math_errhandling & MATH_ERREXCEPT)
  feclearexcept(FE_ALL_EXCEPT);
#endif
errno = 0;

/* call the function */

#if !defined(math_errhandling) \
  || (math_errhandling & MATH_ERRNO)
if (errno != 0) {
  /* handle error */
}
#endif
#if defined(math_errhandling) \
  && (math_errhandling & MATH_ERREXCEPT)
if (fetestexcept(FE_INVALID 
               | FE_DIVBYZERO 
               | FE_OVERFLOW) != 0) 
{
  /* handle error */
}
#endif

See FLP03-A. Detect and handle floating point errors for more details on how to detect floating point errors.

Implementation Details

System V Interface Definition, Third Edition

...