Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Function

Bounds-checking

acos( x ), asin( x )

-1 <= x && x <= 1

atan2( y, x )

x != 0 || y != 0

log( x ), log10( x )

x > = 0

pow( x, y )

x > 0 || (x == 0 && y > 0) || (x < 0 && y is an integer)

sqrt( x )

x >= 0

...

Code Block
bgColor#FFcccc
double x,;
double result;

/* Set the value for x */

result = acos(x);

...

Code Block
bgColor#ccccff
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
bgColor#FFcccc
double x,;
double y,; 
double result;

/* Set the value for x and y */

result = atan2(y, x);

...

Code Block
bgColor#ccccff
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
bgColorFFcccc

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
bgColor#ccccff

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
bgColor#FFcccc
double x,;
double y,;
double result;

result = pow(x, y);

...

Code Block
bgColor#ffcccc
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
bgColor#FFcccc
double x,;
double result;

result = sqrt(x);

...

Code Block
bgColor#ccccff
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]\]:

...