1MATH(3M) MATH(3M)
2
3
4
6 math - introduction to mathematical library functions
7
9 These functions constitute the C math library, libm. The link editor
10 searches this library under the “-lm” option. Declarations for these
11 functions may be obtained from the include file <math.h>. The Fortran
12 math library is described in ``man 3f intro''.
13
15 Name Appears on Page Description Error Bound (ULPs)
16 acos sin.3m inverse trigonometric function 3
17 acosh asinh.3m inverse hyperbolic function 3
18 asin sin.3m inverse trigonometric function 3
19 asinh asinh.3m inverse hyperbolic function 3
20 atan sin.3m inverse trigonometric function 1
21 atanh asinh.3m inverse hyperbolic function 3
22 atan2 sin.3m inverse trigonometric function 2
23 cabs hypot.3m complex absolute value 1
24 cbrt sqrt.3m cube root 1
25 ceil floor.3m integer no less than 0
26 copysign ieee.3m copy sign bit 0
27 cos sin.3m trigonometric function 1
28 cosh sinh.3m hyperbolic function 3
29 drem ieee.3m remainder 0
30 erf erf.3m error function ???
31 erfc erf.3m complementary error function ???
32 exp exp.3m exponential 1
33 expm1 exp.3m exp(x)-1 1
34 fabs floor.3m absolute value 0
35 floor floor.3m integer no greater than 0
36 hypot hypot.3m Euclidean distance 1
37 infnan infnan.3m signals exceptions
38 j0 j0.3m bessel function ???
39 j1 j0.3m bessel function ???
40 jn j0.3m bessel function ???
41 lgamma lgamma.3m log gamma function; (formerly gamma.3m)
42 log exp.3m natural logarithm 1
43 logb ieee.3m exponent extraction 0
44 log10 exp.3m logarithm to base 10 3
45 log1p exp.3m log(1+x) 1
46 pow exp.3m exponential x**y 60-500
47 rint floor.3m round to nearest integer 0
48 scalb ieee.3m exponent adjustment 0
49 sin sin.3m trigonometric function 1
50 sinh sinh.3m hyperbolic function 3
51 sqrt sqrt.3m square root 1
52 tan sin.3m trigonometric function 3
53 tanh sinh.3m hyperbolic function 3
54 y0 j0.3m bessel function ???
55 y1 j0.3m bessel function ???
56 yn j0.3m bessel function ???
57
59 In 4.3 BSD, distributed from the University of California in late 1985,
60 most of the foregoing functions come in two versions, one for the dou‐
61 ble-precision "D" format in the DEC VAX-11 family of computers, another
62 for double-precision arithmetic conforming to the IEEE Standard 754 for
63 Binary Floating-Point Arithmetic. The two versions behave very simi‐
64 larly, as should be expected from programs more accurate and robust
65 than was the norm when UNIX was born. For instance, the programs are
66 accurate to within the numbers of ulps tabulated above; an ulp is one
67 Unit in the Last Place. And the programs have been cured of anomalies
68 that afflicted the older math library libm in which incidents like the
69 following had been reported:
70 sqrt(-1.0) = 0.0 and log(-1.0) = -1.7e38.
71 cos(1.0e-11) > cos(0.0) > 1.0.
72 pow(x,1.0) != x when x = 2.0, 3.0, 4.0, ..., 9.0.
73 pow(-1.0,1.0e10) trapped on Integer Overflow.
74 sqrt(1.0e30) and sqrt(1.0e-30) were very slow.
75 However the two versions do differ in ways that have to be explained,
76 to which end the following notes are provided.
77
78 DEC VAX-11 D_floating-point:
79
80 This is the format for which the original math library libm was devel‐
81 oped, and to which this manual is still principally dedicated. It is
82 the double-precision format for the PDP-11 and the earlier VAX-11
83 machines; VAX-11s after 1983 were provided with an optional "G" format
84 closer to the IEEE double-precision format. The earlier DEC MicroVAXs
85 have no D format, only G double-precision. (Why? Why not?)
86
87 Properties of D_floating-point:
88 Wordsize: 64 bits, 8 bytes. Radix: Binary.
89 Precision: 56 sig. bits, roughly like 17 sig. decimals.
90 If x and x' are consecutive positive D_floating-point
91 numbers (they differ by 1 ulp), then
92 1.3e-17 < 0.5**56 < (x'-x)/x ≤ 0.5**55 < 2.8e-17.
93 Range: Overflow threshold = 2.0**127 = 1.7e38.
94 Underflow threshold = 0.5**128 = 2.9e-39.
95 NOTE: THIS RANGE IS COMPARATIVELY NARROW.
96 Overflow customarily stops computation.
97 Underflow is customarily flushed quietly to zero.
98 CAUTION:
99 It is possible to have x != y and yet x-y = 0
100 because of underflow. Similarly x > y > 0 cannot
101 prevent either x∗y = 0 or y/x = 0 from happening
102 without warning.
103 Zero is represented ambiguously.
104 Although 2**55 different representations of zero are
105 accepted by the hardware, only the obvious representation
106 is ever produced. There is no -0 on a VAX.
107 Infinity is not part of the VAX architecture.
108 Reserved operands:
109 of the 2**55 that the hardware recognizes, only one of
110 them is ever produced. Any floating-point operation upon
111 a reserved operand, even a MOVF or MOVD, customarily
112 stops computation, so they are not much used.
113 Exceptions:
114 Divisions by zero and operations that overflow are
115 invalid operations that customarily stop computation or,
116 in earlier machines, produce reserved operands that will
117 stop computation.
118 Rounding:
119 Every rational operation (+, -, ∗, /) on a VAX (but not
120 necessarily on a PDP-11), if not an over/underflow nor
121 division by zero, is rounded to within half an ulp, and
122 when the rounding error is exactly half an ulp then
123 rounding is away from 0.
124
125 Except for its narrow range, D_floating-point is one of the better com‐
126 puter arithmetics designed in the 1960's. Its properties are reflected
127 fairly faithfully in the elementary functions for a VAX distributed in
128 4.3 BSD. They over/underflow only if their results have to lie out of
129 range or very nearly so, and then they behave much as any rational
130 arithmetic operation that over/underflowed would behave. Similarly,
131 expressions like log(0) and atanh(1) behave like 1/0; and sqrt(-3) and
132 acos(3) behave like 0/0; they all produce reserved operands and/or stop
133 computation! The situation is described in more detail in manual
134 pages.
135 This response seems excessively punitive, so it is destined
136 to be replaced at some time in the foreseeable future by a
137 more flexible but still uniform scheme being developed to
138 handle all floating-point arithmetic exceptions neatly.
139 See infnan(3M) for the present state of affairs.
140
141 How do the functions in 4.3 BSD's new libm for UNIX compare with their
142 counterparts in DEC's VAX/VMS library? Some of the VMS functions are a
143 little faster, some are a little more accurate, some are more puritani‐
144 cal about exceptions (like pow(0.0,0.0) and atan2(0.0,0.0)), and most
145 occupy much more memory than their counterparts in libm. The VMS codes
146 interpolate in large table to achieve speed and accuracy; the libm
147 codes use tricky formulas compact enough that all of them may some day
148 fit into a ROM.
149
150 More important, DEC regards the VMS codes as proprietary and guards
151 them zealously against unauthorized use. But the libm codes in 4.3 BSD
152 are intended for the public domain; they may be copied freely provided
153 their provenance is always acknowledged, and provided users assist the
154 authors in their researches by reporting experience with the codes.
155 Therefore no user of UNIX on a machine whose arithmetic resembles VAX
156 D_floating-point need use anything worse than the new libm.
157
158 IEEE STANDARD 754 Floating-Point Arithmetic:
159
160 This standard is on its way to becoming more widely adopted than any
161 other design for computer arithmetic. VLSI chips that conform to some
162 version of that standard have been produced by a host of manufacturers,
163 among them ...
164 Intel i8087, i80287 National Semiconductor 32081
165 Motorola 68881 Weitek WTL-1032, ... , -1165
166 Zilog Z8070 Western Electric (AT&T) WE32106.
167 Other implementations range from software, done thoroughly in the Apple
168 Macintosh, through VLSI in the Hewlett-Packard 9000 series, to the
169 ELXSI 6400 running ECL at 3 Megaflops. Several other companies have
170 adopted the formats of IEEE 754 without, alas, adhering to the stan‐
171 dard's way of handling rounding and exceptions like over/underflow.
172 The DEC VAX G_floating-point format is very similar to the IEEE 754
173 Double format, so similar that the C programs for the IEEE versions of
174 most of the elementary functions listed above could easily be converted
175 to run on a MicroVAX, though nobody has volunteered to do that yet.
176
177 The codes in 4.3 BSD's libm for machines that conform to IEEE 754 are
178 intended primarily for the National Semi. 32081 and WTL 1164/65. To
179 use these codes with the Intel or Zilog chips, or with the Apple Macin‐
180 tosh or ELXSI 6400, is to forego the use of better codes provided (per‐
181 haps freely) by those companies and designed by some of the authors of
182 the codes above. Except for atan, cabs, cbrt, erf, erfc, hypot, j0-jn,
183 lgamma, pow and y0-yn, the Motorola 68881 has all the functions in libm
184 on chip, and faster and more accurate; it, Apple, the i8087, Z8070 and
185 WE32106 all use 64 sig. bits. The main virtue of 4.3 BSD's libm codes
186 is that they are intended for the public domain; they may be copied
187 freely provided their provenance is always acknowledged, and provided
188 users assist the authors in their researches by reporting experience
189 with the codes. Therefore no user of UNIX on a machine that conforms
190 to IEEE 754 need use anything worse than the new libm.
191
192 Properties of IEEE 754 Double-Precision:
193 Wordsize: 64 bits, 8 bytes. Radix: Binary.
194 Precision: 53 sig. bits, roughly like 16 sig. decimals.
195 If x and x' are consecutive positive Double-Precision
196 numbers (they differ by 1 ulp), then
197 1.1e-16 < 0.5**53 < (x'-x)/x ≤ 0.5**52 < 2.3e-16.
198 Range: Overflow threshold = 2.0**1024 = 1.8e308
199 Underflow threshold = 0.5**1022 = 2.2e-308
200 Overflow goes by default to a signed Infinity.
201 Underflow is Gradual, rounding to the nearest integer
202 multiple of 0.5**1074 = 4.9e-324.
203 Zero is represented ambiguously as +0 or -0.
204 Its sign transforms correctly through multiplication or
205 division, and is preserved by addition of zeros with like
206 signs; but x-x yields +0 for every finite x. The only
207 operations that reveal zero's sign are division by zero
208 and copysign(x,±0). In particular, comparison (x > y, x
209 ≥ y, etc.) cannot be affected by the sign of zero; but
210 if finite x = y then Infinity = 1/(x-y) != -1/(y-x) =
211 -Infinity.
212 Infinity is signed.
213 it persists when added to itself or to any finite number.
214 Its sign transforms correctly through multiplication and
215 division, and (finite)/±Infinity = ±0 (nonzero)/0 =
216 ±Infinity. But Infinity-Infinity, Infinity∗0 and Infin‐
217 ity/Infinity are, like 0/0 and sqrt(-3), invalid opera‐
218 tions that produce NaN. ...
219 Reserved operands:
220 there are 2**53-2 of them, all called NaN (Not a Number).
221 Some, called Signaling NaNs, trap any floating-point
222 operation performed upon them; they are used to mark
223 missing or uninitialized values, or nonexistent elements
224 of arrays. The rest are Quiet NaNs; they are the default
225 results of Invalid Operations, and propagate through sub‐
226 sequent arithmetic operations. If x != x then x is NaN;
227 every other predicate (x > y, x = y, x < y, ...) is FALSE
228 if NaN is involved.
229 NOTE: Trichotomy is violated by NaN.
230 Besides being FALSE, predicates that entail
231 ordered comparison, rather than mere (in)equality,
232 signal Invalid Operation when NaN is involved.
233 Rounding:
234 Every algebraic operation (+, -, ∗, /, sqrt) is rounded
235 by default to within half an ulp, and when the rounding
236 error is exactly half an ulp then the rounded value's
237 least significant bit is zero. This kind of rounding is
238 usually the best kind, sometimes provably so; for
239 instance, for every x = 1.0, 2.0, 3.0, 4.0, ..., 2.0**52,
240 we find (x/3.0)∗3.0 == x and (x/10.0)∗10.0 == x and ...
241 despite that both the quotients and the products have
242 been rounded. Only rounding like IEEE 754 can do that.
243 But no single kind of rounding can be proved best for
244 every circumstance, so IEEE 754 provides rounding towards
245 zero or towards +Infinity or towards -Infinity at the
246 programmer's option. And the same kinds of rounding are
247 specified for Binary-Decimal Conversions, at least for
248 magnitudes between roughly 1.0e-10 and 1.0e37.
249 Exceptions:
250 IEEE 754 recognizes five kinds of floating-point excep‐
251 tions, listed below in declining order of probable impor‐
252 tance.
253 Exception Default Result
254 __________________________________________
255 Invalid Operation NaN, or FALSE
256 Overflow ±Infinity
257 Divide by Zero ±Infinity
258 Underflow Gradual Underflow
259 Inexact Rounded value
260 NOTE: An Exception is not an Error unless handled badly.
261 What makes a class of exceptions exceptional is that no
262 single default response can be satisfactory in every
263 instance. On the other hand, if a default response will
264 serve most instances satisfactorily, the unsatisfactory
265 instances cannot justify aborting computation every time
266 the exception occurs.
267
268 For each kind of floating-point exception, IEEE 754 provides a
269 Flag that is raised each time its exception is signaled, and
270 stays raised until the program resets it. Programs may also
271 test, save and restore a flag. Thus, IEEE 754 provides three
272 ways by which programs may cope with exceptions for which the
273 default result might be unsatisfactory:
274
275 1) Test for a condition that might cause an exception later,
276 and branch to avoid the exception.
277
278 2) Test a flag to see whether an exception has occurred since
279 the program last reset its flag.
280
281 3) Test a result to see whether it is a value that only an
282 exception could have produced.
283 CAUTION: The only reliable ways to discover whether Under‐
284 flow has occurred are to test whether products or quotients
285 lie closer to zero than the underflow threshold, or to test
286 the Underflow flag. (Sums and differences cannot underflow
287 in IEEE 754; if x != y then x-y is correct to full precision
288 and certainly nonzero regardless of how tiny it may be.)
289 Products and quotients that underflow gradually can lose
290 accuracy gradually without vanishing, so comparing them with
291 zero (as one might on a VAX) will not reveal the loss. For‐
292 tunately, if a gradually underflowed value is destined to be
293 added to something bigger than the underflow threshold, as
294 is almost always the case, digits lost to gradual underflow
295 will not be missed because they would have been rounded off
296 anyway. So gradual underflows are usually provably ignor‐
297 able. The same cannot be said of underflows flushed to 0.
298
299 At the option of an implementor conforming to IEEE 754, other
300 ways to cope with exceptions may be provided:
301
302 4) ABORT. This mechanism classifies an exception in advance as
303 an incident to be handled by means traditionally associated
304 with error-handling statements like "ON ERROR GO TO ...".
305 Different languages offer different forms of this statement,
306 but most share the following characteristics:
307
308 — No means is provided to substitute a value for the offending
309 operation's result and resume computation from what may be
310 the middle of an expression. An exceptional result is aban‐
311 doned.
312
313 — In a subprogram that lacks an error-handling statement, an
314 exception causes the subprogram to abort within whatever
315 program called it, and so on back up the chain of calling
316 subprograms until an error-handling statement is encountered
317 or the whole task is aborted and memory is dumped.
318
319 5) STOP. This mechanism, requiring an interactive debugging
320 environment, is more for the programmer than the program.
321 It classifies an exception in advance as a symptom of a pro‐
322 grammer's error; the exception suspends execution as near as
323 it can to the offending operation so that the programmer can
324 look around to see how it happened. Quite often the first
325 several exceptions turn out to be quite unexceptionable, so
326 the programmer ought ideally to be able to resume execution
327 after each one as if execution had not been stopped.
328
329 6) ... Other ways lie beyond the scope of this document.
330
331 The crucial problem for exception handling is the problem of Scope, and
332 the problem's solution is understood, but not enough manpower was
333 available to implement it fully in time to be distributed in 4.3 BSD's
334 libm. Ideally, each elementary function should act as if it were indi‐
335 visible, or atomic, in the sense that ...
336
337 i) No exception should be signaled that is not deserved by the data
338 supplied to that function.
339
340 ii) Any exception signaled should be identified with that function
341 rather than with one of its subroutines.
342
343 iii) The internal behavior of an atomic function should not be dis‐
344 rupted when a calling program changes from one to another of the
345 five or so ways of handling exceptions listed above, although the
346 definition of the function may be correlated intentionally with
347 exception handling.
348
349 Ideally, every programmer should be able conveniently to turn a
350 debugged subprogram into one that appears atomic to its users. But
351 simulating all three characteristics of an atomic function is still a
352 tedious affair, entailing hosts of tests and saves-restores; work is
353 under way to ameliorate the inconvenience.
354
355 Meanwhile, the functions in libm are only approximately atomic. They
356 signal no inappropriate exception except possibly ...
357 Over/Underflow
358 when a result, if properly computed, might have lain
359 barely within range, and
360 Inexact in cabs, cbrt, hypot, log10 and pow
361 when it happens to be exact, thanks to fortuitous cancel‐
362 lation of errors.
363 Otherwise, ...
364 Invalid Operation is signaled only when
365 any result but NaN would probably be misleading.
366 Overflow is signaled only when
367 the exact result would be finite but beyond the overflow
368 threshold.
369 Divide-by-Zero is signaled only when
370 a function takes exactly infinite values at finite oper‐
371 ands.
372 Underflow is signaled only when
373 the exact result would be nonzero but tinier than the
374 underflow threshold.
375 Inexact is signaled only when
376 greater range or precision would be needed to represent
377 the exact result.
378
380 When signals are appropriate, they are emitted by certain operations
381 within the codes, so a subroutine-trace may be needed to identify the
382 function with its signal in case method 5) above is in use. And the
383 codes all take the IEEE 754 defaults for granted; this means that a
384 decision to trap all divisions by zero could disrupt a code that would
385 otherwise get correct results despite division by zero.
386
388 An explanation of IEEE 754 and its proposed extension p854 was pub‐
389 lished in the IEEE magazine MICRO in August 1984 under the title "A
390 Proposed Radix- and Word-length-independent Standard for Floating-point
391 Arithmetic" by W. J. Cody et al. The manuals for Pascal, C and BASIC
392 on the Apple Macintosh document the features of IEEE 754 pretty well.
393 Articles in the IEEE magazine COMPUTER vol. 14 no. 3 (Mar. 1981), and
394 in the ACM SIGNUM Newsletter Special Issue of Oct. 1979, may be helpful
395 although they pertain to superseded drafts of the standard.
396
398 W. Kahan, with the help of Z-S. Alex Liu, Stuart I. McDonald, Dr.
399 Kwok-Choi Ng, Peter Tang.
400
401
402
4034th Berkeley Distribution May 27, 1986 MATH(3M)