xref: /freebsd/lib/msun/man/math.3 (revision 24a0682c6465290759ed0b09ea16e40e7cd47053)
13a8617a8SJordan K. Hubbard.\" Copyright (c) 1985 Regents of the University of California.
23a8617a8SJordan K. Hubbard.\" All rights reserved.
33a8617a8SJordan K. Hubbard.\"
43a8617a8SJordan K. Hubbard.\" Redistribution and use in source and binary forms, with or without
53a8617a8SJordan K. Hubbard.\" modification, are permitted provided that the following conditions
63a8617a8SJordan K. Hubbard.\" are met:
73a8617a8SJordan K. Hubbard.\" 1. Redistributions of source code must retain the above copyright
83a8617a8SJordan K. Hubbard.\"    notice, this list of conditions and the following disclaimer.
93a8617a8SJordan K. Hubbard.\" 2. Redistributions in binary form must reproduce the above copyright
103a8617a8SJordan K. Hubbard.\"    notice, this list of conditions and the following disclaimer in the
113a8617a8SJordan K. Hubbard.\"    documentation and/or other materials provided with the distribution.
123a8617a8SJordan K. Hubbard.\" 3. All advertising materials mentioning features or use of this software
133a8617a8SJordan K. Hubbard.\"    must display the following acknowledgement:
143a8617a8SJordan K. Hubbard.\"	This product includes software developed by the University of
153a8617a8SJordan K. Hubbard.\"	California, Berkeley and its contributors.
163a8617a8SJordan K. Hubbard.\" 4. Neither the name of the University nor the names of its contributors
173a8617a8SJordan K. Hubbard.\"    may be used to endorse or promote products derived from this software
183a8617a8SJordan K. Hubbard.\"    without specific prior written permission.
193a8617a8SJordan K. Hubbard.\"
203a8617a8SJordan K. Hubbard.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
213a8617a8SJordan K. Hubbard.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
223a8617a8SJordan K. Hubbard.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
233a8617a8SJordan K. Hubbard.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
243a8617a8SJordan K. Hubbard.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
253a8617a8SJordan K. Hubbard.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
263a8617a8SJordan K. Hubbard.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
273a8617a8SJordan K. Hubbard.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
283a8617a8SJordan K. Hubbard.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
293a8617a8SJordan K. Hubbard.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
303a8617a8SJordan K. Hubbard.\" SUCH DAMAGE.
313a8617a8SJordan K. Hubbard.\"
323a8617a8SJordan K. Hubbard.\"	from: @(#)math.3	6.10 (Berkeley) 5/6/91
337f3dea24SPeter Wemm.\" $FreeBSD$
343a8617a8SJordan K. Hubbard.\"
352aac156dSDavid Schultz.Dd January 11, 2005
362a6bf1faSDavid Schultz.Dt MATH 3
372a6bf1faSDavid Schultz.Os
384e05ab77SRuslan Ermilov.if n \{\
394e05ab77SRuslan Ermilov.char \[sr] "sqrt
404e05ab77SRuslan Ermilov.\}
412a6bf1faSDavid Schultz.Sh NAME
424e05ab77SRuslan Ermilov.Nm math
434e05ab77SRuslan Ermilov.Nd "floating-point mathematical library"
44dbc8f2b5SDavid Schultz.Sh LIBRARY
45dbc8f2b5SDavid Schultz.Lb libm
46dbc8f2b5SDavid Schultz.Sh SYNOPSIS
47dbc8f2b5SDavid Schultz.In math.h
482a6bf1faSDavid Schultz.Sh DESCRIPTION
49dbc8f2b5SDavid SchultzThese functions constitute the C math library.
502a6bf1faSDavid Schultz.Sh "LIST OF FUNCTIONS"
512a6bf1faSDavid SchultzEach of the following
522a6bf1faSDavid Schultz.Vt double
532a6bf1faSDavid Schultzfunctions has a
542a6bf1faSDavid Schultz.Vt float
552a6bf1faSDavid Schultzcounterpart with an
562a6bf1faSDavid Schultz.Ql f
572a6bf1faSDavid Schultzappended to the name and a
584e05ab77SRuslan Ermilov.Vt "long double"
592a6bf1faSDavid Schultzcounterpart with an
602a6bf1faSDavid Schultz.Ql l
612a6bf1faSDavid Schultzappended.
622a6bf1faSDavid SchultzAs an example, the
632a6bf1faSDavid Schultz.Vt float
642a6bf1faSDavid Schultzand
654e05ab77SRuslan Ermilov.Vt "long double"
662a6bf1faSDavid Schultzcounterparts of
672a6bf1faSDavid Schultz.Ft double
682a6bf1faSDavid Schultz.Fn acos "double x"
692a6bf1faSDavid Schultzare
702a6bf1faSDavid Schultz.Ft float
712a6bf1faSDavid Schultz.Fn acosf "float x"
722a6bf1faSDavid Schultzand
734e05ab77SRuslan Ermilov.Ft "long double"
742a6bf1faSDavid Schultz.Fn acosl "long double x" ,
752a6bf1faSDavid Schultzrespectively.
76dbc8f2b5SDavid Schultz.Pp
77dbc8f2b5SDavid SchultzThe programs are accurate to within the numbers
784e05ab77SRuslan Ermilovof
794e05ab77SRuslan Ermilov.Em ulp Ns s
804e05ab77SRuslan Ermilovtabulated below; an
814e05ab77SRuslan Ermilov.Em ulp
824e05ab77SRuslan Ermilovis one
834e05ab77SRuslan Ermilov.Em U Ns nit
844e05ab77SRuslan Ermilovin the
854e05ab77SRuslan Ermilov.Em L Ns ast
864e05ab77SRuslan Ermilov.Em P Ns lace .
874e05ab77SRuslan Ermilov.Bl -column "nexttoward" "remainder with partial quotient"
884e05ab77SRuslan Ermilov.Em "Name	Description	Error Bound (ULPs)"
892a6bf1faSDavid Schultz.\" XXX Many of these error bounds are wrong for the current implementation!
90dbc8f2b5SDavid Schultzacos	inverse trigonometric function	???
91dbc8f2b5SDavid Schultzacosh	inverse hyperbolic function	???
92dbc8f2b5SDavid Schultzasin	inverse trigonometric function	???
93dbc8f2b5SDavid Schultzasinh	inverse hyperbolic function	???
94dbc8f2b5SDavid Schultzatan	inverse trigonometric function	???
95dbc8f2b5SDavid Schultzatanh	inverse hyperbolic function	???
96dbc8f2b5SDavid Schultzatan2	inverse trigonometric function	???
972a6bf1faSDavid Schultzcbrt	cube root	1
982a6bf1faSDavid Schultzceil	integer no less than	0
992a6bf1faSDavid Schultzcopysign	copy sign bit	0
1002a6bf1faSDavid Schultzcos	trigonometric function	1
101dbc8f2b5SDavid Schultzcosh	hyperbolic function	???
102dbc8f2b5SDavid Schultzerf	error function	1
103dbc8f2b5SDavid Schultzerfc	complementary error function	1
1042a6bf1faSDavid Schultzexp	exponential base e	1
1052a6bf1faSDavid Schultz.\" exp2	exponential base 2	???
1062a6bf1faSDavid Schultzexpm1	exp(x)\-1	1
1072a6bf1faSDavid Schultzfabs	absolute value	0
108dbc8f2b5SDavid Schultzfdim	positive difference	1
1092a6bf1faSDavid Schultzfloor	integer no greater than	0
1102a6bf1faSDavid Schultz.\" fma	multiply-add	???
111dbc8f2b5SDavid Schultzfmax	maximum function	0
112dbc8f2b5SDavid Schultzfmin	minimum function	0
1132a6bf1faSDavid Schultzfmod	remainder function	???
1142a6bf1faSDavid Schultzfrexp	extract mantissa and exponent	0
1152a6bf1faSDavid Schultzhypot	Euclidean distance	1
1162a6bf1faSDavid Schultzilogb	exponent extraction	0
1172a6bf1faSDavid Schultzj0	bessel function	???
1182a6bf1faSDavid Schultzj1	bessel function	???
1192a6bf1faSDavid Schultzjn	bessel function	???
1202a6bf1faSDavid Schultzldexp	multiply by power of 2	0
121dbc8f2b5SDavid Schultzlgamma	log gamma function	1
1222aac156dSDavid Schultzllrint	round to integer	0
1232aac156dSDavid Schultzllround	round to nearest integer	0
1242a6bf1faSDavid Schultzlog	natural logarithm	1
125dbc8f2b5SDavid Schultzlog10	logarithm to base 10	1
1262a6bf1faSDavid Schultzlog1p	log(1+x)	1
1272a6bf1faSDavid Schultz.\" log2	base 2 logarithm	0
1282a6bf1faSDavid Schultzlogb	exponent extraction	0
1292aac156dSDavid Schultzlrint	round to integer	0
1302aac156dSDavid Schultzlround	round to nearest integer	0
131dbc8f2b5SDavid Schultzmodf	extract fractional part	0
1322a6bf1faSDavid Schultz.\" nan	return quiet \*(Na)	0
133dbc8f2b5SDavid Schultznearbyint	round to integer	0
134f7748f6eSDavid Schultznextafter	next representable value	0
1352a6bf1faSDavid Schultz.\" nexttoward	next representable value	0
1364e05ab77SRuslan Ermilovpow	exponential x**y	60-500
1372a6bf1faSDavid Schultzremainder	remainder	0
1382a6bf1faSDavid Schultz.\" remquo	remainder with partial quotient	???
1392a6bf1faSDavid Schultzrint	round to nearest integer	0
1402a6bf1faSDavid Schultzround	round to nearest integer	0
141f7748f6eSDavid Schultzscalbln	exponent adjustment	0
1422a6bf1faSDavid Schultzscalbn	exponent adjustment	0
1432a6bf1faSDavid Schultzsin	trigonometric function	1
144dbc8f2b5SDavid Schultzsinh	hyperbolic function	???
1452a6bf1faSDavid Schultzsqrt	square root	1
146dbc8f2b5SDavid Schultztan	trigonometric function	1
147dbc8f2b5SDavid Schultztanh	hyperbolic function	???
148dbc8f2b5SDavid Schultztgamma	gamma function	1
149f7748f6eSDavid Schultztrunc	round towards zero	0
1502a6bf1faSDavid Schultzy0	bessel function	???
1512a6bf1faSDavid Schultzy1	bessel function	???
1522a6bf1faSDavid Schultzyn	bessel function	???
1534e05ab77SRuslan Ermilov.El
1542a6bf1faSDavid Schultz.Sh NOTES
1552a6bf1faSDavid SchultzVirtually all modern floating-point units attempt to support
1562a6bf1faSDavid SchultzIEEE Standard 754 for Binary Floating-Point Arithmetic.
1572a6bf1faSDavid SchultzThis standard does not cover particular routines in the math library
1582a6bf1faSDavid Schultzexcept for the few documented in
1592a6bf1faSDavid Schultz.Xr ieee 3 ;
1602a6bf1faSDavid Schultzit primarily defines representations of numbers and abstract
1612a6bf1faSDavid Schultzproperties of arithmetic operations relating to precision, rounding,
1622a6bf1faSDavid Schultzand exceptional cases, as described below.
1634e05ab77SRuslan Ermilov.Ss IEEE STANDARD 754 Floating-Point Arithmetic
164dbc8f2b5SDavid Schultz.\" XXX mention single- and extended-/quad- precisions
1654e05ab77SRuslan ErmilovProperties of IEEE 754 Double-Precision:
1664e05ab77SRuslan Ermilov.Bd -ragged -offset indent -compact
1674e05ab77SRuslan ErmilovWordsize: 64 bits, 8 bytes.
1684e05ab77SRuslan Ermilov.Pp
1694e05ab77SRuslan ErmilovRadix: Binary.
1704e05ab77SRuslan Ermilov.Pp
1714e05ab77SRuslan ErmilovPrecision: 53 significant bits,
1724e05ab77SRuslan Ermilovroughly like 16 significant decimals.
1734e05ab77SRuslan Ermilov.Bd -ragged -offset indent -compact
1744e05ab77SRuslan ErmilovIf x and x' are consecutive positive Double-Precision
1754e05ab77SRuslan Ermilovnumbers (they differ by 1
1764e05ab77SRuslan Ermilov.Em ulp ) ,
1774e05ab77SRuslan Ermilovthen
1784e05ab77SRuslan Ermilov.Bd -ragged -compact
1793a8617a8SJordan K. Hubbard1.1e\-16 < 0.5**53 < (x'\-x)/x \(<= 0.5**52 < 2.3e\-16.
1802a6bf1faSDavid Schultz.Ed
1814e05ab77SRuslan Ermilov.Ed
1824e05ab77SRuslan Ermilov.Pp
1834e05ab77SRuslan Ermilov.Bl -column "XXX" -compact
1843a8617a8SJordan K. HubbardRange:	Overflow threshold  = 2.0**1024 = 1.8e308
1853a8617a8SJordan K. Hubbard	Underflow threshold = 0.5**1022 = 2.2e\-308
1864e05ab77SRuslan Ermilov.El
1874e05ab77SRuslan Ermilov.Bd -ragged -offset indent -compact
1885391441cSRuslan ErmilovOverflow goes by default to a signed \*(If.
1894e05ab77SRuslan ErmilovUnderflow is
1904e05ab77SRuslan Ermilov.Em Gradual ,
1914e05ab77SRuslan Ermilovrounding to the nearest
1923a8617a8SJordan K. Hubbardinteger multiple of 0.5**1074 = 4.9e\-324.
1932a6bf1faSDavid Schultz.Ed
1944e05ab77SRuslan Ermilov.Pp
1953a8617a8SJordan K. HubbardZero is represented ambiguously as +0 or \-0.
1964e05ab77SRuslan Ermilov.Bd -ragged -offset indent -compact
1973a8617a8SJordan K. HubbardIts sign transforms correctly through multiplication or
1983a8617a8SJordan K. Hubbarddivision, and is preserved by addition of zeros
1993a8617a8SJordan K. Hubbardwith like signs; but x\-x yields +0 for every
2004e05ab77SRuslan Ermilovfinite x.
2014e05ab77SRuslan ErmilovThe only operations that reveal zero's
2024e05ab77SRuslan Ermilovsign are division by zero and
2034e05ab77SRuslan Ermilov.Fn copysign x \(+-0 .
2044e05ab77SRuslan ErmilovIn particular, comparison (x > y, x \(>= y, etc.)\&
2053a8617a8SJordan K. Hubbardcannot be affected by the sign of zero; but if
2065391441cSRuslan Ermilovfinite x = y then \*(If = 1/(x\-y) \(!= \-1/(y\-x) = \-\*(If.
2072a6bf1faSDavid Schultz.Ed
2084e05ab77SRuslan Ermilov.Pp
2095391441cSRuslan Ermilov\*(If is signed.
2104e05ab77SRuslan Ermilov.Bd -ragged -offset indent -compact
2114e05ab77SRuslan ErmilovIt persists when added to itself
2124e05ab77SRuslan Ermilovor to any finite number.
2134e05ab77SRuslan ErmilovIts sign transforms
2143a8617a8SJordan K. Hubbardcorrectly through multiplication and division, and
2155391441cSRuslan Ermilov(finite)/\(+-\*(If\0=\0\(+-0
2165391441cSRuslan Ermilov(nonzero)/0 = \(+-\*(If.
2173a8617a8SJordan K. HubbardBut
2185391441cSRuslan Ermilov\*(If\-\*(If, \*(If\(**0 and \*(If/\*(If
2193a8617a8SJordan K. Hubbardare, like 0/0 and sqrt(\-3),
2202a6bf1faSDavid Schultzinvalid operations that produce \*(Na. ...
2212a6bf1faSDavid Schultz.Ed
2224e05ab77SRuslan Ermilov.Pp
2233a8617a8SJordan K. HubbardReserved operands:
2244e05ab77SRuslan Ermilov.Bd -ragged -offset indent -compact
2253a8617a8SJordan K. Hubbardthere are 2**53\-2 of them, all
2264e05ab77SRuslan Ermilovcalled \*(Na
2274e05ab77SRuslan Ermilov.Em ( N Ns ot Em a N Ns umber ) .
2284e05ab77SRuslan ErmilovSome, called Signaling \*(Nas, trap any floating-point operation
2293a8617a8SJordan K. Hubbardperformed upon them; they are used to mark missing
2303a8617a8SJordan K. Hubbardor uninitialized values, or nonexistent elements
2314e05ab77SRuslan Ermilovof arrays.
2324e05ab77SRuslan ErmilovThe rest are Quiet \*(Nas; they are
2333a8617a8SJordan K. Hubbardthe default results of Invalid Operations, and
2343a8617a8SJordan K. Hubbardpropagate through subsequent arithmetic operations.
2354e05ab77SRuslan ErmilovIf x \(!= x then x is \*(Na; every other predicate
2362a6bf1faSDavid Schultz(x > y, x = y, x < y, ...) is FALSE if \*(Na is involved.
2374e05ab77SRuslan Ermilov.Pp
2382a6bf1faSDavid SchultzNOTE: Trichotomy is violated by \*(Na.
2393a8617a8SJordan K. HubbardBesides being FALSE, predicates that entail ordered
2403a8617a8SJordan K. Hubbardcomparison, rather than mere (in)equality,
2412a6bf1faSDavid Schultzsignal Invalid Operation when \*(Na is involved.
2422a6bf1faSDavid Schultz.Ed
2434e05ab77SRuslan Ermilov.Pp
2443a8617a8SJordan K. HubbardRounding:
2454e05ab77SRuslan Ermilov.Bd -ragged -offset indent -compact
2463a8617a8SJordan K. HubbardEvery algebraic operation (+, \-, \(**, /,
2473a8617a8SJordan K. Hubbard\(sr)
2484e05ab77SRuslan Ermilovis rounded by default to within half an
2494e05ab77SRuslan Ermilov.Em ulp ,
2504e05ab77SRuslan Ermilovand when the rounding error is exactly half an
2514e05ab77SRuslan Ermilov.Em ulp
2524e05ab77SRuslan Ermilovthen
2533a8617a8SJordan K. Hubbardthe rounded value's least significant bit is zero.
2543a8617a8SJordan K. HubbardThis kind of rounding is usually the best kind,
2553a8617a8SJordan K. Hubbardsometimes provably so; for instance, for every
2563a8617a8SJordan K. Hubbardx = 1.0, 2.0, 3.0, 4.0, ..., 2.0**52, we find
2573a8617a8SJordan K. Hubbard(x/3.0)\(**3.0 == x and (x/10.0)\(**10.0 == x and ...
2583a8617a8SJordan K. Hubbarddespite that both the quotients and the products
2594e05ab77SRuslan Ermilovhave been rounded.
2604e05ab77SRuslan ErmilovOnly rounding like IEEE 754 can do that.
2614e05ab77SRuslan ErmilovBut no single kind of rounding can be
2623a8617a8SJordan K. Hubbardproved best for every circumstance, so IEEE 754
2633a8617a8SJordan K. Hubbardprovides rounding towards zero or towards
2645391441cSRuslan Ermilov+\*(If or towards \-\*(If
2654e05ab77SRuslan Ermilovat the programmer's option.
2664e05ab77SRuslan ErmilovAnd the
2673a8617a8SJordan K. Hubbardsame kinds of rounding are specified for
2684e05ab77SRuslan ErmilovBinary-Decimal Conversions, at least for magnitudes
2693a8617a8SJordan K. Hubbardbetween roughly 1.0e\-10 and 1.0e37.
2702a6bf1faSDavid Schultz.Ed
2714e05ab77SRuslan Ermilov.Pp
2723a8617a8SJordan K. HubbardExceptions:
2734e05ab77SRuslan Ermilov.Bd -ragged -offset indent -compact
2744e05ab77SRuslan ErmilovIEEE 754 recognizes five kinds of floating-point exceptions,
2753a8617a8SJordan K. Hubbardlisted below in declining order of probable importance.
2764e05ab77SRuslan Ermilov.Bl -column -offset indent "Invalid Operation" "Gradual Underflow"
2774e05ab77SRuslan Ermilov.Em "Exception	Default Result"
2782a6bf1faSDavid SchultzInvalid Operation	\*(Na, or FALSE
2795391441cSRuslan ErmilovOverflow	\(+-\*(If
2805391441cSRuslan ErmilovDivide by Zero	\(+-\*(If
2813a8617a8SJordan K. HubbardUnderflow	Gradual Underflow
2823a8617a8SJordan K. HubbardInexact	Rounded value
2834e05ab77SRuslan Ermilov.El
2844e05ab77SRuslan Ermilov.Pp
2853a8617a8SJordan K. HubbardNOTE: An Exception is not an Error unless handled
2864e05ab77SRuslan Ermilovbadly.
2874e05ab77SRuslan ErmilovWhat makes a class of exceptions exceptional
2883a8617a8SJordan K. Hubbardis that no single default response can be satisfactory
2894e05ab77SRuslan Ermilovin every instance.
2904e05ab77SRuslan ErmilovOn the other hand, if a default
2913a8617a8SJordan K. Hubbardresponse will serve most instances satisfactorily,
2923a8617a8SJordan K. Hubbardthe unsatisfactory instances cannot justify aborting
2933a8617a8SJordan K. Hubbardcomputation every time the exception occurs.
2942a6bf1faSDavid Schultz.Ed
2952a6bf1faSDavid Schultz.Pp
2964e05ab77SRuslan ErmilovFor each kind of floating-point exception, IEEE 754
2973a8617a8SJordan K. Hubbardprovides a Flag that is raised each time its exception
2983a8617a8SJordan K. Hubbardis signaled, and stays raised until the program resets
2994e05ab77SRuslan Ermilovit.
3004e05ab77SRuslan ErmilovPrograms may also test, save and restore a flag.
3013a8617a8SJordan K. HubbardThus, IEEE 754 provides three ways by which programs
3023a8617a8SJordan K. Hubbardmay cope with exceptions for which the default result
3033a8617a8SJordan K. Hubbardmight be unsatisfactory:
3042a6bf1faSDavid Schultz.Bl -enum
3052a6bf1faSDavid Schultz.It
3063a8617a8SJordan K. HubbardTest for a condition that might cause an exception
3073a8617a8SJordan K. Hubbardlater, and branch to avoid the exception.
3082a6bf1faSDavid Schultz.It
3093a8617a8SJordan K. HubbardTest a flag to see whether an exception has occurred
3103a8617a8SJordan K. Hubbardsince the program last reset its flag.
3112a6bf1faSDavid Schultz.It
3123a8617a8SJordan K. HubbardTest a result to see whether it is a value that only
3133a8617a8SJordan K. Hubbardan exception could have produced.
3144e05ab77SRuslan Ermilov.Pp
3153a8617a8SJordan K. HubbardCAUTION: The only reliable ways to discover
3163a8617a8SJordan K. Hubbardwhether Underflow has occurred are to test whether
3173a8617a8SJordan K. Hubbardproducts or quotients lie closer to zero than the
3183a8617a8SJordan K. Hubbardunderflow threshold, or to test the Underflow
3194e05ab77SRuslan Ermilovflag.
3204e05ab77SRuslan Ermilov(Sums and differences cannot underflow in
3214e05ab77SRuslan ErmilovIEEE 754; if x \(!= y then x\-y is correct to
3223a8617a8SJordan K. Hubbardfull precision and certainly nonzero regardless of
3234e05ab77SRuslan Ermilovhow tiny it may be.)
3244e05ab77SRuslan ErmilovProducts and quotients that
3253a8617a8SJordan K. Hubbardunderflow gradually can lose accuracy gradually
3263a8617a8SJordan K. Hubbardwithout vanishing, so comparing them with zero
3273a8617a8SJordan K. Hubbard(as one might on a VAX) will not reveal the loss.
3283a8617a8SJordan K. HubbardFortunately, if a gradually underflowed value is
3293a8617a8SJordan K. Hubbarddestined to be added to something bigger than the
3303a8617a8SJordan K. Hubbardunderflow threshold, as is almost always the case,
3313a8617a8SJordan K. Hubbarddigits lost to gradual underflow will not be missed
3323a8617a8SJordan K. Hubbardbecause they would have been rounded off anyway.
3334e05ab77SRuslan ErmilovSo gradual underflows are usually
3344e05ab77SRuslan Ermilov.Em provably
3354e05ab77SRuslan Ermilovignorable.
3363a8617a8SJordan K. HubbardThe same cannot be said of underflows flushed to 0.
3372a6bf1faSDavid Schultz.El
3382a6bf1faSDavid Schultz.Pp
3393a8617a8SJordan K. HubbardAt the option of an implementor conforming to IEEE 754,
3403a8617a8SJordan K. Hubbardother ways to cope with exceptions may be provided:
3414e05ab77SRuslan Ermilov.Bl -enum
3424e05ab77SRuslan Ermilov.It
3434e05ab77SRuslan ErmilovABORT.
3444e05ab77SRuslan ErmilovThis mechanism classifies an exception in
3453a8617a8SJordan K. Hubbardadvance as an incident to be handled by means
3464e05ab77SRuslan Ermilovtraditionally associated with error-handling
3474e05ab77SRuslan Ermilovstatements like "ON ERROR GO TO ...".
3484e05ab77SRuslan ErmilovDifferent
3493a8617a8SJordan K. Hubbardlanguages offer different forms of this statement,
3503a8617a8SJordan K. Hubbardbut most share the following characteristics:
3512a6bf1faSDavid Schultz.Bl -dash
3522a6bf1faSDavid Schultz.It
3533a8617a8SJordan K. HubbardNo means is provided to substitute a value for
3543a8617a8SJordan K. Hubbardthe offending operation's result and resume
3553a8617a8SJordan K. Hubbardcomputation from what may be the middle of an
3564e05ab77SRuslan Ermilovexpression.
3574e05ab77SRuslan ErmilovAn exceptional result is abandoned.
3582a6bf1faSDavid Schultz.It
3594e05ab77SRuslan ErmilovIn a subprogram that lacks an error-handling
3603a8617a8SJordan K. Hubbardstatement, an exception causes the subprogram to
3613a8617a8SJordan K. Hubbardabort within whatever program called it, and so
3623a8617a8SJordan K. Hubbardon back up the chain of calling subprograms until
3634e05ab77SRuslan Ermilovan error-handling statement is encountered or the
3643a8617a8SJordan K. Hubbardwhole task is aborted and memory is dumped.
3652a6bf1faSDavid Schultz.El
3664e05ab77SRuslan Ermilov.It
3674e05ab77SRuslan ErmilovSTOP.
3684e05ab77SRuslan ErmilovThis mechanism, requiring an interactive
3693a8617a8SJordan K. Hubbarddebugging environment, is more for the programmer
3704e05ab77SRuslan Ermilovthan the program.
3714e05ab77SRuslan ErmilovIt classifies an exception in
3723a8617a8SJordan K. Hubbardadvance as a symptom of a programmer's error; the
3733a8617a8SJordan K. Hubbardexception suspends execution as near as it can to
3743a8617a8SJordan K. Hubbardthe offending operation so that the programmer can
3754e05ab77SRuslan Ermilovlook around to see how it happened.
3764e05ab77SRuslan ErmilovQuite often
3773a8617a8SJordan K. Hubbardthe first several exceptions turn out to be quite
3783a8617a8SJordan K. Hubbardunexceptionable, so the programmer ought ideally
3793a8617a8SJordan K. Hubbardto be able to resume execution after each one as if
3803a8617a8SJordan K. Hubbardexecution had not been stopped.
3814e05ab77SRuslan Ermilov.It
3823a8617a8SJordan K. Hubbard\&... Other ways lie beyond the scope of this document.
3832a6bf1faSDavid Schultz.El
3842a6bf1faSDavid Schultz.Ed
3852a6bf1faSDavid Schultz.Pp
3862a6bf1faSDavid SchultzIdeally, each
3873a8617a8SJordan K. Hubbardelementary function should act as if it were indivisible, or
3883a8617a8SJordan K. Hubbardatomic, in the sense that ...
3894e05ab77SRuslan Ermilov.Bl -enum
3904e05ab77SRuslan Ermilov.It
3913a8617a8SJordan K. HubbardNo exception should be signaled that is not deserved by
3923a8617a8SJordan K. Hubbardthe data supplied to that function.
3934e05ab77SRuslan Ermilov.It
3943a8617a8SJordan K. HubbardAny exception signaled should be identified with that
3953a8617a8SJordan K. Hubbardfunction rather than with one of its subroutines.
3964e05ab77SRuslan Ermilov.It
3973a8617a8SJordan K. HubbardThe internal behavior of an atomic function should not
3983a8617a8SJordan K. Hubbardbe disrupted when a calling program changes from
3993a8617a8SJordan K. Hubbardone to another of the five or so ways of handling
4003a8617a8SJordan K. Hubbardexceptions listed above, although the definition
4013a8617a8SJordan K. Hubbardof the function may be correlated intentionally
4023a8617a8SJordan K. Hubbardwith exception handling.
4032a6bf1faSDavid Schultz.El
4042a6bf1faSDavid Schultz.Pp
4054e05ab77SRuslan ErmilovThe functions in
4064e05ab77SRuslan Ermilov.Nm libm
4074e05ab77SRuslan Ermilovare only approximately atomic.
4082a6bf1faSDavid SchultzThey signal no inappropriate exception except possibly ...
4094e05ab77SRuslan Ermilov.Bl -tag -width indent -offset indent -compact
4104e05ab77SRuslan Ermilov.It Xo
4113a8617a8SJordan K. HubbardOver/Underflow
4124e05ab77SRuslan Ermilov.Xc
4133a8617a8SJordan K. Hubbardwhen a result, if properly computed, might have lain barely within range, and
4144e05ab77SRuslan Ermilov.It Xo
4154e05ab77SRuslan ErmilovInexact in
4164e05ab77SRuslan Ermilov.Fn cabs ,
4174e05ab77SRuslan Ermilov.Fn cbrt ,
4184e05ab77SRuslan Ermilov.Fn hypot ,
4194e05ab77SRuslan Ermilov.Fn log10
4204e05ab77SRuslan Ermilovand
4214e05ab77SRuslan Ermilov.Fn pow
4224e05ab77SRuslan Ermilov.Xc
4233a8617a8SJordan K. Hubbardwhen it happens to be exact, thanks to fortuitous cancellation of errors.
4244e05ab77SRuslan Ermilov.El
4253a8617a8SJordan K. HubbardOtherwise, ...
4264e05ab77SRuslan Ermilov.Bl -tag -width indent -offset indent -compact
4274e05ab77SRuslan Ermilov.It Xo
4283a8617a8SJordan K. HubbardInvalid Operation is signaled only when
4294e05ab77SRuslan Ermilov.Xc
4302a6bf1faSDavid Schultzany result but \*(Na would probably be misleading.
4314e05ab77SRuslan Ermilov.It Xo
4323a8617a8SJordan K. HubbardOverflow is signaled only when
4334e05ab77SRuslan Ermilov.Xc
4343a8617a8SJordan K. Hubbardthe exact result would be finite but beyond the overflow threshold.
4354e05ab77SRuslan Ermilov.It Xo
4364e05ab77SRuslan ErmilovDivide-by-Zero is signaled only when
4374e05ab77SRuslan Ermilov.Xc
4383a8617a8SJordan K. Hubbarda function takes exactly infinite values at finite operands.
4394e05ab77SRuslan Ermilov.It Xo
4403a8617a8SJordan K. HubbardUnderflow is signaled only when
4414e05ab77SRuslan Ermilov.Xc
4423a8617a8SJordan K. Hubbardthe exact result would be nonzero but tinier than the underflow threshold.
4434e05ab77SRuslan Ermilov.It Xo
4443a8617a8SJordan K. HubbardInexact is signaled only when
4454e05ab77SRuslan Ermilov.Xc
4463a8617a8SJordan K. Hubbardgreater range or precision would be needed to represent the exact result.
4474e05ab77SRuslan Ermilov.El
4482a6bf1faSDavid Schultz.Sh SEE ALSO
4492a6bf1faSDavid Schultz.Xr fenv 3 ,
4502a6bf1faSDavid Schultz.Xr ieee 3
4512a6bf1faSDavid Schultz.Pp
4523a8617a8SJordan K. HubbardAn explanation of IEEE 754 and its proposed extension p854
4533a8617a8SJordan K. Hubbardwas published in the IEEE magazine MICRO in August 1984 under
4544e05ab77SRuslan Ermilovthe title "A Proposed Radix- and Word-length-independent
4552d82ac31SRuslan ErmilovStandard for Floating-point Arithmetic" by
4562d82ac31SRuslan Ermilov.An "W. J. Cody"
4572d82ac31SRuslan Ermilovet al.
4583a8617a8SJordan K. HubbardThe manuals for Pascal, C and BASIC on the Apple Macintosh
4593a8617a8SJordan K. Hubbarddocument the features of IEEE 754 pretty well.
4602d82ac31SRuslan ErmilovArticles in the IEEE magazine COMPUTER vol.\& 14 no.\& 3 (Mar.\&
4613a8617a8SJordan K. Hubbard1981), and in the ACM SIGNUM Newsletter Special Issue of
4622d82ac31SRuslan ErmilovOct.\& 1979, may be helpful although they pertain to
4633a8617a8SJordan K. Hubbardsuperseded drafts of the standard.
4642a6bf1faSDavid Schultz.Sh HISTORY
4652a6bf1faSDavid SchultzA math library with many of the present functions appeared in
4664e05ab77SRuslan Ermilov.At v7 .
4674e05ab77SRuslan ErmilovThe library was substantially rewritten for
4684e05ab77SRuslan Ermilov.Bx 4.3
4694e05ab77SRuslan Ermilovto provide
4702a6bf1faSDavid Schultzbetter accuracy and speed on machines supporting either VAX
4712a6bf1faSDavid Schultzor IEEE 754 floating-point.
4722a6bf1faSDavid SchultzMost of this library was replaced with FDLIBM, developed at Sun
4732a6bf1faSDavid SchultzMicrosystems, in
4742a6bf1faSDavid Schultz.Fx 1.1.5 .
47524a0682cSRuslan Ermilov.Sh BUGS
47624a0682cSRuslan ErmilovSeveral functions required by
47724a0682cSRuslan Ermilov.St -isoC-99
47824a0682cSRuslan Ermilovare missing, and many functions are not available in their
47924a0682cSRuslan Ermilov.Vt "long double"
48024a0682cSRuslan Ermilovvariants.
48124a0682cSRuslan Ermilov.Pp
48224a0682cSRuslan ErmilovOn some architectures, trigonometric argument reduction is not
48324a0682cSRuslan Ermilovperformed accurately, resulting in errors greater than 1
48424a0682cSRuslan Ermilov.Em ulp
48524a0682cSRuslan Ermilovfor large arguments to
48624a0682cSRuslan Ermilov.Fn cos ,
48724a0682cSRuslan Ermilov.Fn sin ,
48824a0682cSRuslan Ermilovand
48924a0682cSRuslan Ermilov.Fn tan .
490