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