math_errhandling - C math.h

C examples for math.h:math_errhandling

type

macro

From

<cmath>
<ctgmath>
<math.h>

Description

Expands to an expression that identifies the error handling mechanism employed by the functions in the <cmath> header:

constant value description
MATH_ERRNO 1 errno is used to signal errors. On domain error: errno is set to EDOM. On range error: errno is set to ERANGE.
MATH_ERREXCEPT 2 The proper C exception is raised. On domain error: FE_INVALID is raised. On pole error: FE_DIVBYZERO is raised. On overflow: FE_OVERFLOW is raised. On underflow: FE_UNDERFLOW may be raised.
MATH_ERRNO|MATH_ERREXCEPT 3 Both of the above

Both MATH_ERRNO and MATH_ERREXCEPT are macro constant expressions defined in math.h as 1 and 2 respectively.

Example

Demo Code


#include <stdio.h>
#include <math.h>
#include <errno.h>
#include <fenv.h>
#pragma STDC FENV_ACCESS on/*from   w w w.j  av  a  2  s .c o  m*/

int main () {
  errno = 0;
  if (math_errhandling & MATH_ERREXCEPT) 
     feclearexcept(FE_ALL_EXCEPT);

  printf ("Error handling: %d",math_errhandling);

  sqrt (1);

  if (math_errhandling & MATH_ERRNO && errno==EDOM) {
       printf("errno set to EDOM\n");
  }else{
       printf("no errno set to EDOM\n");
  }
  if (math_errhandling  &MATH_ERREXCEPT && fetestexcept(FE_INVALID)) {
       printf("FE_INVALID raised\n");
  }else{
       printf("no FE_INVALID raised\n");
  }


  sqrt (-1);

  if (math_errhandling & MATH_ERRNO && errno==EDOM) {
       printf("errno set to EDOM\n");
  }
  if (math_errhandling  &MATH_ERREXCEPT && fetestexcept(FE_INVALID)) {
       printf("FE_INVALID raised\n");
  }
  return 0;
}

Related Tutorials