Prevent math errors by carefully bounds-checking before calling functions. In particular, the following domain errors should be prevented by prior bounds-checking:
The calling function should take alternative action if these bounds are violated.
acos( x ), asin( x )
Non-Compliant Code Example
...
Code Block |
---|
float x, result;
if( islessequal(x,-1) || isgreaterequal(x, 1) ){
/* handle domain error */
}
result = acos(x);
|
atan2( y, x )
Non-Compliant Code Example
...
Code Block |
---|
float x, y, result;
if( fpclassify(x) == FP_ZERO && fpclassify(y) == FP_ZERO){
/* handle domain error */
}
result = atan2(y, x);
|
log( x ), log10( x )
Non-Compliant Code Example
...
Code Block |
---|
float result, x;
if(islessequal(x, 0)){
/* handle domain and range errors */
}
result = log(x);
|
pow( x, y )
Non-Compliant Code Example
...
Code Block |
---|
float x, y, result;
if(fpclassify(x) == FP_ZERO && islessequal(y, 0)){
/* handle domain error condition */
}
result = pow(x, y);
|
sqrt( x )
Non-Compliant Code Example
...