Versions Compared

Key

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

Section Subclause 7.12.1 of the C Standard [ISO/IEC 9899:2011] defines two types of errors that relate specifically to math functions in math.h:

domain error occurs if an input argument is outside the domain over which the mathematical function is defined.
range error occurs if the mathematical result of the function cannot be represented in an object of the specified type, due to extreme magnitude.

An example of a domain error is the square root of a negative number, such as sqrt(-1.0), which has no meaning in real arithmetic. On the other hand, 10 raised to the 1-millionth power, pow(10., 1e6), likely cannot be represented in an implementation's floating-point representation and consequently constitutes a range error. In both cases, the function will return some value, but the value returned is not the correct result of the computation.

...

The following table lists standard mathematical functions, along with any checks that should be performed on their domain, and indicates if whether they also throw range errors, as reported by the C Standard. If a function has a specific domain over which it is defined, the programmer should check its input values, and if a function throws range errors, the programmer should detect if whether a range error occurs. The standard math functions not listed in this table, such as atan(), have no domain restrictions and do not throw range errors.

...

The most reliable way to handle domain errors is to prevent them by checking arguments beforehand, as in the following template:

Code Block
if (/* argumentsArguments will cause a domain error */) {
  /* Handle domain error */
}
else {
  /* Perform computation */
}

...

The exact treatment of error conditions from math functions is quite complicated. Section Subclause 7.12.1 of the C Standard [ISO/IEC 9899:2011] defines the following behavior for floating-point overflow:

...

It is also difficult to check for math errors using errno because an implementation might not set it. For real functions, the programmer can tell whether the implementation sets errno by checking whether math_errhandling & MATH_ERRNO is nonzero. For complex functions, the C Standard, section subclause 7.3.2, simply states that "an implementation may set errno but is not required to" [ISO/IEC 9899:2011].

The System V Interface Definition, Third Edition (SVID3) , [UNIX 1992] provides more control over the treatment of errors in the math library. The user can provide a function named matherr that is invoked if errors occur in a math function. This function can print diagnostics, terminate the execution, or specify the desired return value. The matherr() function has not been adopted by C, so its use is not generally portable.

...

Because the pow() function can produce both domain errors and range errors, the programmer must first check that x and y lie within the proper domain, then detect if whether a range error occurs and act accordingly:

...

Tool

Version

Checker

Description

Fortify SCA

5.0

 

Can detect violations of this rule with CERT C Rule Pack

...

Bibliography

[ISO/IEC 9899:2011]Section Subclause 7.3, "Complex Arithmetic <complex.h>"
Section Subclause 7.12, "Mathematics <math.h>"
[Plum 1985]Rule 2-2
[Plum 1989]Topic 2.10, "conv—Conversions and Overflow"
[UNIX 1992]System V Interface Definition (SVID3)

 

...