Go to the previous, next section.

Domain and Range Errors

Many of the functions listed in this chapter are defined mathematically over a domain that is only a subset of real numbers. For example, the acos function is defined over the domain between -1 and 1. If you pass an argument to one of these functions that is outside the domain over which it is defined, the function sets errno to EDOM to indicate a domain error. On machines that support IEEE floating point, functions reporting error EDOM also return a NaN.

Some of these functions are defined mathematically to result in a complex value over parts of their domains. The most familiar example of this is taking the square root of a negative number. The functions in this chapter take only real arguments and return only real values; therefore, if the value ought to be nonreal, this is treated as a domain error.

A related problem is that the mathematical result of a function may not be representable as a floating point number. If magnitude of the correct result is too large to be represented, the function sets errno to ERANGE to indicate a range error, and returns a particular very large value (named by the macro HUGE_VAL) or its negation (- HUGE_VAL).

If the magnitude of the result is too small, a value of zero is returned instead. In this case, errno might or might not be set to ERANGE.

The only completely reliable way to check for domain and range errors is to set errno to 0 before you call the mathematical function and test errno afterward. As a consequence of this use of errno, use of the mathematical functions is not reentrant if you check for errors.

None of the mathematical functions ever generates signals as a result of domain or range errors. In particular, this means that you won't see SIGFPE signals generated within these functions. (See section Signal Handling, for more information about signals.)

Macro: double HUGE_VAL

An expression representing a particular very large number. On machines that use IEEE floating point format, the value is "infinity". On other machines, it's typically the largest positive number that can be represented.

The value of this macro is used as the return value from various mathematical functions in overflow situations.

For more information about floating-point representations and limits, see section Floating Point Parameters. In particular, the macro DBL_MAX might be more appropriate than HUGE_VAL for many uses other than testing for an error in a mathematical function.

Go to the previous, next section.