Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#ccccff
double x;
double result;

/* Set the value for x */

if ( isnan(x) || isless(x,-1) || isgreater(x, 1) ){
     /* handle domain error */
}

result = acos(x);

...

Code Block
bgColor#ccccff
double x;
double y;
double result;

/* Set the value for x and y */

if ( (x == 0.f) && (y == 0.f) ) {
     /* handle domain error */
}

result = atan2(y, x);

...

Code Block
bgColor#ccccff
double x;
double result;

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

result = sqrt(x);

Anchor
Gamma functions
Gamma functions

lgamma(x), tgamma(x)

The "too large" and "too small" conditions for a range error in the gamma functions are vague enough to make it very difficult to catch all possible range errors simply by looking at the input.

Noncompliant Code Example

The following This noncompliant code example attempts to prevent domain and range errors, but a range error occurs nonetheless.

Code Block
bgColor#ffcccc
float x = -90.5;

if ((x == 0) || (x < 0 && x == nearbyint(x))) {
  /* Handlehandle Errorerror */
}

float f = tgamma(x);

...

Code Block
bgColor#ccccff
float x = -90.5;

if ((x == 0) || (x < 0 && x == nearbyint(x))) {
  /* Handlehandle Errorerror */
}

feclearexcept(FE_ALL_EXCEPT);

float f = tgamma(x);

if (fetestexcept(FE_UNDERFLOW) != 0) {
    printf("Underflow detected\n");
}

...