...
Anchor | ||||
---|---|---|---|---|
|
pow(x, y)
Noncompliant Code Example
The following noncompliant code raises x
to the power of y
.
...
bgColor | #FFcccc |
---|
...
Mathematically speaking, the domain of pow( x,
...
y)
...
However, this code may produce a domain error if x
is negative and y
is not an integer, or if x
is zero and y
is zero. A domain error or range error may occur if x
is zero and y
is negative, and a range error may occur if the result cannot be represented as a double
dictates that if x
is 0, then y
must be strictly positive, and if x
is neative, then y
must be an integer. However, since pow()
can yield numbers of very large magnitude or very small magnitude, the set of inputs that do not cause a range error is not only more limited, but poorly defined.
Noncompliant Code Example
This code only performs bounds checking on x
and y
. It prevents domain errors and some range errors, but does not prevent range errors where the result cannot be represented as a double
(see the Error Checking and Detection section below regarding ways to mitigate the effects of a range error).
The following noncompliant code raises x
to the power of y
.
Code Block | ||
---|---|---|
| ||
Code Block | ||
| ||
double x; double y; double result; if ((result = pow(x, y); |
However, this code may produce a domain error if x
is negative and y
is not an integer, or if x
is zero and y
is zero. A domain error or range error may occur if x
is zero and y
is negative, and a range error may occur if the result cannot be represented as a double
.
Noncompliant Code Example
This noncompliant code example only performs bounds checking on x
and y
. It prevents domain errors and some range errors, but does not prevent range errors where the result cannot be represented as a double
(see the Error Checking and Detection section below regarding ways to mitigate the effects of a range error).
Code Block | ||
---|---|---|
| ||
double x; double y; double result; if (((x = == 0.f) && islessequal(y, 0)) || (isless(x, 0))) { /* handle domain and range errors */ } result = pow(x, y); |
...
Noncompliant Code Example
This The following noncompliant code example attempts to prevent domain errors, but does not prevent range errors. The result is often an underflow errorcomputes the gamma value of x
.
Code Block | ||
---|---|---|
| ||
double x; double d float x = -90.5; if ((x == 0) || (x < 0 && x == nearbyint(x))) { /* handle error */ } float f = tgamma(x); |
Compliant Solution
|
However, this code may produce a domain error if x
is 0 or a negative integer. Also, a range error may occur if the result cannot be represented as a double
.
Noncompliant Code Example
This noncompliant code example attempts to prevent domain errors, but does not prevent range errors. The result may be an underflow or overflow error. See the This compliant solution detects the underflow by using the methods described below in the Error Checking and Detection section below regarding ways to mitigate the effects of a range error.
Code Block | ||
---|---|---|
| ||
floatdouble x = -90.5; if ((x == 0) || (x < 0 && x == nearbyint(x))) { /* handle error */ } feclearexcept(FE_ALL_EXCEPT); float fdouble d = tgamma(x); if (fetestexcept(FE_UNDERFLOW) != 0) { printf("Underflow detected\n"); } |
Anchor | ||||
---|---|---|---|---|
|
...