man-pages/man7/math_error.7.html
2021-03-31 01:06:50 +01:00

381 lines
8.5 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of MATH_ERROR</TITLE>
</HEAD><BODY>
<H1>MATH_ERROR</H1>
Section: Linux Programmer's Manual (7)<BR>Updated: 2017-09-15<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
math_error - detecting errors from mathematical functions
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/math.h">math.h</A>&gt;</B>
<B>#include &lt;<A HREF="file:///usr/include/errno.h">errno.h</A>&gt;</B>
<B>#include &lt;<A HREF="file:///usr/include/fenv.h">fenv.h</A>&gt;</B>
</PRE>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
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
<I>&lt;<A HREF="file:///usr/include/math.h">math.h</A>&gt;</I>
indicate an error using other mechanisms.
There are two error-reporting mechanisms:
the older one sets
<I>errno</I>;
the newer one uses the floating-point exception mechanism (the use of
<B><A HREF="/cgi-bin/man/man2html?3+feclearexcept">feclearexcept</A></B>(3)
and
<B><A HREF="/cgi-bin/man/man2html?3+fetestexcept">fetestexcept</A></B>(3),
as outlined below)
described in
<B><A HREF="/cgi-bin/man/man2html?3+fenv">fenv</A></B>(3).
<P>
A portable program that needs to check for an error from a mathematical
function should set
<I>errno</I>
to zero, and make the following call
<P>
feclearexcept(FE_ALL_EXCEPT);
<P>
before calling a mathematical function.
<P>
Upon return from the mathematical function, if
<I>errno</I>
is nonzero, or the following call (see
<B><A HREF="/cgi-bin/man/man2html?3+fenv">fenv</A></B>(3))
returns nonzero
<P>
fetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW |
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FE_UNDERFLOW);
<P>
then an error occurred in the mathematical function.
<P>
The error conditions that can occur for mathematical functions
are described below.
<A NAME="lbAE">&nbsp;</A>
<H3>Domain error</H3>
A
<I>domain error</I>
occurs when a mathematical function is supplied with an argument whose
value falls outside the domain for which the function
is defined (e.g., giving a negative argument to
<B><A HREF="/cgi-bin/man/man2html?3+log">log</A></B>(3)).
When a domain error occurs,
math functions commonly return a NaN
(though some functions return a different value in this case);
<I>errno</I>
is set to
<B>EDOM</B>,
and an &quot;invalid&quot;
(<B>FE_INVALID</B>)
floating-point exception is raised.
<A NAME="lbAF">&nbsp;</A>
<H3>Pole error</H3>
A
<I>pole error</I>
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
<B>HUGE_VAL</B>,
<B>HUGE_VALF</B>,
or
<B>HUGE_VALL</B>,
depending on whether the function result type is
<I>double</I>,
<I>float</I>,
or
<I>long double</I>.
The sign of the result is that which is mathematically correct for
the function.
<I>errno</I>
is set to
<B>ERANGE</B>,
and a &quot;divide-by-zero&quot;
(<B>FE_DIVBYZERO</B>)
floating-point exception is raised.
<A NAME="lbAG">&nbsp;</A>
<H3>Range error</H3>
A
<I>range error</I>
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.
<P>
A floating result
<I>overflows</I>
if the result is finite,
but is too large to represented in the result type.
When an overflow occurs,
the function returns the value
<B>HUGE_VAL</B>,
<B>HUGE_VALF</B>,
or
<B>HUGE_VALL</B>,
depending on whether the function result type is
<I>double</I>,
<I>float</I>,
or
<I>long double</I>.
<I>errno</I>
is set to
<B>ERANGE</B>,
and an &quot;overflow&quot;
(<B>FE_OVERFLOW</B>)
floating-point exception is raised.
<P>
A floating result
<I>underflows</I>
if the result is too small to be represented in the result type.
If an underflow occurs,
a mathematical function typically returns 0.0
(C99 says a function shall return &quot;an implementation-defined value
whose magnitude is no greater than the smallest normalized
positive number in the specified type&quot;).
<I>errno</I>
may be set to
<B>ERANGE</B>,
and an &quot;overflow&quot;
(<B>FE_UNDERFLOW</B>)
floating-point exception may be raised.
<P>
Some functions deliver a range error if the supplied argument value,
or the correct function result, would be
<I>subnormal</I>.
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 significand.
<A NAME="lbAH">&nbsp;</A>
<H2>NOTES</H2>
The
<I>math_errhandling</I>
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
(<I>errno</I>,
exceptions retrievable via
<B><A HREF="/cgi-bin/man/man2html?3+fettestexcept">fettestexcept</A></B>(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
<I>errno</I>.
A few functions set
<I>errno</I>,
but don't raise an exception.
A very few functions do neither.
See the individual manual pages for details.
<P>
To avoid the complexities of using
<I>errno</I>
and
<B><A HREF="/cgi-bin/man/man2html?3+fetestexcept">fetestexcept</A></B>(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
<B><A HREF="/cgi-bin/man/man2html?3+log">log</A></B>(3)'s
argument is not a NaN and is not zero (a pole error) or
less than zero (a domain error):
<P>
double x, r;
<P>
if (isnan(x) || islessequal(x, 0)) {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Deal&nbsp;with&nbsp;NaN&nbsp;/&nbsp;pole&nbsp;error&nbsp;/&nbsp;domain&nbsp;error&nbsp;*/
}
<P>
r = log(x);
<P>
The discussion on this page does not apply to the complex
mathematical functions (i.e., those declared by
<I>&lt;<A HREF="file:///usr/include/complex.h">complex.h</A>&gt;</I>),
which in general are not required to return errors by C99
and POSIX.1.
<P>
The
<B><A HREF="/cgi-bin/man/man2html?1+gcc">gcc</A></B>(1)
<I>-fno-math-errno</I>
option causes the executable to employ implementations of some
mathematical functions that are faster than the standard
implementations, but do not set
<I>errno</I>
on error.
(The
<B><A HREF="/cgi-bin/man/man2html?1+gcc">gcc</A></B>(1)
<I>-ffast-math</I>
option also enables
<I>-fno-math-errno</I>.)
An error can still be tested for using
<B><A HREF="/cgi-bin/man/man2html?3+fetestexcept">fetestexcept</A></B>(3).
<A NAME="lbAI">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?1+gcc">gcc</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?3+errno">errno</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+fenv">fenv</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+fpclassify">fpclassify</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+INFINITY">INFINITY</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+isgreater">isgreater</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+matherr">matherr</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+nan">nan</A></B>(3)
<P>
<I>info libc</I>
<A NAME="lbAJ">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="1"><A HREF="#lbAB">NAME</A><DD>
<DT id="2"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="3"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DL>
<DT id="4"><A HREF="#lbAE">Domain error</A><DD>
<DT id="5"><A HREF="#lbAF">Pole error</A><DD>
<DT id="6"><A HREF="#lbAG">Range error</A><DD>
</DL>
<DT id="7"><A HREF="#lbAH">NOTES</A><DD>
<DT id="8"><A HREF="#lbAI">SEE ALSO</A><DD>
<DT id="9"><A HREF="#lbAJ">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:06:09 GMT, March 31, 2021
</BODY>
</HTML>