date and time created 85/09/06 17:52:49 by zliu
[unix-history] / usr / src / lib / libm / common_source / math.3
CommitLineData
cd30a7d2 1.TH INTRO 3M "19 August 1985"
6a716ef9 2.UC 4
cd30a7d2
MAN
3.ds up \fIulp\fR
4.ds nn \fINaN\fR
5.de If
6.if n \\
7\\$1Infinity\\$2
8.if t \\
9\\$1\\(if\\$2
10..
fb3e39d8
KM
11.SH NAME
12intro \- introduction to mathematical library functions
13.SH DESCRIPTION
14These functions constitute the math library,
15.I libm.
16They are automatically loaded as needed by the Fortran compiler
17.IR f77 (1).
18The link editor searches this library under the \*(lq\-lm\*(rq option.
19Declarations for these functions may be obtained from the include file
20.RI < math.h >.
21.SH "LIST OF FUNCTIONS"
22.sp 2
23.nf
cd30a7d2
MAN
24.ta \w'copysign'u+2n +\w'infnan.3m'u+10n +\w'inverse trigonometric func'u
25\fIName\fP \fIAppears on Page\fP \fIDescription\fP \fIError Bound (ULPs)\fP
26.ta \w'copysign'u+4n +\w'infnan.3m'u+4n +\w'inverse trigonometric function'u+6nC
fb3e39d8 27.sp 5p
cd30a7d2
MAN
28acos sin.3m inverse trigonometric function 3
29acosh asinh.3m inverse hyperbolic function 3
30asin sin.3m inverse trigonometric function 3
31asinh asinh.3m inverse hyperbolic function 3
32atan sin.3m inverse trigonometric function 1
33atanh asinh.3m inverse hyperbolic function 3
34atan2 sin.3m inverse trigonometric function 2
35cabs hypot.3m complex absolute value 1
36cbrt sqrt.3m cube root 1
37ceil floor.3m integer no less than 0
38copysign ieee.3m copy sign bit 0
39cos sin.3m trigonometric function 1
40cosh sinh.3m hyperbolic function 3
41drem ieee.3m remainder 0
42erf erf.3m error function ???
43erfc erf.3m complementary error function ???
44exp exp.3m exponential 1
45expm1 exp.3m exp(x)\-1 1
46fabs floor.3m absolute value 0
47floor floor.3m integer no greater than 0
afdf7bc1 48gamma gamma.3m log gamma function
cd30a7d2
MAN
49hypot hypot.3m Euclidean distance 1
50infnan infnan.3m signals exceptions
51j0 j0.3m bessel function ???
52j1 j0.3m bessel function ???
53jn j0.3m bessel function ???
cd30a7d2
MAN
54log exp.3m natural logarithm 1
55logb ieee.3m exponent extraction 0
56log10 exp.3m logarithm to base 10 3
57log1p exp.3m log(1+x) 1
58pow exp.3m exponential x**y 60\-500
59scalb ieee.3m exponent adjustment 0
60sin sin.3m trigonometric function 1
61sinh sinh.3m hyperbolic function 3
62sqrt sqrt.3m square root 1
63tan sin.3m trigonometric function 3
64tanh sinh.3m hyperbolic function 3
65y0 j0.3m bessel function ???
66y1 j0.3m bessel function ???
67yn j0.3m bessel function ???
68.ta
fb3e39d8 69.fi
cd30a7d2
MAN
70.SH NOTES
71In 4.3 BSD, distributed from the University of California
72in late 1985, most of the foregoing functions come in two
73versions, one for the double\-precision "D" format in the
74DEC VAX\-11 family of computers, another for double\-precision
75arithmetic conforming to the IEEE Standard 754 for Binary
76Floating\-Point Arithmetic. The two versions behave very
77similarly, as should be expected from programs more accurate
78and robust than was the norm when UNIX was born. For
79instance, the programs are accurate to within the numbers
80of \*(ups tabulated above; an \*(up is one \fIU\fRnit in the \fIL\fRast
81\fIP\fRlace. And the programs have been cured of anomalies that
82afflicted the older math library \fIlibm\fR in which incidents like
83the following had been reported:
84.RS
85sqrt(\-1.0) = 0.0 and log(\-1.0) = \-1.7e38.
86.br
87cos(1.0e\-11) > cos(0.0) > 1.0.
88.br
89pow(x,1.0)
90.if n \
91!=
92.if t \
93\(!=
94x when x = 2.0, 3.0, 4.0, ..., 9.0.
95.br
96pow(\-1.0,1.0e10) trapped on Integer Overflow.
97.br
98sqrt(1.0e30) and sqrt(1.0e\-30) were very slow.
99.RE
100However the two versions do differ in ways that have to be
101explained, to which end the following notes are provided.
102.PP
103\fBDEC VAX\-11 D_floating\-point:\fR
104.PP
105This is the format for which the original math library \fIlibm\fR
106was developed, and to which this manual is still principally
107dedicated. It is \fIthe\fR double\-precision format for the PDP\-11
108and the earlier VAX\-11 machines; VAX\-11s after 1983 were
109provided with an optional "G" format closer to the IEEE
110double\-precision format. The earlier DEC MicroVAXs have no
111D format, only G double\-precision. (Why? Why not?)
112.PP
113Properties of D_floating\-point:
114.RS
115Wordsize: 64 bits, 8 bytes. Radix: Binary.
116.br
117Precision: 56 bits; equivalent to about 17 significant decimals.
118.RS
119If x and x' are consecutive positive D_floating\-point
120numbers (they differ by 1 \*(up), then
121.br
1221.3e\-17 < 0.5**56 < (x'\-x)/x \(<= 0.5**55 < 2.8e\-17.
123.RE
124.nf
125.ta \w'Range:'u+1n +\w'Underflow threshold'u+1n +\w'= 2.0**127'u+1n
126Range: Overflow threshold = 2.0**127 = 1.7e38.
127 Underflow threshold = 0.5**128 = 2.9e\-39.
128 NOTE: THIS RANGE IS COMPARATIVELY NARROW.
129.ta
130.fi
131.RS
132Overflow customarily stops computation.
133.br
134Underflow is customarily flushed quietly to zero.
135.br
136CAUTION:
137.RS
138It is possible to have x
139.if n \
140!=
141.if t \
142\(!=
143y and yet
144x\-y = 0 because of underflow. Similarly
145x > y > 0 cannot prevent either x\(**y = 0
146or y/x = 0 from happening without warning.
147.RE
148.RE
149Zero is represented ambiguously.
150.RS
151Although 2**55 different representations of zero are accepted by
152the hardware, only the obvious representation is ever produced.
153There is no \-0 on a VAX.
154.RE
155.If
156is not part of the VAX architecture.
157.br
158Reserved operands:
159.RS
160of the 2**55 that the hardware
161recognizes, only one of them is ever produced.
162Any floating\-point operation upon a reserved
163operand, even a MOVF or MOVD, customarily stops
164computation, so they are not much used.
165.RE
166Exceptions:
167.RS
168Divisions by zero and operations that
169overflow are invalid operations that customarily
170stop computation or, in earlier machines, produce
171reserved operands that will stop computation.
172.RE
173Rounding:
174.RS
175Every rational operation (+, \-, \(**, /) on a
176VAX (but not necessarily on a PDP\-11), if not an
177over/underflow nor division by zero, is rounded to
178within half an \*(up, and when the rounding error is
179exactly half an \*(up then rounding is away from 0.
180.RE
181.RE
182.PP
183Except for its narrow range, D_floating\-point is one of the
184better computer arithmetics designed in the 1960's.
185Its properties are reflected fairly faithfully in the elementary
186functions for a VAX distributed in 4.3 BSD.
187They over/underflow only if their results have to lie out of range
188or very nearly so, and then they behave much as any rational
189arithmetic operation that over/underflowed would behave.
190Similarly, expressions like log(0) and atanh(1) behave
191like 1/0; and sqrt(\-3) and acos(3) behave like 0/0;
192they all produce reserved operands and/or stop computation!
193The situation is described in more detail in manual pages.
194.RS
195.ll -0.5i
196\fIThis response seems excessively punitive, so it is destined
197to be replaced at some time in the foreseeable future by a
198more flexible but still uniform scheme being developed to
199handle all floating\-point arithmetic exceptions neatly.
200See infnan(3M) for the present state of affairs.\fR
201.ll +0.5i
202.RE
203.PP
204How do the functions in 4.3 BSD's new \fIlibm\fR for UNIX
205compare with their counterparts in DEC's VAX/VMS library?
206Some of the VMS functions are a little faster, some are
207a little more accurate, some are more puritanical about
208exceptions (like pow(0.0,0.0) and atan2(0.0,0.0)),
209and most occupy much more memory than their counterparts in
210\fIlibm\fR.
211The VMS codes interpolate in large table to achieve
212speed and accuracy; the \fIlibm\fR codes use tricky formulas
213compact enough that all of them may some day fit into a ROM.
214.PP
215More important, DEC regards the VMS codes as proprietary
216and guards them zealously against unauthorized use. But the
217\fIlibm\fR codes in 4.3 BSD are intended for the public domain;
218they may be copied freely provided their provenance is always
219acknowledged, and provided users assist the authors in their
220researches by reporting experience with the codes.
221Therefore no user of UNIX on a machine whose arithmetic resembles
222VAX D_floating\-point need use anything worse than the new \fIlibm\fR.
223.PP
224\fBIEEE STANDARD 754 Floating\-Point Arithmetic:\fR
225.PP
226This standard is on its way to becoming more widely adopted
227than any other design for computer arithmetic.
228VLSI chips that conform to some version of that standard have been
229produced by a host of manufacturers, among them ...
230.nf
231.ta 0.5i +\w'Intel i8070, i80287'u+6n
232 Intel i8087, i80287 National Semiconductor 32081
233 Motorola 68881 Weitek WTL-1032, ... , -1065
234 Zilog Z8070 Western Electric (AT&T) 32106.
235.ta
236.fi
237Other implementations range from software, done thoroughly
238in the Apple Macintosh, through VLSI in the Hewlett\-Packard
2399000 series, to the ELXSI 6400 running at 3 Megaflops.
240Several other companies have adopted the formats
241of IEEE 754 without, alas, adhering to the standard's way
242of handling rounding and exceptions like over/underflow.
243The DEC VAX G_floating\-point format is very similar to the IEEE
244754 Double format, so similar that the C programs for the
245IEEE versions of most of the elementary functions listed
246above could easily be converted to run on a MicroVAX, though
247nobody has volunteered to do that yet.
248.PP
249The codes in 4.3 BSD's \fIlibm\fR for machines that conform to
250IEEE 754 are intended primarily for the National Semi. 32081
251and Weitek 1065. To use these codes with the Intel or Zilog
252chips, or with the Apple Macintosh or ELXSI 6400, is to
253forego the use of better codes available (for a price) from
254those companies and designed by some of the authors of the
255codes above. The Motorola 68881 has all the \fIelementary\fR
256functions above except \fIpow\fR implemented on chip already,
257and faster and more accurate. The main virtue of 4.3 BSD's
258\fIlibm\fR codes is that they are intended for the public domain;
259they may be copied freely provided their provenance is always
260acknowledged, and provided users assist the authors in their
261researches by reporting experience with the codes.
262Therefore no user of UNIX on a machine that conforms to
263IEEE 754 need use anything worse than the new \fIlibm\fR.
264.PP
265Properties of IEEE 754 Double\-Precision:
266.RS
267Wordsize: 64 bits, 8 bytes. Radix: Binary.
268.br
269Precision: 53 bits; equivalent to about 16 significant decimals.
270.RS
271If x and x' are consecutive positive Double\-Precision
272numbers (they differ by 1 \*(up), then
273.br
2741.1e\-16 < 0.5**53 < (x'\-x)/x \(<= 0.5**52 < 2.3e\-16.
275.RE
276.nf
277.ta \w'Range:'u+1n +\w'Underflow threshold'u+1n +\w'= 2.0**1024'u+1n
278Range: Overflow threshold = 2.0**1024 = 1.8e308
279 Underflow threshold = 0.5**1022 = 2.2e\-308
280.ta
281.fi
282.RS
283Overflow goes by default to a signed
284.If "" .
285.br
286Underflow is \fIGradual,\fR rounding to the nearest
287integer multiple of 0.5**1074 = 4.9e\-324.
288.RE
289Zero is represented ambiguously as +0 or \-0.
290.RS
291Its sign transforms correctly through multiplication or
292division, and is preserved by addition of zeros
293with like signs; but x\-x yields +0 for every
294finite x. The only operations that reveal zero's
295sign are division by zero and copysign(x,\(+-0).
296In particular, comparison (x > y, x \(>= y, etc.)
297cannot be affected by the sign of zero; but if
298finite x = y then
299.If
300\&= 1/(x\-y)
301.if n \
302!=
303.if t \
304\(!=
305\-1/(y\-x) =
306.If \- .
307.RE
308.If
309is signed.
310.RS
311it persists after addition of itself
312or of any finite number. Its sign transforms
313correctly through multiplication and division, and
314.If (finite)/\(+- \0=\0\(+-0
315(nonzero)/0 =
316.If \(+- .
317But
318.if n \
319Infinity\-Infinity, Infinity\(**0 and Infinity/Infinity
320.if t \
321\(if\-\(if, \(if\(**0 and \(if/\(if
322are, like 0/0 and sqrt(\-3),
323invalid operations that produce \*(nn. ...
324.RE
325Reserved operands:
326.RS
327there are 2**53\-2 of them, all
328called \*(nn (\fIN\fRot \fIa N\fRumber).
329Some, called Signaling \*(nns, trap any floating\-point operation
330performed upon them; they are used to mark missing
331or uninitialized values, or nonexistent elements
332of arrays. The rest are Quiet \*(nns; they are
333the default results of Invalid Operations, and
334propagate through subsequent arithmetic operations.
335If x
336.if n \
337!=
338.if t \
339\(!=
340x then x is \*(nn; every other predicate
341(x > y, x = y, x < y, ...) is FALSE if \*(nn is involved.
342.br
343NOTE: Trichotomy is violated by \*(nn.
344.RS
345Besides being FALSE, predicates that entail ordered
346comparison, rather than mere (in)equality,
347signal Invalid Operation when \*(nn is involved.
348.RE
349.RE
350Rounding:
351.RS
352Every algebraic operation (+, \-, \(**, /,
353.if n \
354sqrt)
355.if t \
356\(sr)
357is rounded by default to within half an \*(up, and
358when the rounding error is exactly half an \*(up then
359the rounded value's least significant bit is zero.
360This kind of rounding is usually the best kind,
361sometimes provably so; for instance, for every
362x = 1.0, 2.0, 3.0, 4.0, ..., 2.0**52, we find
363(x/3.0)\(**3.0 == x and (x/10.0)\(**10.0 == x and ...
364despite that both the quotients and the products
365have been rounded. Only rounding like IEEE 754
366can do that. But no single kind of rounding can be
367proved best for every circumstance, so IEEE 754
368provides rounding towards zero or towards
369.If +
370or towards
371.If \-
372at the programmer's option. And the
373same kinds of rounding are specified for
374Binary\-Decimal Conversions, at least for magnitudes
375between roughly 1.0e\-10 and 1.0e37.
376.RE
377Exceptions:
378.RS
379IEEE 754 recognizes five kinds of floating\-point exceptions,
380listed below in declining order of probable importance.
381.RS
382.nf
383.ta \w'Invalid Operation'u+6n +\w'Gradual Underflow'u+2n
384Exception Default Result
385.tc \(ru
386
387.tc
388Invalid Operation \*(nn, or FALSE
389.if n \{\
390Overflow \(+-Infinity
391Divide by Zero \(+-Infinity \}
392.if t \{\
393Overflow \(+-\(if
394Divide by Zero \(+-\(if \}
395Underflow Gradual Underflow
396Inexact Rounded value
397.ta
398.fi
399.RE
400NOTE: An Exception is not an Error unless handled
401badly. What makes a class of exceptions exceptional
402is that no single default response can be satisfactory
403in every instance. On the other hand, if a default
404response will serve most instances satisfactorily,
405the unsatisfactory instances cannot justify aborting
406computation every time the exception occurs.
407.RE
408.PP
409For each kind of floating\-point exception, IEEE 754
410provides a Flag that is raised each time its exception
411is signaled, and stays raised until the program resets
412it. Programs may also test, save and restore a flag.
413Thus, IEEE 754 provides three ways by which programs
414may cope with exceptions for which the default result
415might be unsatisfactory:
416.IP 1) \w'\0\0\0\0'u
417Test for a condition that might cause an exception
418later, and branch to avoid the exception.
419.IP 2) \w'\0\0\0\0'u
420Test a flag to see whether an exception has occurred
421since the program last reset its flag.
422.IP 3) \w'\0\0\0\0'u
423Test a result to see whether it is a value that only
424an exception could have produced.
425.RS
426CAUTION: The only reliable ways to discover
427whether Underflow has occurred are to test whether
428products or quotients lie closer to zero than the
429underflow threshold, or to test the Underflow
430flag. (Sums and differences cannot underflow in
431IEEE 754; if x
432.if n \
433!=
434.if t \
435\(!=
436y then x\-y is correct to
437full precision and certainly nonzero regardless of
438how tiny it may be.) Products and quotients that
439underflow gradually can lose accuracy gradually
440without vanishing, so comparing them with zero
441(as one might on a VAX) will not reveal the loss.
442Fortunately, if a gradually underflowed value is
443destined to be added to something bigger than the
444underflow threshold, as is almost always the case,
445digits lost to gradual underflow will not be missed
446because they would have been rounded off anyway.
447So gradual underflows are usually \fIprovably\fR ignorable.
448The same cannot be said of underflows flushed to 0.
449.RE
450.PP
451At the option of an implementor conforming to IEEE 754,
452other ways to cope with exceptions may be provided:
453.IP 4) \w'\0\0\0\0'u
454ABORT. This mechanism classifies an exception in
455advance as an incident to be handled by means
456traditionally associated with error\-handling
457statements like "ON ERROR GO TO ...". Different
458languages offer different forms of this statement,
459but most share the following characteristics:
460.IP \(em \w'\0\0\0\0'u
461No means is provided to substitute a value for
462the offending operation's result and resume
463computation from what may be the middle of an
464expression. An exceptional result is abandoned.
465.IP \(em \w'\0\0\0\0'u
466In a subprogram that lacks an error\-handling
467statement, an exception causes the subprogram to
468abort within whatever program called it, and so
469on back up the chain of calling subprograms until
470an error\-handling statement is encountered or the
471whole task is aborted and memory is dumped.
472.IP 5) \w'\0\0\0\0'u
473STOP. This mechanism, requiring an interactive
474debugging environment, is more for the programmer
475than the program. It classifies an exception in
476advance as a symptom of a programmer's error; the
477exception suspends execution as near as it can to
478the offending operation so that the programmer can
479look around to see how it happened. Quite often
480the first several exceptions turn out to be quite
481unexceptionable, so the programmer ought ideally
482to be able to resume execution after each one as if
483execution had not been stopped.
484.IP 6) \w'\0\0\0\0'u
485\&... Other ways lie beyond the scope of this document.
486.RE
487.PP
488The crucial problem for exception handling is the problem of
489Scope, and the problem's solution is understood, but not
490enough manpower was available to implement it fully in time
491to be distributed in 4.3 BSD's \fIlibm\fR. Ideally, each
492elementary function should act as if it were indivisible, or
493atomic, in the sense that ...
494.IP i) \w'iii)'u+2n
495No exception should be signaled that is not deserved by
496the data supplied to that function.
497.IP ii) \w'iii)'u+2n
498Any exception signaled should be identified with that
499function rather than with one of its subroutines.
500.IP iii) \w'iii)'u+2n
501The internal behavior of an atomic function cannot be
502disrupted when a calling program changes from one
503to another of the five or so ways of handling
504exceptions listed above, although the definition
505of the function may be correlated intentionally
506with exception handling.
507.PP
508Ideally, every programmer should be able \fIconveniently\fR to
509turn a debugged subprogram into one that appears atomic to
510its users. But simulating all three characteristics of an
511atomic function is still a tedious affair, entailing hosts
512of tests and saves\-restores; work is under way to ameliorate
513the inconvenience.
514.PP
515Meanwhile, the functions in \fIlibm\fR are only approximately
516atomic. They signal no inappropriate exception except
517possibly ...
518.RS
519Over/Underflow
520.RS
521when a result, if properly computed, might have lain barely within range, and
522.RE
523Inexact in pow(x,y)
524.RS
525when it happens to be exact thanks to fortuitous cancellation of errors.
526.RE
527.RE
528Otherwise, ...
529.RS
530Invalid Operation is signaled only when
531.RS
532any result but \*(nn would probably be misleading.
533.RE
534Overflow is signaled only when
535.RS
536the exact result would be finite but beyond the overflow threshold.
537.RE
538Divide\-by\-Zero is signaled only when
539.RS
540a function takes exactly infinite values at finite operands.
541.RE
542Underflow is signaled only when
543.RS
544the exact result would be nonzero but tinier than the underflow threshold.
545.RE
546Inexact is signaled only when
547.RS
548greater range or precision would be needed to represent the exact result.
549.RE
550.RE
551.SH BUGS
552When signals are appropriate, they are emitted by certain
553operations within the codes, so a subroutine\-trace may be
554needed to identify the function with its signal in case
555method 6) above is in use. And the codes all take the
556IEEE 754 defaults for granted; this means that a decision to
557trap all divisions by zero could disrupt a code that would
558otherwise get correct results despite division by zero.
559.SH SEE ALSO
560An explanation of IEEE 754 and its proposed extension p854
561was published in the IEEE magazine MICRO in August 1984 under
562the title "A Proposed Radix\- and Word\-length\-independent
563Standard for Floating\-point Arithmetic" by W. J. Cody et al.
564The manuals for Pascal, C and BASIC on the Apple Macintosh
565document the features of IEEE 754 pretty well.
566Articles in the IEEE magazine COMPUTER vol. 14 no. 3 (Mar.
5671981), and in the ACM SIGNUM Newsletter Special Issue of
568Oct. 1979, may be helpful although they pertain to
569superseded drafts of the standard.
570.SH AUTHOR
571W. Kahan, with the help of Alex Z\-S. Liu, Stuart I. McDonald,
572Dr. Kwok\-Choi Ng, Peter Tang.