...
Code Block |
---|
|
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 |
---|
|
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 |
---|
|
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 |
---|
|
float x = -90.5;
if ((x == 0) || (x < 0 && x == nearbyint(x))) {
/* Handlehandle Errorerror */
}
float f = tgamma(x);
|
...
Code Block |
---|
|
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");
}
|
...