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