```MATH_ERROR(7)             Linux Programmer's Manual            MATH_ERROR(7)
```

## NAME         top

```       math_error - detecting errors from mathematical functions
```

## SYNOPSIS         top

```       #include <math.h>
#include <errno.h>
#include <fenv.h>
```

## DESCRIPTION         top

```       When an error occurs, most library functions indicate this fact by
returning a special value (e.g., -1 or NULL).  Because they typically
return a floating-point number, the mathematical functions declared
in <math.h> indicate an error using other mechanisms.  There are two
error-reporting mechanisms: the older one sets errno; the newer one
uses the floating-point exception mechanism (the use of
feclearexcept(3) and fetestexcept(3), as outlined below) described in
fenv(3).

A portable program that needs to check for an error from a
mathematical function should set errno to zero, and make the
following call

feclearexcept(FE_ALL_EXCEPT);

before calling a mathematical function.

Upon return from the mathematical function, if errno is nonzero, or
the following call (see fenv(3)) returns nonzero

fetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW |
FE_UNDERFLOW);

then an error occurred in the mathematical function.

The error conditions that can occur for mathematical functions are
described below.

Domain error
A domain error occurs when a mathematical function is supplied with
an argument whose value falls outside the domain for which the func‐
tion is defined (e.g., giving a negative argument to log(3)).  When a
domain error occurs, math functions commonly return a NaN (though
some functions return a different value in this case); errno is set
to EDOM, and an "invalid" (FE_INVALID) floating-point exception is
raised.

Pole error
A pole error occurs when the mathematical result of a function is an
exact infinity (e.g., the logarithm of 0 is negative infinity).  When
a pole error occurs, the function returns the (signed) value
HUGE_VAL, HUGE_VALF, or HUGE_VALL, depending on whether the function
result type is double, float, or long double.  The sign of the result
is that which is mathematically correct for the function.  errno is
set to ERANGE, and a "divide-by-zero" (FE_DIVBYZERO) floating-point
exception is raised.

Range error
A range error occurs when the magnitude of the function result means
that it cannot be represented in the result type of the function.
The return value of the function depends on whether the range error
was an overflow or an underflow.

A floating result overflows if the result is finite, but is too large
to represented in the result type.  When an overflow occurs, the
function returns the value HUGE_VAL, HUGE_VALF, or HUGE_VALL, depend‐
ing on whether the function result type is double, float, or long
double.  errno is set to ERANGE, and an "overflow" (FE_OVERFLOW)
floating-point exception is raised.

A floating result underflows if the result is too small to be repre‐
sented in the result type.  If an underflow occurs, a mathematical
function typically returns 0.0 (C99 says a function shall return "an
implementation-defined value whose magnitude is no greater than the
smallest normalized positive number in the specified type").  errno
may be set to ERANGE, and an "overflow" (FE_UNDERFLOW) floating-point
exception may be raised.

Some functions deliver a range error if the supplied argument value,
or the correct function result, would be subnormal.  A subnormal
value is one that is nonzero, but with a magnitude that is so small
that it can't be presented in normalized form (i.e., with a 1 in the
most significant bit of the significand).  The representation of a
subnormal number will contain one or more leading zeros in the sig‐
nificand.
```