Prevent or detect domain errors and range errors in math functions before using the results in further computations. Most of the math functions can fail , if they are given arguments which that do not produce a proper result. For example, the square root of a negative number, such as sqrt(-1.0)
, has no meaning in real arithmetic. This is known as a domain error; the argument is outside the domain over which the function is defined. For another example, ten raised to the one-millionth power, pow(10., 1e6)
, cannot be represented in any (current) floating point representation. This is known as a range error; the result cannot be represented as a proper double
number. In all such error cases, the function will return some value, but the value returned is not the correct result of the computation.
...
This code uses bounds checking to ensure that there is not a domain error.
...
This code tests the suspect arguments to ensure that no domain or range errors are raised.
...
This code tests the suspect argument to ensure that no domain error is raised.
Code Block | ||
---|---|---|
| ||
float x, result; if (isless(x, 0)){ /* handle domain error */ } result = sqrt(x); |
Non-Compliant Coding Example (
...
Error Checking)
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]\]: |
The macro
Code Block HUGE_VALexpands to a positive double constant expression, not necessarily representable as a
float. The macros
Code Block HUGE_VALF HUGE_VALLare respectively float and long double analogs of
HUGE_VAL
.
It is best not to check for errors by comparing the returned value against HUGE_VAL
or zero ) for several reasons. First of all, these are in general valid (albeit unlikely) data values. Secondly, making such tests requires detailed knowledge of the various error returns for each math function. Also, there are three different possibilities; , -HUGE_VAL, 0, and HUGE_VAL; , and you must know which are possible in each case. Finally, different versions of the library have differed in their error-return behavior.
It is also difficult to check for math errors using errno
since because an implementation might not set it. For real functions, the programmer can test whether the implementation sets errno
because in that case math_errhandling & MATH_ERRNO
is nonzero. For complex functions, the C Standard section Section 7.3.2 simply states that setting errno
is optional.
Compliant Solution (
...
Error Checking)
The most reliable way to test for errors is by checking its arguments beforehand, as in the following compliant solution:
...
The System V Interface Definition, Third Edition (SVID3) provide provides more control over the treatment of errors in the math library. The user can provide a function named matherr
which that is invoked if errors occur in a math function. This function could print diagnostics, terminate the execution, or specify the desired return-value. The matherr()
function has not been adopted by the C standard, so its use is not generally portable.
...