2008-07-23 14:05:42 +00:00
|
|
|
.\" Copyright (c) 2008, Linux Foundation, written by Michael Kerrisk
|
|
|
|
.\" <mtk.manpages@gmail.com>
|
|
|
|
.\"
|
|
|
|
.\" Permission is granted to make and distribute verbatim copies of this
|
|
|
|
.\" manual provided the copyright notice and this permission notice are
|
|
|
|
.\" preserved on all copies.
|
|
|
|
.\"
|
|
|
|
.\" Permission is granted to copy and distribute modified versions of this
|
|
|
|
.\" manual under the conditions for verbatim copying, provided that the
|
|
|
|
.\" entire resulting derived work is distributed under the terms of a
|
|
|
|
.\" permission notice identical to this one.
|
|
|
|
.\"
|
|
|
|
.\" Since the Linux kernel and libraries are constantly changing, this
|
|
|
|
.\" manual page may be incorrect or out-of-date. The author(s) assume no
|
|
|
|
.\" responsibility for errors or omissions, or for damages resulting from
|
|
|
|
.\" the use of the information contained herein. The author(s) may not
|
|
|
|
.\" have taken the same level of care in the production of this manual,
|
|
|
|
.\" which is licensed free of charge, as they might when working
|
|
|
|
.\" professionally.
|
|
|
|
.\"
|
|
|
|
.\" Formatted or processed versions of this manual, if unaccompanied by
|
|
|
|
.\" the source, must acknowledge the copyright and authors of this work.
|
|
|
|
.\"
|
2008-08-11 16:35:43 +00:00
|
|
|
.TH MATH_ERROR 7 2008-08-11 "Linux" "Linux Programmer's Manual"
|
2008-07-24 03:59:56 +00:00
|
|
|
.SH NAME
|
|
|
|
math_error \- detecting errors from mathematical functions
|
2008-07-23 14:05:42 +00:00
|
|
|
.SH SYNOPSIS
|
|
|
|
.nf
|
|
|
|
.B #include <math.h>
|
|
|
|
.B #include <errno.h>
|
|
|
|
.B #include <fenv.h>
|
|
|
|
.fi
|
|
|
|
.SH DESCRIPTION
|
2008-08-11 16:35:43 +00:00
|
|
|
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
|
2008-07-23 14:05:42 +00:00
|
|
|
.IR <math.h>
|
2008-08-11 16:35:43 +00:00
|
|
|
indicate an error using other mechanisms.
|
|
|
|
There are two error-reporting mechanisms:
|
2008-07-23 14:05:42 +00:00
|
|
|
the older one sets
|
|
|
|
.IR errno ;
|
|
|
|
the newer one uses the floating-point exception mechanism (the use of
|
|
|
|
.BR feclearexcept (3)
|
|
|
|
and
|
|
|
|
.BR fetestexcept (3),
|
|
|
|
as outlined below)
|
|
|
|
described in
|
|
|
|
.BR fenv (3).
|
|
|
|
|
|
|
|
A portable program that needs to check for an error from a mathematical
|
|
|
|
function should set
|
|
|
|
.I errno
|
|
|
|
to zero, and make the following call
|
|
|
|
.in +4n
|
|
|
|
.nf
|
|
|
|
|
|
|
|
feclearexcept(FE_ALL_EXCEPT);
|
|
|
|
|
|
|
|
.fi
|
|
|
|
.in
|
|
|
|
before calling a mathematical function.
|
|
|
|
|
|
|
|
Upon return from the mathematical function, if
|
|
|
|
.I errno
|
intro.1, time.1, adjtimex.2, capget.2, eventfd.2, fcntl.2, getrlimit.2, getsockopt.2, gettimeofday.2, intro.2, ioctl_list.2, ioperm.2, mlock.2, pivot_root.2, poll.2, prctl.2, ptrace.2, sched_setscheduler.2, select_tut.2, semget.2, sigaltstack.2, signalfd.2, sysctl.2, timer_settime.2, timerfd_create.2, wait.2, CPU_SET.3, argz_add.3, assert_perror.3, atexit.3, backtrace.3, bcmp.3, clearenv.3, ctime.3, dl_iterate_phdr.3, dlopen.3, ecvt.3, errno.3, error.3, ether_aton.3, exit.3, fenv.3, ferror.3, finite.3, flockfile.3, fnmatch.3, fpathconf.3, fpclassify.3, ftime.3, ftok.3, ftw.3, fwide.3, getaddrinfo.3, gethostbyname.3, getlogin.3, getnameinfo.3, getnetent.3, getopt.3, getprotoent.3, getrpcent.3, getservent.3, glob.3, hsearch.3, inet.3, isalpha.3, iswalnum.3, iswalpha.3, iswblank.3, iswcntrl.3, iswctype.3, iswdigit.3, iswgraph.3, iswlower.3, iswprint.3, iswpunct.3, iswspace.3, iswupper.3, iswxdigit.3, longjmp.3, lsearch.3, malloc.3, matherr.3, mblen.3, mbsinit.3, mbtowc.3, on_exit.3, printf.3, pthread_attr_init.3, pthread_attr_setaffinity_np.3, pthread_attr_setdetachstate.3, pthread_attr_setguardsize.3, pthread_attr_setinheritsched.3, pthread_attr_setschedparam.3, pthread_attr_setschedpolicy.3, pthread_attr_setscope.3, pthread_attr_setstack.3, pthread_attr_setstackaddr.3, pthread_attr_setstacksize.3, pthread_cancel.3, pthread_cleanup_push.3, pthread_equal.3, pthread_getattr_np.3, pthread_getcpuclockid.3, pthread_setaffinity_np.3, pthread_setcancelstate.3, pthread_setconcurrency.3, pthread_setschedparam.3, pthread_setschedprio.3, ptsname.3, putenv.3, putgrent.3, raise.3, rcmd.3, regex.3, rexec.3, rpc.3, rpmatch.3, rtnetlink.3, scandir.3, sem_init.3, setaliasent.3, setbuf.3, setenv.3, setjmp.3, signbit.3, stdio_ext.3, strtod.3, strtol.3, strtoul.3, system.3, termios.3, timeradd.3, tzset.3, ualarm.3, wctomb.3, xdr.3, st.4, tty_ioctl.4, core.5, elf.5, proc.5, bootparam.7, capabilities.7, icmp.7, ip.7, ipv6.7, math_error.7, mdoc.samples.7, mq_overview.7, pthreads.7, raw.7, regex.7, socket.7, tcp.7, tzselect.8: Global fix: s/non-zero/nonzero/
The tendency in English, as prescribed in style guides like
Chicago MoS, is towards removing hyphens after prefixes
like "non-" etc.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2010-01-16 16:40:55 +00:00
|
|
|
is nonzero, or the following call (see
|
2008-07-23 14:05:42 +00:00
|
|
|
.BR fenv (3))
|
intro.1, time.1, adjtimex.2, capget.2, eventfd.2, fcntl.2, getrlimit.2, getsockopt.2, gettimeofday.2, intro.2, ioctl_list.2, ioperm.2, mlock.2, pivot_root.2, poll.2, prctl.2, ptrace.2, sched_setscheduler.2, select_tut.2, semget.2, sigaltstack.2, signalfd.2, sysctl.2, timer_settime.2, timerfd_create.2, wait.2, CPU_SET.3, argz_add.3, assert_perror.3, atexit.3, backtrace.3, bcmp.3, clearenv.3, ctime.3, dl_iterate_phdr.3, dlopen.3, ecvt.3, errno.3, error.3, ether_aton.3, exit.3, fenv.3, ferror.3, finite.3, flockfile.3, fnmatch.3, fpathconf.3, fpclassify.3, ftime.3, ftok.3, ftw.3, fwide.3, getaddrinfo.3, gethostbyname.3, getlogin.3, getnameinfo.3, getnetent.3, getopt.3, getprotoent.3, getrpcent.3, getservent.3, glob.3, hsearch.3, inet.3, isalpha.3, iswalnum.3, iswalpha.3, iswblank.3, iswcntrl.3, iswctype.3, iswdigit.3, iswgraph.3, iswlower.3, iswprint.3, iswpunct.3, iswspace.3, iswupper.3, iswxdigit.3, longjmp.3, lsearch.3, malloc.3, matherr.3, mblen.3, mbsinit.3, mbtowc.3, on_exit.3, printf.3, pthread_attr_init.3, pthread_attr_setaffinity_np.3, pthread_attr_setdetachstate.3, pthread_attr_setguardsize.3, pthread_attr_setinheritsched.3, pthread_attr_setschedparam.3, pthread_attr_setschedpolicy.3, pthread_attr_setscope.3, pthread_attr_setstack.3, pthread_attr_setstackaddr.3, pthread_attr_setstacksize.3, pthread_cancel.3, pthread_cleanup_push.3, pthread_equal.3, pthread_getattr_np.3, pthread_getcpuclockid.3, pthread_setaffinity_np.3, pthread_setcancelstate.3, pthread_setconcurrency.3, pthread_setschedparam.3, pthread_setschedprio.3, ptsname.3, putenv.3, putgrent.3, raise.3, rcmd.3, regex.3, rexec.3, rpc.3, rpmatch.3, rtnetlink.3, scandir.3, sem_init.3, setaliasent.3, setbuf.3, setenv.3, setjmp.3, signbit.3, stdio_ext.3, strtod.3, strtol.3, strtoul.3, system.3, termios.3, timeradd.3, tzset.3, ualarm.3, wctomb.3, xdr.3, st.4, tty_ioctl.4, core.5, elf.5, proc.5, bootparam.7, capabilities.7, icmp.7, ip.7, ipv6.7, math_error.7, mdoc.samples.7, mq_overview.7, pthreads.7, raw.7, regex.7, socket.7, tcp.7, tzselect.8: Global fix: s/non-zero/nonzero/
The tendency in English, as prescribed in style guides like
Chicago MoS, is towards removing hyphens after prefixes
like "non-" etc.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2010-01-16 16:40:55 +00:00
|
|
|
returns nonzero
|
2008-07-23 14:05:42 +00:00
|
|
|
.in +4n
|
|
|
|
.nf
|
|
|
|
|
|
|
|
fetestexcept(FE_INVALID | FE_DIVBYZERO | FE_OVERFLOW |
|
|
|
|
FE_UNDERFLOW);
|
|
|
|
|
|
|
|
.fi
|
|
|
|
.in
|
|
|
|
.\" enum
|
|
|
|
.\" {
|
|
|
|
.\" FE_INVALID = 0x01,
|
|
|
|
.\" __FE_DENORM = 0x02,
|
|
|
|
.\" FE_DIVBYZERO = 0x04,
|
|
|
|
.\" FE_OVERFLOW = 0x08,
|
|
|
|
.\" FE_UNDERFLOW = 0x10,
|
|
|
|
.\" FE_INEXACT = 0x20
|
|
|
|
.\" };
|
|
|
|
then an error occurred in the mathematical function.
|
|
|
|
|
|
|
|
The error conditions that can occur for mathematical functions
|
|
|
|
are described below.
|
|
|
|
.SS Domain Error
|
|
|
|
A
|
|
|
|
.I domain error
|
|
|
|
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
|
|
|
|
.BR log (3)).
|
|
|
|
When a domain error occurs,
|
2008-08-11 16:35:43 +00:00
|
|
|
math functions commonly return a NaN
|
|
|
|
(though some functions return a different value in this case);
|
2008-07-23 14:05:42 +00:00
|
|
|
.I errno
|
|
|
|
is set to
|
|
|
|
.BR EDOM ,
|
|
|
|
and an "invalid"
|
|
|
|
.RB ( FE_INVALID )
|
|
|
|
floating-point exception is raised.
|
|
|
|
.SS Pole Error
|
|
|
|
A
|
|
|
|
.I 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
|
|
|
|
.BR HUGE_VAL ,
|
|
|
|
.BR HUGE_VALF ,
|
|
|
|
or
|
|
|
|
.BR HUGE_VALL ,
|
|
|
|
depending on whether the function result type is
|
|
|
|
.IR double ,
|
|
|
|
.IR float ,
|
|
|
|
or
|
|
|
|
.IR "long double" .
|
|
|
|
The sign of the result is that which is mathematically correct for
|
|
|
|
the function.
|
|
|
|
.I errno
|
|
|
|
is set to
|
|
|
|
.BR ERANGE ,
|
|
|
|
and a "divide-by-zero"
|
|
|
|
.RB ( FE_DIVBYZERO )
|
|
|
|
floating-point exception is raised.
|
|
|
|
.SS Range Error
|
|
|
|
A
|
|
|
|
.I 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
|
|
|
|
.I 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
|
|
|
|
.BR HUGE_VAL ,
|
|
|
|
.BR HUGE_VALF ,
|
|
|
|
or
|
|
|
|
.BR HUGE_VALL ,
|
|
|
|
depending on whether the function result type is
|
|
|
|
.IR double ,
|
|
|
|
.IR float ,
|
|
|
|
or
|
|
|
|
.IR "long double" .
|
|
|
|
.I errno
|
|
|
|
is set to
|
|
|
|
.BR ERANGE ,
|
|
|
|
and an "overflow"
|
|
|
|
.RB ( FE_OVERFLOW )
|
|
|
|
floating-point exception is raised.
|
|
|
|
|
|
|
|
A floating result
|
|
|
|
.I underflows
|
|
|
|
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 "an implementation-defined value
|
|
|
|
whose magnitude is no greater than the smallest normalized
|
|
|
|
positive number in the specified type").
|
|
|
|
.I errno
|
|
|
|
may be set to
|
|
|
|
.BR ERANGE ,
|
|
|
|
and an "overflow"
|
|
|
|
.RB ( 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
|
|
|
|
.IR subnormal .
|
intro.1, time.1, adjtimex.2, capget.2, eventfd.2, fcntl.2, getrlimit.2, getsockopt.2, gettimeofday.2, intro.2, ioctl_list.2, ioperm.2, mlock.2, pivot_root.2, poll.2, prctl.2, ptrace.2, sched_setscheduler.2, select_tut.2, semget.2, sigaltstack.2, signalfd.2, sysctl.2, timer_settime.2, timerfd_create.2, wait.2, CPU_SET.3, argz_add.3, assert_perror.3, atexit.3, backtrace.3, bcmp.3, clearenv.3, ctime.3, dl_iterate_phdr.3, dlopen.3, ecvt.3, errno.3, error.3, ether_aton.3, exit.3, fenv.3, ferror.3, finite.3, flockfile.3, fnmatch.3, fpathconf.3, fpclassify.3, ftime.3, ftok.3, ftw.3, fwide.3, getaddrinfo.3, gethostbyname.3, getlogin.3, getnameinfo.3, getnetent.3, getopt.3, getprotoent.3, getrpcent.3, getservent.3, glob.3, hsearch.3, inet.3, isalpha.3, iswalnum.3, iswalpha.3, iswblank.3, iswcntrl.3, iswctype.3, iswdigit.3, iswgraph.3, iswlower.3, iswprint.3, iswpunct.3, iswspace.3, iswupper.3, iswxdigit.3, longjmp.3, lsearch.3, malloc.3, matherr.3, mblen.3, mbsinit.3, mbtowc.3, on_exit.3, printf.3, pthread_attr_init.3, pthread_attr_setaffinity_np.3, pthread_attr_setdetachstate.3, pthread_attr_setguardsize.3, pthread_attr_setinheritsched.3, pthread_attr_setschedparam.3, pthread_attr_setschedpolicy.3, pthread_attr_setscope.3, pthread_attr_setstack.3, pthread_attr_setstackaddr.3, pthread_attr_setstacksize.3, pthread_cancel.3, pthread_cleanup_push.3, pthread_equal.3, pthread_getattr_np.3, pthread_getcpuclockid.3, pthread_setaffinity_np.3, pthread_setcancelstate.3, pthread_setconcurrency.3, pthread_setschedparam.3, pthread_setschedprio.3, ptsname.3, putenv.3, putgrent.3, raise.3, rcmd.3, regex.3, rexec.3, rpc.3, rpmatch.3, rtnetlink.3, scandir.3, sem_init.3, setaliasent.3, setbuf.3, setenv.3, setjmp.3, signbit.3, stdio_ext.3, strtod.3, strtol.3, strtoul.3, system.3, termios.3, timeradd.3, tzset.3, ualarm.3, wctomb.3, xdr.3, st.4, tty_ioctl.4, core.5, elf.5, proc.5, bootparam.7, capabilities.7, icmp.7, ip.7, ipv6.7, math_error.7, mdoc.samples.7, mq_overview.7, pthreads.7, raw.7, regex.7, socket.7, tcp.7, tzselect.8: Global fix: s/non-zero/nonzero/
The tendency in English, as prescribed in style guides like
Chicago MoS, is towards removing hyphens after prefixes
like "non-" etc.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2010-01-16 16:40:55 +00:00
|
|
|
A subnormal value is one that is nonzero,
|
2008-07-23 14:05:42 +00:00
|
|
|
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.
|
|
|
|
.SH NOTES
|
|
|
|
The
|
|
|
|
.I math_errhandling
|
2008-07-30 07:16:18 +00:00
|
|
|
identifier specified by C99 and POSIX.1-2001 is not supported by glibc.
|
2008-07-23 14:05:42 +00:00
|
|
|
.\" See CONFORMANCE in the glibc 2.8 (and earlier) source.
|
2008-08-21 07:18:36 +00:00
|
|
|
This identifier is supposed to indicate which of the two
|
2008-07-30 07:16:18 +00:00
|
|
|
error-notification mechanisms
|
|
|
|
.RI ( errno ,
|
|
|
|
exceptions retrievable via
|
|
|
|
.BR fettestexcept (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
|
|
|
|
.IR errno .
|
|
|
|
A few functions set
|
|
|
|
.IR errno ,
|
|
|
|
but don't raise an exception.
|
|
|
|
A very few functions do neither.
|
|
|
|
See the individual manual pages for details.
|
2008-07-23 14:05:42 +00:00
|
|
|
|
|
|
|
To avoid the complexities of using
|
|
|
|
.I errno
|
|
|
|
and
|
|
|
|
.BR fetestexcept (3)
|
|
|
|
for error checking,
|
|
|
|
it is often advised that one should instead check for bad argument
|
|
|
|
values before each call.
|
|
|
|
.\" http://www.securecoding.cert.org/confluence/display/seccode/FLP32-C.+Prevent+or+detect+domain+and+range+errors+in+math+functions
|
|
|
|
For example, the following code ensures that
|
|
|
|
.BR log (3)'s
|
|
|
|
argument is not a NaN and is not zero (a pole error) or
|
|
|
|
less than zero (a domain error):
|
|
|
|
.in +4n
|
|
|
|
.nf
|
|
|
|
|
|
|
|
double x, r;
|
|
|
|
|
|
|
|
if (isnan(x) || islessequal(x, 0)) {
|
|
|
|
/* Deal with NaN / pole error / domain error */
|
|
|
|
}
|
|
|
|
|
|
|
|
r = log(x);
|
|
|
|
|
|
|
|
.fi
|
|
|
|
.in
|
|
|
|
The discussion on this page does not apply to the complex
|
|
|
|
mathematical functions (i.e., those declared by
|
|
|
|
.IR <complex.h> ),
|
|
|
|
which in general are not required to return errors by C99
|
|
|
|
and POSIX.1-2001.
|
|
|
|
|
|
|
|
The
|
|
|
|
.BR gcc (1)
|
|
|
|
.I "-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
|
|
|
|
.I errno
|
|
|
|
on error.
|
|
|
|
(The
|
|
|
|
.BR gcc (1)
|
|
|
|
.I "-ffast-math"
|
|
|
|
option also enables
|
|
|
|
.IR "-fno-math-errno" .)
|
|
|
|
An error can still be tested for using
|
|
|
|
.BR fetestexcept (3).
|
|
|
|
.SH SEE ALSO
|
|
|
|
.BR gcc (1),
|
|
|
|
.BR errno (3),
|
|
|
|
.BR fenv (3),
|
|
|
|
.BR fpclassify (3),
|
|
|
|
.BR INFINITY (3),
|
|
|
|
.BR isgreater (3),
|
|
|
|
.BR matherr (3),
|
|
|
|
.BR nan (3)
|
|
|
|
.br
|
|
|
|
.I "info libc"
|