math_error(7) — Linux manual page


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

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


       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 |

       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

   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 "underflow" (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‐

NOTES         top

       The math_errhandling identifier specified by C99 and POSIX.1 is not
       supported by glibc.  This identifier is supposed to indicate which of
       the two error-notification mechanisms (errno, exceptions retrievable
       via fetestexcept(3)) is in use.  The standards require that at least
       one be in use, but permit both to be available.  The current (version
       2.8) situation under glibc is messy.  Most (but not all) functions
       raise exceptions on errors.  Some also set errno.  A few functions
       set errno, but don't raise an exception.  A very few functions do
       neither.  See the individual manual pages for details.

       To avoid the complexities of using errno and fetestexcept(3) for
       error checking, it is often advised that one should instead check for
       bad argument values before each call.  For example, the following
       code ensures that log(3)'s argument is not a NaN and is not zero (a
       pole error) or less than zero (a domain error):

           double x, r;

           if (isnan(x) || islessequal(x, 0)) {
               /* Deal with NaN / pole error / domain error */

           r = log(x);

       The discussion on this page does not apply to the complex mathemati‐
       cal functions (i.e., those declared by <complex.h>), which in general
       are not required to return errors by C99 and POSIX.1.

       The gcc(1) -fno-math-errno option causes the executable to employ
       implementations of some mathematical functions that are faster than
       the standard implementations, but do not set errno on error.  (The
       gcc(1) -ffast-math option also enables -fno-math-errno.)  An error
       can still be tested for using fetestexcept(3).

SEE ALSO         top

       gcc(1), errno(3), fenv(3), fpclassify(3), INFINITY(3), isgreater(3),
       matherr(3), nan(3)

       info libc

COLOPHON         top

       This page is part of release 5.08 of the Linux man-pages project.  A
       description of the project, information about reporting bugs, and the
       latest version of this page, can be found at

Linux                            2017-09-15                    MATH_ERROR(7)

Pages that refer to this page: acos(3)acosf(3)acosh(3)acoshf(3)acoshl(3)acosl(3)asin(3)asinf(3)asinl(3)atanh(3)atanhf(3)atanhl(3)cos(3)cosf(3)cosh(3)coshf(3)coshl(3)cosl(3)drem(3)dremf(3)dreml(3)erf(3)erfc(3)erfcf(3)erfcl(3)erff(3)erfl(3)exp10(3)exp10f(3)exp10l(3)exp2(3)exp2f(3)exp2l(3)exp(3)expf(3)expl(3)expm1(3)expm1f(3)expm1l(3)fdim(3)fdimf(3)fdiml(3)feclearexcept(3)fedisableexcept(3)feenableexcept(3)fegetenv(3)fegetexcept(3)fegetexceptflag(3)fegetround(3)feholdexcept(3)fenv(3)feraiseexcept(3)fesetenv(3)fesetexceptflag(3)fesetround(3)fetestexcept(3)feupdateenv(3)fma(3)fmaf(3)fmal(3)fmod(3)fmodf(3)fmodl(3)huge_val(3)HUGE_VAL(3)huge_valf(3)HUGE_VALF(3)huge_vall(3)HUGE_VALL(3)hypot(3)hypotf(3)hypotl(3)ilogb(3)ilogbf(3)ilogbl(3)infinity(3)INFINITY(3)intro(3)j0(3)j0f(3)j0l(3)j1(3)j1f(3)j1l(3)jn(3)jnf(3)jnl(3)ldexp(3)ldexpf(3)ldexpl(3)lgamma(3)lgammaf(3)lgammaf_r(3)lgammal(3)lgammal_r(3)lgamma_r(3)llrint(3)llrintf(3)llrintl(3)llround(3)llroundf(3)llroundl(3)log10(3)log10f(3)log10l(3)log1p(3)log1pf(3)log1pl(3)log2(3)log2f(3)log2l(3)log(3)logb(3)logbf(3)logbl(3)logf(3)logl(3)lrint(3)lrintf(3)lrintl(3)lround(3)lroundf(3)lroundl(3)matherr(3)nan(3)NAN(3)nanf(3)nanl(3)nextafter(3)nextafterf(3)nextafterl(3)nexttoward(3)nexttowardf(3)nexttowardl(3)pow(3)powf(3)powl(3)remainder(3)remainderf(3)remainderl(3)remquo(3)remquof(3)remquol(3)scalb(3)scalbf(3)scalbl(3)scalbln(3)scalblnf(3)scalblnl(3)scalbn(3)scalbnf(3)scalbnl(3)signgam(3)sin(3)sincos(3)sincosf(3)sincosl(3)sinf(3)sinh(3)sinhf(3)sinhl(3)sinl(3)sqrt(3)sqrtf(3)sqrtl(3)tan(3)tanf(3)tanl(3)tgamma(3)tgammaf(3)tgammal(3)y0(3)y0f(3)y0l(3)y1(3)y1f(3)y1l(3)yn(3)ynf(3)ynl(3)