...
Code Block |
---|
float x, result; if( x <= -1 || x >= 1){ /* handle domain error */ } result = acos(x); |
atan2(y, x)
Non-Compliant Solution
The following code may produce a domain error if both x and y are zero.
...
Code Block |
---|
float x, y, result; if( x == 0 && y == 0){ /* handle domain error */ } result = atan2(y, x); |
log(x), log10(x)
Non-Compliant Example
The following code may produce a domain error if x is negative and a range error if x is zero.
Code Block |
---|
float result, x;
result = log(x);
|
Compliant Example
The following code tests the suspect arguments to ensure no domain or range errors are raised.
Code Block |
---|
float result, x;
if(x <=0){
/* handle domain and range errors */
}
result = log(x);
|
pow(x,y)
Non-Compliant Example
...
Code Block |
---|
float x, y, result;
if(x == 0 && y <=0){
/* handle domain error condition */
}
result = pow(x, y);
|
Sqrt(x)
Non-Compliant Solution
The following code may produce a domain error if x is negative.
Code Block |
---|
float x, result;
result = sqrt(x);
|
Compliant Solution
The following code tests the suspect argument to ensure no domain error is raised.
Code Block |
---|
float x, result;
if(x < 0){
/* handle domain error */
}
result = sqrt(x);
|
References
- ISO/IEC 9899-1999 7.12 Mathematics <math.h>
- Plum 91 Topic: 2.10 conv - conversions and overflow