...
Function | Bounds-checking |
---|---|
-1 <= x && x <= 1 | |
x != 0 || y != 0 | |
x > = 0 | |
x > 0 || (x == 0 && y > 0) || (x < 0 && y is an integer) | |
x >= 0 |
...
Code Block | ||
---|---|---|
| ||
double x,; double result; /* Set the value for x */ result = acos(x); |
...
Code Block | ||
---|---|---|
| ||
double x,; double result; /* Set the value for x */ if ( islessequalisnan(x) || isless(x,-1) || isgreaterequalisgreater(x, 1) ){ /* handle domain error */ } result = acos(x); |
...
Code Block | ||
---|---|---|
| ||
double x,; double y,; double result; /* Set the value for x and y */ result = atan2(y, 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); |
...
The following non-compliant code determines the natural logarithm of x
.
Code Block | ||
---|---|---|
| ||
double x; double result, x; /* Set the value for x */ result = log(x); |
...
The following compliant solution tests the suspect arguments to ensure that no domain errors or range errors are raised.
Code Block | ||
---|---|---|
| ||
double x; double result, x; /* Set the value for x */ if (isnan(x) || islessequal(x, 0)) { /* handle domain and range errors */ } result = log(x); |
...
Code Block | ||
---|---|---|
| ||
double x,; double y,; double result; result = pow(x, y); |
...
Code Block | ||
---|---|---|
| ||
double x,; double y,; double result; if (((x == 0.f) && islessequal(y, 0)) || (isless(x, 0) && !isInteger(y))) { /* handle domain and range errors */ } result = pow(x, y); |
...
Code Block | ||
---|---|---|
| ||
double x,; double result; result = sqrt(x); |
...
Code Block | ||
---|---|---|
| ||
double x,; double result; if (isless(x, 0)){ /* handle domain error */ } result = sqrt(x); |
...
The exact treatment of error conditions from math functions is quite complicated (see C99 Section 7.12.1, "Treatment of error conditions").
Wiki Markup |
---|
The {{sqrt()}} function returns zero when given a negative argument. The {{pow()}} function returns a very large number (appropriately positive or negative) on an overflow _range error_. This very large number is defined in {{<math.h>}} as described by C99 \[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\]: |
...