home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2331 < prev    next >
Encoding:
Internet Message Format  |  1990-12-28  |  54.9 KB

  1. From: glenn@qed.physics.su.OZ.AU (Glenn Geers)
  2. Newsgroups: alt.sources
  3. Subject: 80386 alternative math lib v2.0 part02/06
  4. Message-ID: <1990Dec16.210128.27937@metro.ucc.su.OZ.AU>
  5. Date: 16 Dec 90 21:01:28 GMT
  6.  
  7. Submitted-by: root@trantor
  8. Archive-name: mathlib2.0/part02
  9.  
  10. ---- Cut Here and feed the following to sh ----
  11. #!/bin/sh
  12. # this is mathlib.02 (part 2 of mathlib2.0)
  13. # do not concatenate these parts, unpack them in order with /bin/sh
  14. # file erf.c continued
  15. #
  16. if test ! -r _shar_seq_.tmp; then
  17.     echo 'Please unpack part 1 first!'
  18.     exit 1
  19. fi
  20. (read Scheck
  21.  if test "$Scheck" != 2; then
  22.     echo Please unpack part "$Scheck" next!
  23.     exit 1
  24.  else
  25.     exit 0
  26.  fi
  27. ) < _shar_seq_.tmp || exit 1
  28. if test ! -f _shar_wnt_.tmp; then
  29.     echo 'x - still skipping erf.c'
  30. else
  31. echo 'x - continuing file erf.c'
  32. sed 's/^X//' << 'SHAR_EOF' >> 'erf.c' &&
  33. X            xden = ysq;
  34. X            for (i = 0; i <= 3; i++) {
  35. X                xnum = (xnum+P[i])*ysq;
  36. X                xden = (xden+Q[i])*ysq;
  37. X            }
  38. X            result = ysq*(xnum+P[4])/(xden+Q[4]);
  39. X            result = (SQRPI-result)/y;
  40. X            if (jint != 2) {
  41. X                i = (int)(y*SIXTEN); ysq = (double)i/SIXTEN;
  42. X                result *= exp(-ysq*ysq)*exp(-(y-ysq)*(y+ysq));
  43. X            }
  44. X        }
  45. X    }
  46. X    if (jint == 0) {    /* Fix up for negative argument, erf, etc. */
  47. X        result = HALF-result; result += HALF;
  48. X        if (x < ZERO)
  49. X            result = -result;
  50. X    }
  51. X    else if (jint == 1) {
  52. X        if (x < ZERO)
  53. X            result = TWO-result;
  54. X    }
  55. X    else if (x < ZERO) {
  56. X        if (x < XNEG)
  57. X            result = XINF;
  58. X        else {
  59. X            i = (int)(x*SIXTEN); ysq = (double)i/SIXTEN;
  60. X            y = exp(ysq*ysq)*exp((x-ysq)*(x+ysq));
  61. X            result = -result; result += y+y;
  62. X        }
  63. X    }
  64. X    return result;
  65. }
  66. X
  67. /* 
  68. X *  This subprogram computes approximate values for erf(x).
  69. X *    (see comments heading calerf()).
  70. X * 
  71. X *    Author/date: W. J. Cody, January 8, 1985
  72. X */
  73. double
  74. #if defined(__STDC__) || defined(__GNUC__)
  75. erf(double x)
  76. #else
  77. erf(x)
  78. double x;
  79. #endif
  80. {
  81. X    return calerf(x,0);
  82. }
  83. X
  84. /* 
  85. X *  This subprogram computes approximate values for erfc(x).
  86. X *    (see comments heading calerf()).
  87. X * 
  88. X *    Author/date: W. J. Cody, January 8, 1985
  89. X */
  90. double
  91. #if defined(__STDC__) || defined(__GNUC__)
  92. erfc(double x)
  93. #else
  94. erfc(x)
  95. double x;
  96. #endif
  97. {
  98. X    return calerf(x,1);
  99. }
  100. X
  101. /* 
  102. X *  This subprogram computes approximate values for exp(x*x) * erfc(x).
  103. X *    (see comments heading calerf()).
  104. X * 
  105. X *    Author/date: W. J. Cody, March 30, 1987
  106. X */
  107. double
  108. #if defined(__STDC__) || defined(__GNUC__)
  109. erfcx(double x)
  110. #else
  111. erfcx(x)
  112. double x;
  113. #endif
  114. {
  115. X    return calerf(x,2);
  116. }
  117. SHAR_EOF
  118. echo 'File erf.c is complete' &&
  119. chmod 0644 erf.c ||
  120. echo 'restore of erf.c failed'
  121. Wc_c="`wc -c < 'erf.c'`"
  122. test 9681 -eq "$Wc_c" ||
  123.     echo 'erf.c: original size 9681, current size' "$Wc_c"
  124. rm -f _shar_wnt_.tmp
  125. fi
  126. # ============= nextafter.c ==============
  127. if test -f 'nextafter.c' -a X"$1" != X"-c"; then
  128.     echo 'x - skipping nextafter.c (File already exists)'
  129.     rm -f _shar_wnt_.tmp
  130. else
  131. > _shar_wnt_.tmp
  132. echo 'x - extracting nextafter.c (Text)'
  133. sed 's/^X//' << 'SHAR_EOF' > 'nextafter.c' &&
  134. /*
  135. ** This file is part of the alternative 80386 math library and is
  136. ** covered by the GNU General Public license with my modification
  137. ** as noted in the README file that accompanied this file.
  138. **
  139. ** Copyright 1990 G. Geers
  140. **
  141. ** A mix of C and assembler - well I've got the functions so I might 
  142. ** as well use them!
  143. **
  144. */
  145. X
  146. #include "fpumath.h"
  147. X
  148. asm(".align 4");
  149. asm(".Lulp:");
  150. asm(".double 1.1102230246251565e-16");
  151. X
  152. asm(".align 4");
  153. asm(".Lulpup:");
  154. asm(".double 2.2204460492503131e-16");
  155. X
  156. double
  157. nextafter(x, y)
  158. double x, y;
  159. {
  160. X    asm("subl $8, %esp");
  161. X
  162. X    if (isnan(x) || isnan(y))
  163. X        return(quiet_nan(1.0));
  164. X
  165. X    if (isinf(x))
  166. X        if (y > x)
  167. X            return(-max_normal());
  168. X        else
  169. X            if (y < x)
  170. X                return(max_normal());
  171. X
  172. X    if (x == 0.0)
  173. X        if (y > 0.0)
  174. X            return(min_subnormal());
  175. X        else
  176. X            return(-min_subnormal());
  177. X
  178. X    if (isnormal(x)) {
  179. X        if ((x == min_normal()) && y < x)
  180. X            return(max_subnormal());
  181. X
  182. X        if ((x == max_normal()) && y > x)
  183. X            return(infinity());
  184. X
  185. X        if ((x == -max_normal()) && y < x)
  186. X            return(-infinity());
  187. X
  188. X        asm("movl 12(%ebp), %eax");
  189. X        asm("andl $0x7ff00000, %eax");
  190. X        asm("movl %eax, -12(%ebp)");
  191. X        asm("movl $0x0, -16(%ebp)");
  192. X        asm("fldl -16(%ebp)");
  193. X
  194. X        if (fabs(x) <= 2.0 && y < x)
  195. X            asm("fldl .Lulp");
  196. X        else
  197. X            asm("fldl .Lulpup");
  198. X
  199. X        asm("fmulp");
  200. X
  201. X        if (y > x) {
  202. X            asm("fldl 8(%ebp)");
  203. X            asm("faddp");
  204. X            asm("leave");
  205. X            asm("ret");
  206. X        }
  207. X        if (y < x) {
  208. X            asm("fldl 8(%ebp)");
  209. X            asm("fsubp");
  210. X            asm("leave");
  211. X            asm("ret");
  212. X        }
  213. X    }
  214. X    else
  215. X    if (issubnormal(x)) {
  216. X        if ((x == max_subnormal()) && y > x)
  217. X            return(min_normal());
  218. X
  219. X        if ((x == -max_subnormal()) && y < x)
  220. X            return(-min_normal());
  221. X
  222. X        if (y > x) 
  223. X            return(x + min_subnormal());
  224. X
  225. X        if (y < x)
  226. X            return(x - min_subnormal());
  227. X    }
  228. X    
  229. X    return(x);
  230. }
  231. SHAR_EOF
  232. chmod 0644 nextafter.c ||
  233. echo 'restore of nextafter.c failed'
  234. Wc_c="`wc -c < 'nextafter.c'`"
  235. test 1725 -eq "$Wc_c" ||
  236.     echo 'nextafter.c: original size 1725, current size' "$Wc_c"
  237. rm -f _shar_wnt_.tmp
  238. fi
  239. # ============= j0f.c ==============
  240. if test -f 'j0f.c' -a X"$1" != X"-c"; then
  241.     echo 'x - skipping j0f.c (File already exists)'
  242.     rm -f _shar_wnt_.tmp
  243. else
  244. > _shar_wnt_.tmp
  245. echo 'x - extracting j0f.c (Text)'
  246. sed 's/^X//' << 'SHAR_EOF' > 'j0f.c' &&
  247. #ifndef lint
  248. static char sccsid[] = "@(#)j0.c    1.2    (ucb.beef)    10/2/89";
  249. #endif    /* !defined(lint) */
  250. /* 
  251. X *  This packet computes zero-order Bessel functions of the first and
  252. X *    second kind (j0 and y0), for real arguments x, where 0 < x <= XMAX
  253. X *    for y0, and |x| <= XMAX for j0.  It contains three function-type
  254. X *    subprograms, j0, y0 and caljy0.  The calling statements for the
  255. X *    primary entries are:
  256. X * 
  257. X *            y = j0(x)
  258. X *    and
  259. X *            y = y0(x),
  260. X * 
  261. X *    where the entry points correspond to the functions j0(x) and y0(x),
  262. X *    respectively.  The routine caljy0() is intended for internal packet
  263. X *    use only, all computations within the packet being concentrated in
  264. X *    this one routine.  The function subprograms invoke  caljy0  with
  265. X *    the statement
  266. X *
  267. X *            result = caljy0(x,jint);
  268. X *
  269. X *    where the parameter usage is as follows:
  270. X * 
  271. X *       Function                  Parameters for caljy0
  272. X *        call              x             result          jint
  273. X * 
  274. X *       j0(x)        |x| <= XMAX         j0(x)           0
  275. X *       y0(x)      0 < x <= XMAX         y0(x)           1
  276. X * 
  277. X *    The main computation uses unpublished minimax rational
  278. X *    approximations for x <= 8.0, and an approximation from the 
  279. X *    book  Computer Approximations  by Hart, et. al., Wiley and Sons, 
  280. X *    New York, 1968, for arguments larger than 8.0   Part of this
  281. X *    transportable packet is patterned after the machine-dependent
  282. X *    FUNPACK program for j0(x), but cannot match that version for
  283. X *    efficiency or accuracy.  This version uses rational functions
  284. X *    that are theoretically accurate to at least 18 significant decimal
  285. X *    digits for x <= 8, and at least 18 decimal places for x > 8.  The
  286. X *    accuracy achieved depends on the arithmetic system, the compiler,
  287. X *    the intrinsic functions, and proper selection of the machine-
  288. X *    dependent constants.
  289. X * 
  290. X *********************************************************************
  291. X * 
  292. X *  Explanation of machine-dependent constants
  293. X * 
  294. X *    XINF   = largest positive machine number
  295. X *    XMAX   = largest acceptable argument.  The functions sin(), floor()
  296. X *             and cos() must perform properly for  fabs(x) <= XMAX.
  297. X *             We recommend that XMAX be a small integer multiple of
  298. X *             sqrt(1/eps), where eps is the smallest positive number
  299. X *             such that  1+eps > 1. 
  300. X *    XSMALL = positive argument such that  1.0-(x/2)**2 = 1.0
  301. X *             to machine precision for all  fabs(x) <= XSMALL.
  302. X *             We recommend that  XSMALL < sqrt(eps)/beta, where beta
  303. X *             is the floating-point radix (usually 2 or 16).
  304. X * 
  305. X *      Approximate values for some important machines are
  306. X * 
  307. X *                           eps      XMAX     XSMALL      XINF  
  308. X * 
  309. X *   CDC 7600      (S.P.)  7.11E-15  1.34E+08  2.98E-08  1.26E+322
  310. X *   CRAY-1        (S.P.)  7.11E-15  1.34E+08  2.98E-08  5.45E+2465
  311. X *   IBM PC (8087) (S.P.)  5.96E-08  8.19E+03  1.22E-04  3.40E+38
  312. X *   IBM PC (8087) (D.P.)  1.11D-16  2.68D+08  3.72D-09  1.79D+308
  313. X *   IBM 195       (D.P.)  2.22D-16  6.87D+09  9.09D-13  7.23D+75
  314. X *   UNIVAC 1108   (D.P.)  1.73D-18  4.30D+09  2.33D-10  8.98D+307
  315. X *   VAX 11/780    (D.P.)  1.39D-17  1.07D+09  9.31D-10  1.70D+38
  316. X * 
  317. X *********************************************************************
  318. X *********************************************************************
  319. X * 
  320. X *  Error Returns
  321. X * 
  322. X *   The program returns the value zero for  x > XMAX, and returns
  323. X *     -XINF when y0 is called with a negative or zero argument.
  324. X * 
  325. X * 
  326. X *  Intrinsic functions required are:
  327. X * 
  328. X *      fabs, floor, cos, log, sin, sqrt
  329. X * 
  330. X *
  331. X *   Latest modification: June 2, 1989
  332. X * 
  333. X *   Author: W. J. Cody
  334. X *           Mathematics and Computer Science Division 
  335. X *           Argonne National Laboratory
  336. X *           Argonne, IL 60439
  337. X */
  338. X
  339. X #include "fpumath.h"
  340. X
  341. X                    /* Machine-dependent constants */
  342. #if defined(vax) || defined(tahoe)
  343. #define    XMAX    (double)1.07e9
  344. #define    XSMALL    (double)9.31e-10
  345. #define    XINF    (double)1.70e38
  346. #else    /* defined(vax) || defined(tahoe) */
  347. #define    XMAX    (double)2.68e8
  348. #define    XSMALL    (double)3.72e-9
  349. #define    XINF    MAXFLOAT
  350. #endif    /* defined(vax) || defined(tahoe) */
  351. X                    /* Mathematical constants */
  352. #define    EIGHT    (double)8
  353. #define    CONS    (double)(-1.1593151565841244881e-1) /* ln(.5)+Euler's gamma */
  354. #define    FIVE5    (double)5.5
  355. #define    FOUR    (double)4
  356. #define    ONE    (double)1
  357. #define    ONEOV8    (double)0.125
  358. #define    PI2    (double)6.3661977236758134308e-1
  359. #define    P17    (double)1.716e-1
  360. #define    SIXTY4    (double)64
  361. #define    THREE    (double)3
  362. #define    TWOPI    (double)6.2831853071795864769e0
  363. #define    TWOPI1    (double)6.28125
  364. #define    TWOPI2    (double)1.9353071795864769253e-03
  365. #define    TWO56    (double)256
  366. #define    ZERO    (double)0
  367. X                    /* Zeroes of Bessel functions */
  368. #define    XJ0    (double)2.4048255576957727686e0
  369. #define    XJ1    (double)5.5200781102863106496e0
  370. #define    XY0    (double)8.9357696627916752158e-1
  371. #define    XY1    (double)3.9576784193148578684e0
  372. #define    XY2    (double)7.0860510603017726976e0
  373. #define    XJ01    (double)616
  374. #define    XJ02    (double)(-1.4244423042272313784e-3)
  375. #define    XJ11    (double)1413
  376. #define    XJ12    (double)5.4686028631064959660e-4
  377. #define    XY01    (double)228.0
  378. #define    XY02    (double)2.9519662791675215849e-3
  379. #define    XY11    (double)1013
  380. #define    XY12    (double)6.4716931485786837568e-4
  381. #define    XY21    (double)1814
  382. #define    XY22    (double)1.1356030177269762362e-4
  383. X
  384. /*
  385. X * Coefficients for rational approximation to ln(x/a)
  386. X */
  387. static double PLG[] = {
  388. X    -2.4562334077563243311e01,
  389. X     2.3642701335621505212e02,
  390. X    -5.4989956895857911039e02,
  391. X     3.5687548468071500413e02,
  392. };
  393. static double QLG[] = {
  394. X    -3.5553900764052419184e01,
  395. X     1.9400230218539473193e02,
  396. X    -3.3442903192607538956e02,
  397. X     1.7843774234035750207e02,
  398. };
  399. X
  400. /*
  401. X * Coefficients for rational approximation of
  402. X * j0(x) / (x**2 - XJ0**2),  XSMALL  <  |x|  <=  4.0
  403. X */
  404. static double PJ0[] = {
  405. X     6.6302997904833794242e06,
  406. X    -6.2140700423540120665e08,
  407. X     2.7282507878605942706e10,
  408. X    -4.1298668500990866786e11,
  409. X    -1.2117036164593528341e-01,
  410. X     1.0344222815443188943e02,
  411. X    -3.6629814655107086448e04,
  412. };
  413. static double QJ0[] = {
  414. X    4.5612696224219938200e05,
  415. X    1.3985097372263433271e08,
  416. X    2.6328198300859648632e10,
  417. X    2.3883787996332290397e12,
  418. X    9.3614022392337710626e02,
  419. };
  420. X
  421. /*
  422. X * Coefficients for rational approximation of
  423. X * j0(x) / (x**2 - XJ1**2),  4.0  <  |x|  <=  8.0
  424. X */
  425. static double PJ1[] = {
  426. X     4.4176707025325087628e03,
  427. X     1.1725046279757103576e04,
  428. X     1.0341910641583726701e04,
  429. X    -7.2879702464464618998e03,
  430. X    -1.2254078161378989535e04,
  431. X    -1.8319397969392084011e03,
  432. X     4.8591703355916499363e01,
  433. X     7.4321196680624245801e02,
  434. };
  435. static double QJ1[] = {
  436. X     3.3307310774649071172e02,
  437. X    -2.9458766545509337327e03,
  438. X     1.8680990008359188352e04,
  439. X    -8.4055062591169562211e04,
  440. X     2.4599102262586308984e05,
  441. X    -3.5783478026152301072e05,
  442. X    -2.5258076240801555057e01,
  443. };
  444. X
  445. /*
  446. X * Coefficients for rational approximation of
  447. X *   (y0(x) - 2 LN(x/XY0) j0(x)) / (x**2 - XY0**2),
  448. X *       XSMALL  <  |x|  <=  3.0
  449. X */
  450. static double PY0[] = {
  451. X     1.0102532948020907590e04,
  452. X    -2.1287548474401797963e06,
  453. X     2.0422274357376619816e08,
  454. X    -8.3716255451260504098e09,
  455. X     1.0723538782003176831e11,
  456. X    -1.8402381979244993524e01,
  457. };
  458. static double QY0[] = {
  459. X    6.6475986689240190091e02,
  460. X    2.3889393209447253406e05,
  461. X    5.5662956624278251596e07,
  462. X    8.1617187777290363573e09,
  463. X    5.8873865738997033405e11,
  464. };
  465. X
  466. /*
  467. X * Coefficients for rational approximation of
  468. X *   (y0(x) - 2 LN(x/XY1) j0(x)) / (x**2 - XY1**2),
  469. X *       3.0  <  |x|  <=  5.5
  470. X */
  471. static double PY1[] = {
  472. X    -1.4566865832663635920e04,
  473. X     4.6905288611678631510e06,
  474. X    -6.9590439394619619534e08,
  475. X     4.3600098638603061642e10,
  476. X    -5.5107435206722644429e11,
  477. X    -2.2213976967566192242e13,
  478. X     1.7427031242901594547e01,
  479. };
  480. static double QY1[] = {
  481. X    8.3030857612070288823e02,
  482. X    4.0669982352539552018e05,
  483. X    1.3960202770986831075e08,
  484. X    3.4015103849971240096e10,
  485. X    5.4266824419412347550e12,
  486. X    4.3386146580707264428e14,
  487. };
  488. X
  489. /*
  490. X * Coefficients for rational approximation of
  491. X *   (y0(x) - 2 LN(x/XY2) j0(x)) / (x**2 - XY2**2),
  492. X *       5.5  <  |x|  <=  8.0
  493. X */
  494. static double PY2[] = {
  495. X     2.1363534169313901632e04,
  496. X    -1.0085539923498211426e07,
  497. X     2.1958827170518100757e09,
  498. X    -1.9363051266772083678e11,
  499. X    -1.2829912364088687306e11,
  500. X     6.7016641869173237784e14,
  501. X    -8.0728726905150210443e15,
  502. X    -1.7439661319197499338e01,
  503. };
  504. static double QY2[] = {
  505. X    8.7903362168128450017e02,
  506. X    5.3924739209768057030e05,
  507. X    2.4727219475672302327e08,
  508. X    8.6926121104209825246e10,
  509. X    2.2598377924042897629e13,
  510. X    3.9272425569640309819e15,
  511. X    3.4563724628846457519e17,
  512. };
  513. X
  514. /*
  515. X * Coefficients for Hart,s approximation,  |x| > 8.0
  516. X */
  517. static double P0[] = {
  518. X    3.4806486443249270347e03,
  519. X    2.1170523380864944322e04,
  520. X    4.1345386639580765797e04,
  521. X    2.2779090197304684302e04,
  522. X    8.8961548424210455236e-01,
  523. X    1.5376201909008354296e02,
  524. };
  525. static double Q0[] = {
  526. X    3.5028735138235608207e03,
  527. X    2.1215350561880115730e04,
  528. X    4.1370412495510416640e04,
  529. X    2.2779090197304684318e04,
  530. X    1.5711159858080893649e02,
  531. };
  532. static double P1[] = {
  533. X    -2.2300261666214198472e01,
  534. X    -1.1183429920482737611e02,
  535. X    -1.8591953644342993800e02,
  536. X    -8.9226600200800094098e01,
  537. X    -8.8033303048680751817e-03,
  538. X    -1.2441026745835638459e00,
  539. };
  540. static double Q1[] = {
  541. X    1.4887231232283756582e03,
  542. X    7.2642780169211018836e03,
  543. X    1.1951131543434613647e04,
  544. X    5.7105024128512061905e03,
  545. X    9.0593769594993125859e01,
  546. };
  547. X
  548. static double
  549. #if defined(__STDC__) || defined(__GNUC__)
  550. caljy0(double x,int jint)
  551. #else
  552. caljy0(x,jint)
  553. double x;
  554. int jint;
  555. #endif
  556. {
  557. X    int i;
  558. X    double resj,down,up,xden,xnum,w,wsq,z,zsq;
  559. X
  560. X    if (jint && x <= ZERO)        /* Check for error conditions */
  561. X        return -XINF;
  562. #define    ax x
  563. X    else if ((ax = fabs(x)) > XMAX)
  564. X        return ZERO;
  565. /*
  566. X * Calculate j0 or y0 for |x|  >  8.0
  567. X */
  568. X    if (ax > EIGHT) {
  569. X        z = EIGHT/ax;
  570. X        w = ax/TWOPI;
  571. X        w = floor(w)+ONEOV8;
  572. X        w = (ax-w*TWOPI1)-w*TWOPI2;
  573. X        zsq = z*z;
  574. X        xnum = P0[4]*zsq+P0[5];
  575. X        xden = zsq+Q0[4];
  576. X        up = P1[4]*zsq+P1[5];
  577. X        down = zsq+Q1[4];
  578. X        for (i = 0; i <= 3; i++) {
  579. X            xnum = xnum*zsq+P0[i];
  580. X            xden = xden*zsq+Q0[i];
  581. X            up = up*zsq+P1[i];
  582. X            down = down*zsq+Q1[i];
  583. X        }
  584. #define    r0 xnum
  585. #define    r1 up
  586. X        r0 = xnum/xden;
  587. X        r1 = up/down;
  588. X        return sqrt(PI2/ax)*(jint ? r0*sin(w)+z*r1*cos(w) :
  589. X            r0*cos(w)-z*r1*sin(w));
  590. #undef    r1
  591. #undef    r0
  592. X    }
  593. X    if (ax <= XSMALL)
  594. X        return jint ? PI2*(log(ax)+CONS) : ONE;
  595. /*
  596. X * Calculate j0 for appropriate interval, preserving
  597. X *    accuracy near the zero of j0
  598. X */
  599. X    zsq = ax*ax;
  600. X    if (ax <= FOUR) {
  601. X        xnum = (PJ0[4]*zsq+PJ0[5])*zsq+PJ0[6];
  602. X        xden = zsq+QJ0[4];
  603. X        for (i = 0; i <= 3; i++) {
  604. X            xnum = xnum*zsq+PJ0[i];
  605. X            xden = xden*zsq+QJ0[i];
  606. X        }
  607. #define    prod resj
  608. X        prod = ((ax-XJ01/TWO56)-XJ02)*(ax+XJ0);
  609. X    }
  610. X    else {
  611. X        wsq = ONE-zsq/SIXTY4;
  612. X        xnum = PJ1[6]*wsq+PJ1[7];
  613. X        xden = wsq+QJ1[6];
  614. X        for (i = 0; i <= 5; i++) {
  615. X            xnum = xnum*wsq+PJ1[i];
  616. X            xden = xden*wsq+QJ1[i];
  617. X        }
  618. X        prod = (ax+XJ1)*((ax-XJ11/TWO56)-XJ12);
  619. X    }
  620. #define    result resj
  621. X    result = prod*xnum/xden;
  622. #undef    prod
  623. X    if (!jint)
  624. X        return result;
  625. /*
  626. X * Calculate y0.  First find  resj = pi/2 ln(x/xn) j0(x),
  627. X *   where xn is a zero of y0
  628. X */
  629. #define    xy z
  630. X    if (ax <= THREE) {
  631. X        up = (ax-XY01/TWO56)-XY02;
  632. X        xy = XY0;
  633. X    }
  634. X    else if (ax <= FIVE5) {
  635. X        up = (ax-XY11/TWO56)-XY12;
  636. X        xy = XY1;
  637. X    }
  638. X    else {
  639. X        up = (ax-XY21/TWO56)-XY22;
  640. X        xy = XY2;
  641. X    }
  642. X    down = ax+xy;
  643. X    if (fabs(up) < P17*down) {
  644. X        w = up/down;
  645. X        wsq = w*w;
  646. X        xnum = PLG[0];
  647. X        xden = wsq+QLG[0];
  648. X        for (i = 1; i <= 3; i++) {
  649. X            xnum = xnum*wsq+PLG[i];
  650. X            xden = xden*wsq+QLG[i];
  651. X        }
  652. X        resj = PI2*result*w*xnum/xden;
  653. X    }
  654. X    else
  655. X        resj = PI2*result*log(ax/xy);
  656. #undef    xy
  657. #undef    result
  658. /*
  659. X * Now calculate y0 for appropriate interval, preserving
  660. X *    accuracy near the zero of y0
  661. X */
  662. X    if (ax <= THREE) {
  663. X        xnum = PY0[5]*zsq+PY0[0];
  664. X        xden = zsq+QY0[0];
  665. X        for (i = 1; i <= 4; i++) {
  666. X            xnum = xnum*zsq+PY0[i];
  667. X            xden = xden*zsq+QY0[i];
  668. X        }
  669. X    }
  670. X    else if (ax <= FIVE5) {
  671. #undef    ax
  672. X        xnum = PY1[6]*zsq+PY1[0];
  673. X        xden = zsq+QY1[0];
  674. X        for (i = 1; i <= 5; i++) {
  675. X            xnum = xnum*zsq+PY1[i];
  676. X            xden = xden*zsq+QY1[i];
  677. X        }
  678. X    }
  679. X    else {
  680. X        xnum = PY2[7]*zsq+PY2[0];
  681. X        xden = zsq+QY2[0];
  682. X        for (i = 1; i <= 6; i++) {
  683. X            xnum = xnum*zsq+PY2[i];
  684. X            xden = xden*zsq+QY2[i];
  685. X        }
  686. X    }
  687. X    return resj+up*down*xnum/xden;
  688. }
  689. X
  690. /* 
  691. X *  This subprogram computes approximate values for Bessel functions
  692. X *    of the first kind of order zero for arguments  |x| <= XMAX
  693. X *    (see comments heading caljy0).
  694. X */
  695. float
  696. j0f(float x)
  697. {
  698. X    return ((float)caljy0(x,0));
  699. }
  700. X
  701. /* 
  702. X *  This subprogram computes approximate values for Bessel functions
  703. X *    of the second kind of order zero for arguments 0 < x <= XMAX
  704. X *    (see comments heading caljy0).
  705. X */
  706. float
  707. y0f(float x)
  708. {
  709. X    return ((float)caljy0(x,1));
  710. }
  711. SHAR_EOF
  712. chmod 0644 j0f.c ||
  713. echo 'restore of j0f.c failed'
  714. Wc_c="`wc -c < 'j0f.c'`"
  715. test 12458 -eq "$Wc_c" ||
  716.     echo 'j0f.c: original size 12458, current size' "$Wc_c"
  717. rm -f _shar_wnt_.tmp
  718. fi
  719. # ============= lgammaf.c ==============
  720. if test -f 'lgammaf.c' -a X"$1" != X"-c"; then
  721.     echo 'x - skipping lgammaf.c (File already exists)'
  722.     rm -f _shar_wnt_.tmp
  723. else
  724. > _shar_wnt_.tmp
  725. echo 'x - extracting lgammaf.c (Text)'
  726. sed 's/^X//' << 'SHAR_EOF' > 'lgammaf.c' &&
  727. #ifndef lint
  728. static char sccsid[] = "@(#)lgamma.c    1.2    (ucb.beef)    3/1/89";
  729. #endif    /* !defined(lint) */
  730. /* 
  731. X * This routine calculates the log(GAMMA) function for a positive real
  732. X *   argument x.  Computation is based on an algorithm outlined in
  733. X *   references 1 and 2.  The program uses rational functions that
  734. X *   theoretically approximate log(GAMMA) to at least 18 significant
  735. X *   decimal digits.  The approximation for x > 12 is from reference
  736. X *   3, while approximations for x < 12.0 are similar to those in
  737. X *   reference 1, but are unpublished.  The accuracy achieved depends
  738. X *   on the arithmetic system, the compiler, the intrinsic functions,
  739. X *   and proper selection of the machine-dependent constants.
  740. X * 
  741. X **********************************************************************
  742. X **********************************************************************
  743. X * 
  744. X * Explanation of machine-dependent constants
  745. X * 
  746. X * beta   - radix for the floating-point representation
  747. X * maxexp - the smallest positive power of beta that overflows
  748. X * XBIG   - largest argument for which LN(GAMMA(x)) is representable
  749. X *          in the machine, i.e., the solution to the equation
  750. X *                  LN(GAMMA(XBIG)) = beta**maxexp
  751. X * XINF   - largest machine representable floating-point number;
  752. X *          approximately beta**maxexp.
  753. X * EPS    - The smallest positive floating-point number such that
  754. X *          1.0+EPS > 1.0
  755. X * FRTBIG - Rough estimate of the fourth root of XBIG
  756. X * 
  757. X * 
  758. X *     Approximate values for some important machines are:
  759. X * 
  760. X *                           beta      maxexp         XBIG
  761. X * 
  762. X * CRAY-1        (S.P.)        2        8191       9.62E+2461
  763. X * Cyber 180/855
  764. X *   under NOS   (S.P.)        2        1070       1.72E+319
  765. X * IEEE (IBM/XT,
  766. X *   SUN, etc.)  (S.P.)        2         128       4.08E+36
  767. X * IEEE (IBM/XT,
  768. X *   SUN, etc.)  (D.P.)        2        1024       2.55D+305
  769. X * IBM 3033      (D.P.)       16          63       4.29D+73
  770. X * VAX D-Format  (D.P.)        2         127       2.05D+36
  771. X * VAX G-Format  (D.P.)        2        1023       1.28D+305
  772. X *
  773. X * 
  774. X *                           XINF        EPS        FRTBIG
  775. X * 
  776. X * CRAY-1        (S.P.)   5.45E+2465   7.11E-15    3.13E+615
  777. X * Cyber 180/855
  778. X *   under NOS   (S.P.)   1.26E+322    3.55E-15    6.44E+79
  779. X * IEEE (IBM/XT,
  780. X *   SUN, etc.)  (S.P.)   3.40E+38     1.19E-7     1.42E+9
  781. X * IEEE (IBM/XT,
  782. X *   SUN, etc.)  (D.P.)   1.79D+308    2.22D-16    2.25D+76
  783. X * IBM 3033      (D.P.)   7.23D+75     2.22D-16    2.56D+18
  784. X * VAX D-Format  (D.P.)   1.70D+38     1.39D-17    1.20D+9
  785. X * VAX G-Format  (D.P.)   8.98D+307    1.11D-16    1.89D+76
  786. X * 
  787. X ***************************************************************
  788. X ***************************************************************
  789. X * 
  790. X * Error returns
  791. X * 
  792. X *  The program returns the value XINF for x <= 0.0 or when
  793. X *     overflow would occur.  The computation is believed to 
  794. X *     be free of underflow and overflow.
  795. X * 
  796. X * 
  797. X * Intrinsic functions required are:
  798. X * 
  799. X *      log
  800. X * 
  801. X * 
  802. X * References:
  803. X * 
  804. X *  1) W. J. Cody and K. E. Hillstrom, 'Chebyshev Approximations for
  805. X *     the Natural Logarithm of the Gamma Function,' Math. Comp. 21,
  806. X *     1967, pp. 198-203.
  807. X * 
  808. X *  2) K. E. Hillstrom, ANL/AMD Program ANLC366S, DGAMMA/DLGAMA, May,
  809. X *     1969.
  810. X * 
  811. X *  3) Hart, Et. Al., Computer Approximations, Wiley and sons, New
  812. X *     York, 1968.
  813. X * 
  814. X * 
  815. X *  Authors: W. J. Cody and L. Stoltz
  816. X *           Argonne National Laboratory
  817. X * 
  818. X *  Latest modification: June 16, 1988
  819. X */
  820. X
  821. #include "fpumath.h"
  822. X
  823. X                    /* Machine-dependent constants */
  824. #if defined(vax) || defined(tahoe)
  825. #define    XBIG    (double)2.05e36
  826. #define    XINF    (double)1.70e38
  827. #define    EPS    (double)1.39e-17
  828. #define    FRTBIG    (double)1.20e9
  829. #else    /* defined(vax) || defined(tahoe) */
  830. #define    XBIG    (double)2.55e305
  831. #define    XINF    MAXFLOAT
  832. #define    EPS    (double)2.22e-16
  833. #define    FRTBIG    (double)2.25e76
  834. #endif    /* defined(vax) || defined(tahoe) */
  835. X                    /* Mathematical constants */
  836. #define    ONE    (double)1
  837. #define    HALF    (double)0.5
  838. #define    TWELVE    (double)12
  839. #define    ZERO    (double)0
  840. #define    FOUR    (double)4
  841. #define    THRHAL    (double)1.5
  842. #define    TWO    (double)2
  843. #define    PNT68    (double)0.6796875
  844. #define    SQRTPI    (double)0.9189385332046727417803297
  845. X
  846. /*
  847. X * Numerator and denominator coefficients for rational minimax Approximation
  848. X * over (0.5,1.5).
  849. X */
  850. static double D1 = -5.772156649015328605195174e-1;
  851. static double P1[] = {
  852. X    4.945235359296727046734888e0,
  853. X    2.018112620856775083915565e2,
  854. X    2.290838373831346393026739e3,
  855. X    1.131967205903380828685045e4,
  856. X    2.855724635671635335736389e4,
  857. X    3.848496228443793359990269e4,
  858. X    2.637748787624195437963534e4,
  859. X    7.225813979700288197698961e3,
  860. };
  861. static double Q1[] = {
  862. X    6.748212550303777196073036e1,
  863. X    1.113332393857199323513008e3,
  864. X    7.738757056935398733233834e3,
  865. X    2.763987074403340708898585e4,
  866. X    5.499310206226157329794414e4,
  867. X    6.161122180066002127833352e4,
  868. X    3.635127591501940507276287e4,
  869. X    8.785536302431013170870835e3,
  870. };
  871. X
  872. /*
  873. X * Numerator and denominator coefficients for rational minimax Approximation
  874. X * over (1.5,4.0).
  875. X */
  876. static double D2 = 4.227843350984671393993777e-1;
  877. static double P2[] = {
  878. X    4.974607845568932035012064e0,
  879. X    5.424138599891070494101986e2,
  880. X    1.550693864978364947665077e4,
  881. X    1.847932904445632425417223e5,
  882. X    1.088204769468828767498470e6,
  883. X    3.338152967987029735917223e6,
  884. X    5.106661678927352456275255e6,
  885. X    3.074109054850539556250927e6,
  886. };
  887. static double Q2[] = {
  888. X    1.830328399370592604055942e2,
  889. X    7.765049321445005871323047e3,
  890. X    1.331903827966074194402448e5,
  891. X    1.136705821321969608938755e6,
  892. X    5.267964117437946917577538e6,
  893. X    1.346701454311101692290052e7,
  894. X    1.782736530353274213975932e7,
  895. X    9.533095591844353613395747e6,
  896. };
  897. X
  898. /*
  899. X * Numerator and denominator coefficients for rational minimax Approximation
  900. X * over (4.0,12.0).
  901. X */
  902. static double D4 = 1.791759469228055000094023e0;
  903. static double P4[] = {
  904. X    1.474502166059939948905062e4,
  905. X    2.426813369486704502836312e6,
  906. X    1.214755574045093227939592e8,
  907. X    2.663432449630976949898078e9,
  908. X    2.940378956634553899906876e10,
  909. X    1.702665737765398868392998e11,
  910. X    4.926125793377430887588120e11,
  911. X    5.606251856223951465078242e11,
  912. };
  913. static double Q4[] = {
  914. X    2.690530175870899333379843e3,
  915. X    6.393885654300092398984238e5,
  916. X    4.135599930241388052042842e7,
  917. X    1.120872109616147941376570e9,
  918. X    1.488613728678813811542398e10,
  919. X    1.016803586272438228077304e11,
  920. X    3.417476345507377132798597e11,
  921. X    4.463158187419713286462081e11,
  922. };
  923. X
  924. /*
  925. X * Coefficients for minimax approximation over (12, INF).
  926. X */
  927. static double C[] = {
  928. X    -1.910444077728e-03,
  929. X     8.4171387781295e-04,
  930. X    -5.952379913043012e-04,
  931. X     7.93650793500350248e-04,
  932. X    -2.777777777777681622553e-03,
  933. X     8.333333333333333331554247e-02,
  934. X     5.7083835261e-03,
  935. };
  936. X
  937. float
  938. lgammaf(float x)
  939. {
  940. X    register i;
  941. X    double res,corr,xden,xnum,dtmp;
  942. X
  943. #define    y x
  944. X    if (y > ZERO && y <= XBIG) {
  945. X        if (y <= EPS)
  946. X            res = -log(y);
  947. X        else if (y <= THRHAL) {            /*  EPS < x <= 1.5 */
  948. #define    xm1 dtmp
  949. X            if (y < PNT68) {
  950. X                corr = -log(y);
  951. X                xm1 = y;
  952. X            }
  953. X            else {
  954. X                corr = ZERO;
  955. X                xm1 = y-HALF; xm1 -= HALF;
  956. X            }
  957. X            if (y <= HALF || y >= PNT68) {
  958. X                xden = ONE;
  959. X                xnum = ZERO;
  960. X                for (i = 0; i <= 7; i++) {
  961. X                    xnum = xnum*xm1+P1[i];
  962. X                    xden = xden*xm1+Q1[i];
  963. X                }
  964. X                res = xnum/xden; res = corr+xm1*(D1+xm1*res);
  965. #undef    xm1
  966. X            }
  967. X            else {
  968. #define    xm2 dtmp
  969. X                xm2 = y-HALF; xm2 -= HALF;
  970. X                xden = ONE;
  971. X                xnum = ZERO;
  972. X                for (i = 0; i <= 7; i++) {
  973. X                    xnum = xnum*xm2+P2[i];
  974. X                    xden = xden*xm2+Q2[i];
  975. X                }
  976. X                res = xnum/xden; res = corr+xm2*(D2+xm2*res);
  977. X            }
  978. X        }
  979. X        else if (y <= FOUR) {            /*  1.5 < x <= 4.0 */
  980. X            xm2 = y-TWO;
  981. X            xden = ONE;
  982. X            xnum = ZERO;
  983. X            for (i = 0; i <= 7; i++) {
  984. X                xnum = xnum*xm2+P2[i];
  985. X                xden = xden*xm2+Q2[i];
  986. X            }
  987. X            res = xnum/xden; res = xm2*(D2+xm2*res);
  988. #undef    xm2
  989. X        }
  990. X        else if (y <= TWELVE) {            /*  4.0 < x <= 12.0 */
  991. #define    xm4 dtmp
  992. X            xm4 = y-FOUR;
  993. X            xden = -ONE;
  994. X            xnum = ZERO;
  995. X            for (i = 0; i <= 7; i++) {
  996. X                xnum = xnum*xm4+P4[i];
  997. X                xden = xden*xm4+Q4[i];
  998. X            }
  999. X            res = xnum/xden; res = D4+xm4*res;
  1000. #undef    xm4
  1001. X        }
  1002. X        else {                    /*  x >= 12.0 */
  1003. X            res = ZERO;
  1004. X            if (y <= FRTBIG) {
  1005. X                res = C[6];
  1006. #define    ysq dtmp
  1007. X                ysq = y*y;
  1008. X                for (i = 0; i <= 5; i++)
  1009. X                    res = res/ysq+C[i];
  1010. #define    ysq dtmp
  1011. X            }
  1012. X            res /= y;
  1013. X            corr = log(y);
  1014. X            res += SQRTPI; res -= HALF*corr;
  1015. X            res += y*(corr-ONE);
  1016. X        }
  1017. #undef    y
  1018. X    }
  1019. X    else                    /*  Return for bad arguments */
  1020. X        res = XINF;
  1021. X    return ((float)res);
  1022. }
  1023. SHAR_EOF
  1024. chmod 0644 lgammaf.c ||
  1025. echo 'restore of lgammaf.c failed'
  1026. Wc_c="`wc -c < 'lgammaf.c'`"
  1027. test 8280 -eq "$Wc_c" ||
  1028.     echo 'lgammaf.c: original size 8280, current size' "$Wc_c"
  1029. rm -f _shar_wnt_.tmp
  1030. fi
  1031. # ============= gammaf.c ==============
  1032. if test -f 'gammaf.c' -a X"$1" != X"-c"; then
  1033.     echo 'x - skipping gammaf.c (File already exists)'
  1034.     rm -f _shar_wnt_.tmp
  1035. else
  1036. > _shar_wnt_.tmp
  1037. echo 'x - extracting gammaf.c (Text)'
  1038. sed 's/^X//' << 'SHAR_EOF' > 'gammaf.c' &&
  1039. #ifndef lint
  1040. static char sccsid[] = "@(#)gamma.c    1.6    (ucb.beef)    10/3/89";
  1041. #endif    /* !defined(lint) */
  1042. /* 
  1043. X *  This routine calculates the GAMMA function for a "double" argument X.
  1044. X *    Computation is based on an algorithm outlined in reference 1.
  1045. X *    The program uses rational functions that approximate the GAMMA
  1046. X *    function to at least 20 significant decimal digits.  Coefficients
  1047. X *    for the approximation over the interval (1,2) are unpublished.
  1048. X *    Those for the approximation for X .GE. 12 are from reference 2.
  1049. X *    The accuracy achieved depends on the arithmetic system, the
  1050. X *    compiler, the intrinsic functions, and proper selection of the
  1051. X *    machine-dependent constants.
  1052. X * 
  1053. X * 
  1054. X *********************************************************************
  1055. X *********************************************************************
  1056. X * 
  1057. X *  Explanation of machine-dependent constants
  1058. X * 
  1059. X *  beta   - radix for the floating-point representation
  1060. X *  maxexp - the smallest positive power of beta that overflows
  1061. X *  XBIG   - the largest argument for which GAMMA(X) is representable
  1062. X *           in the machine, i.e., the solution to the equation
  1063. X *                   GAMMA(XBIG) = beta**maxexp
  1064. X *  XINF   - the largest machine representable floating-point number;
  1065. X *           approximately beta**maxexp
  1066. X *  EPS    - the smallest positive floating-point number such that
  1067. X *           1.0+EPS .GT. 1.0
  1068. X *  XMININ - the smallest positive floating-point number such that
  1069. X *           1/XMININ is machine representable
  1070. X * 
  1071. X *      Approximate values for some important machines are:
  1072. X * 
  1073. X *                             beta       maxexp        XBIG
  1074. X * 
  1075. X *  CRAY-1         (S.P.)        2         8191        967.095
  1076. X *  Cyber 180/855
  1077. X *    under NOS    (S.P.)        2         1070        177.980
  1078. X *  IEEE (IBM/XT,
  1079. X *    SUN, etc.)   (S.P.)        2          128        35.299
  1080. X *  IEEE (IBM/XT,
  1081. X *    SUN, etc.)   (D.P.)        2         1024        171.624
  1082. X *  IBM 3033       (D.P.)       16           63        57.801
  1083. X *  VAX D-Format   (D.P.)        2          127        34.844
  1084. X *  VAX G-Format   (D.P.)        2         1023        171.668
  1085. X * 
  1086. X *                             XINF         EPS        XMININ
  1087. X * 
  1088. X *  CRAY-1         (S.P.)   5.45E+2465   7.11E-15    1.84E-2466
  1089. X *  Cyber 180/855
  1090. X *    under NOS    (S.P.)   1.26E+322    3.55E-15    3.14E-294
  1091. X *  IEEE (IBM/XT,
  1092. X *    SUN, etc.)   (S.P.)   3.40E+38     1.19E-7     1.18E-38
  1093. X *  IEEE (IBM/XT,
  1094. X *    SUN, etc.)   (D.P.)   1.79D+308    2.22D-16    2.23D-308
  1095. X *  IBM 3033       (D.P.)   7.23D+75     2.22D-16    1.39D-76
  1096. X *  VAX D-Format   (D.P.)   1.70D+38     1.39D-17    5.88D-39
  1097. X *  VAX G-Format   (D.P.)   8.98D+307    1.11D-16    1.12D-308
  1098. X * 
  1099. X *********************************************************************
  1100. X *********************************************************************
  1101. X * 
  1102. X *  Error returns
  1103. X * 
  1104. X *   The program returns the value XINF for singularities or
  1105. X *      when overflow would occur.  The computation is believed
  1106. X *      to be free of underflow and overflow.
  1107. X * 
  1108. X * 
  1109. X *   Intrinsic functions required are:
  1110. X * 
  1111. X *      exp, floor, log, sin
  1112. X * 
  1113. X * 
  1114. X *   References: "An Overview of Software Development for Special
  1115. X *               Functions", W. J. Cody, Lecture Notes in Mathematics,
  1116. X *               506, Numerical Analysis Dundee, 1975, G. A. Watson
  1117. X *               (ed.), Springer Verlag, Berlin, 1976.
  1118. X * 
  1119. X *               Computer Approximations, Hart, Et. Al., Wiley and
  1120. X *               sons, New York, 1968.
  1121. X * 
  1122. X *   Latest modification: May 30, 1989
  1123. X * 
  1124. X *   Authors: W. J. Cody and L. Stoltz
  1125. X *            Applied Mathematics Division
  1126. X *            Argonne National Laboratory
  1127. X *            Argonne, IL 60439
  1128. X */
  1129. X
  1130. #include "fpumath.h"
  1131. X
  1132. X                    /* Machine dependent parameters */
  1133. #if defined(vax) || defined(tahoe)
  1134. #define    XBIG    (double)34.844e0
  1135. #define    XMININ    (double)5.88e-39
  1136. #define    EPS    (double)1.39e-17
  1137. #define    XINF    (double)1.70e38
  1138. #else    /* defined(vax) || defined(tahoe) */
  1139. #define    XBIG    (double)171.624e0
  1140. #define    XMININ    (double)2.23e-308
  1141. #define    EPS    (double)2.22e-16
  1142. #define    XINF    MAXFLOAT
  1143. #endif    /* defined(vax) || defined(tahoe) */
  1144. X                    /* Mathematical constants */
  1145. #define    ONE    (double)1
  1146. #define    HALF    (double)0.5
  1147. #define    TWELVE    (double)12
  1148. #define    ZERO    (double)0
  1149. #define    SQRTPI    (double)0.9189385332046727417803297e0
  1150. #define    PI    (double)3.1415926535897932384626434e0
  1151. X
  1152. typedef int logical;
  1153. #define FALSE    (logical)0
  1154. #define    TRUE    (logical)1
  1155. X
  1156. /*
  1157. X *   Numerator and denominator coefficients for rational minimax
  1158. X *      approximation over (1,2).
  1159. X */
  1160. static double P[] = {
  1161. X    -1.71618513886549492533811e0,
  1162. X     2.47656508055759199108314e1,
  1163. X    -3.79804256470945635097577e2,
  1164. X     6.29331155312818442661052e2,
  1165. X     8.66966202790413211295064e2,
  1166. X    -3.14512729688483675254357e4,
  1167. X    -3.61444134186911729807069e4,
  1168. X     6.64561438202405440627855e4,
  1169. };
  1170. static double Q[] = {
  1171. X    -3.08402300119738975254353e1,
  1172. X     3.15350626979604161529144e2,
  1173. X    -1.01515636749021914166146e3,
  1174. X    -3.10777167157231109440444e3,
  1175. X     2.25381184209801510330112e4,
  1176. X     4.75584627752788110767815e3,
  1177. X    -1.34659959864969306392456e5,
  1178. X    -1.15132259675553483497211e5,
  1179. };
  1180. X
  1181. /*
  1182. X *   Coefficients for minimax approximation over (12, INF).
  1183. X */
  1184. static double C[] = {
  1185. X    -1.910444077728e-03,
  1186. X     8.4171387781295e-04,
  1187. X    -5.952379913043012e-04,
  1188. X     7.93650793500350248e-04,
  1189. X    -2.777777777777681622553e-03,
  1190. X     8.333333333333333331554247e-02,
  1191. X     5.7083835261e-03,
  1192. };
  1193. X
  1194. float
  1195. gammaf(float x)
  1196. {
  1197. X    register i,n;
  1198. X    logical parity;
  1199. X    double fact,res,xden,xnum,dtmp1,dtmp2;
  1200. X
  1201. X    parity = FALSE;
  1202. X    fact = ONE;
  1203. X    n = 0;
  1204. #define    y x
  1205. X    if (y <= ZERO) {            /* Arg. is negative */
  1206. X        y = -x;
  1207. #define    y1 dtmp1
  1208. X        y1 = floor(y);
  1209. X        res = y-y1;
  1210. X        if (res != ZERO) {
  1211. X            parity = floor(y1/2)*2 != y1;
  1212. X            fact = -PI/sin(PI*res);
  1213. X            y += ONE;
  1214. X        }
  1215. X        else
  1216. X            return XINF;
  1217. X    }
  1218. X                        /* Arg. is positive */
  1219. X    if (y < EPS) {                /* Arg. < EPS */
  1220. X        if (y >= XMININ)
  1221. X            res = ONE/y;
  1222. X        else
  1223. X          return XINF;
  1224. X    }
  1225. X    else if (y < TWELVE) {
  1226. #define    y1 dtmp1
  1227. #define    z dtmp2
  1228. X        y1 = y;
  1229. X        if (y < ONE) {            /* 0.0 < arg. < 1.0 */
  1230. X            z = y;
  1231. X            y += ONE;
  1232. X        }
  1233. X        else {                /* 1.0 < arg. < 12.0 */
  1234. X
  1235. X            n = (int)y; n--;
  1236. X            y -= (double)n;        /* reduce arg. if needed */
  1237. X            z = y-ONE;
  1238. X        }
  1239. X            /* Evaluate approximation for 1.0 < arg. < 2.0 */
  1240. X        xnum = ZERO;
  1241. X        xden = ONE;
  1242. X        for (i = 0; i <= 7; i++) {
  1243. X            xnum = (xnum+P[i])*z;
  1244. X            xden = xden*z+Q[i];
  1245. X        }
  1246. X        res = xnum/xden+ONE;
  1247. X        if (y1 < y)        /* Adjust res for 0.0 < arg. < 1.0 */
  1248. X            res /= y1;
  1249. X        else if (y1 > y) {    /* Adjust res for 2.0 < arg. < 12.0 */
  1250. X            for (i = 0; i <= n-1; i++) {
  1251. X                res *= y;
  1252. X                y += ONE;
  1253. X            }
  1254. X        }
  1255. #undef    z
  1256. #undef    y1
  1257. X    }
  1258. X    else {                    /* Evaluate for arg. >= 12.0 */
  1259. X        if (y <= XBIG) {
  1260. #define    ysq dtmp1
  1261. #define    sum dtmp2
  1262. X            ysq = y*y;
  1263. X            sum = C[6];
  1264. X            for (i = 0; i <= 5; i++)
  1265. X                sum = sum/ysq+C[i];
  1266. X            sum = sum/y-y; sum += SQRTPI;
  1267. X            sum += (y-HALF)*log(y);
  1268. X            res = exp(sum);
  1269. #undef    sum
  1270. #undef    ysq
  1271. X        }
  1272. X        else
  1273. X            return XINF;
  1274. X    }
  1275. X                        /* Final adjustments and return */
  1276. X    if (parity == TRUE)
  1277. X        res = -res;
  1278. X    if (fact != ONE)
  1279. X        res = fact/res;
  1280. X    return ((float)res);
  1281. #undef    y
  1282. }
  1283. SHAR_EOF
  1284. chmod 0644 gammaf.c ||
  1285. echo 'restore of gammaf.c failed'
  1286. Wc_c="`wc -c < 'gammaf.c'`"
  1287. test 6996 -eq "$Wc_c" ||
  1288.     echo 'gammaf.c: original size 6996, current size' "$Wc_c"
  1289. rm -f _shar_wnt_.tmp
  1290. fi
  1291. # ============= j1f.c ==============
  1292. if test -f 'j1f.c' -a X"$1" != X"-c"; then
  1293.     echo 'x - skipping j1f.c (File already exists)'
  1294.     rm -f _shar_wnt_.tmp
  1295. else
  1296. > _shar_wnt_.tmp
  1297. echo 'x - extracting j1f.c (Text)'
  1298. sed 's/^X//' << 'SHAR_EOF' > 'j1f.c' &&
  1299. #ifndef lint
  1300. static char sccsid[] = "@(#)j1.c    1.1    (ucb.beef)    10/1/89";
  1301. #endif    /* !defined(lint) */
  1302. /* 
  1303. X *  This packet computes first-order Bessel functions of the first and
  1304. X *    second kind (j1 and y1), for real arguments x, where 0 < x <= XMAX
  1305. X *    for y1, and |x| <= XMAX for j1.  It contains three function-type
  1306. X *    subprograms, j1, y1 and caljy1.  The calling statements for the
  1307. X *    primary entries are:
  1308. X * 
  1309. X *            y = j1(x)
  1310. X *    and
  1311. X *            y = y1(x),
  1312. X * 
  1313. X *    where the entry points correspond to the functions j1(x) and y1(x),
  1314. X *    respectively.  The routine caljy1() is intended for internal packet
  1315. X *    use only, all computations within the packet being concentrated in
  1316. X *    this one routine.  The function subprograms invoke  caljy1  with
  1317. X *    the statement
  1318. X *
  1319. X *            result = caljy1(x,jint);
  1320. X *
  1321. X *    where the parameter usage is as follows:
  1322. X * 
  1323. X *       Function                  Parameters for caljy1
  1324. X *        call              x             result          jint
  1325. X * 
  1326. X *       j1(x)       |x| <= XMAX          j1(x)           0
  1327. X *       y1(x)     0 < x <= XMAX          y1(x)           1
  1328. X * 
  1329. X *    The main computation uses unpublished minimax rational
  1330. X *    approximations for x <= 8.0, and an approximation from the 
  1331. X *    book  Computer Approximations  by Hart, et. al., Wiley and Sons, 
  1332. X *    New York, 1968, for arguments larger than 8.0   Part of this
  1333. X *    transportable packet is patterned after the machine-dependent
  1334. X *    FUNPACK program for j1(x), but cannot match that version for
  1335. X *    efficiency or accuracy.  This version uses rational functions
  1336. X *    that are theoretically accurate to at least 18 significant decimal
  1337. X *    digits for x <= 8, and at least 18 decimal places for x > 8.  The
  1338. X *    accuracy achieved depends on the arithmetic system, the compiler,
  1339. X *    the intrinsic functions, and proper selection of the machine-
  1340. X *    dependent constants.
  1341. X * 
  1342. X *********************************************************************
  1343. X * 
  1344. X *  Explanation of machine-dependent constants
  1345. X * 
  1346. X *    XINF   = largest positive machine number
  1347. X *    XMAX   = largest acceptable argument.  The functions floor(), sin()
  1348. X *             and cos() must perform properly for  fabs(x) <= XMAX.
  1349. X *             We recommend that XMAX be a small integer multiple of
  1350. X *             sqrt(1/eps), where eps is the smallest positive number
  1351. X *             such that  1+eps > 1. 
  1352. X *    XSMALL = positive argument such that  1.0-(1/2)(x/2)**2 = 1.0
  1353. X *             to machine precision for all  fabs(x) <= XSMALL.
  1354. X *             We recommend that  XSMALL < sqrt(eps)/beta, where beta
  1355. X *             is the floating-point radix (usually 2 or 16).
  1356. X * 
  1357. X *      Approximate values for some important machines are
  1358. X * 
  1359. X *                           eps      XMAX     XSMALL      XINF  
  1360. X * 
  1361. X *   CDC 7600      (S.P.)  7.11E-15  1.34E+08  2.98E-08  1.26E+322
  1362. X *   CRAY-1        (S.P.)  7.11E-15  1.34E+08  2.98E-08  5.45E+2465
  1363. X *   IBM PC (8087) (S.P.)  5.96E-08  8.19E+03  1.22E-04  3.40E+38
  1364. X *   IBM PC (8087) (D.P.)  1.11e-16  2.68e08  3.72e-09  1.79e308
  1365. X *   IBM 195       (D.P.)  2.22e-16  6.87e09  9.09e-13  7.23e75
  1366. X *   UNIVAC 1108   (D.P.)  1.73e-18  4.30e09  2.33e-10  8.98e307
  1367. X *   VAX 11/780    (D.P.)  1.39e-17  1.07e09  9.31e-10  1.70e38
  1368. X * 
  1369. X *********************************************************************
  1370. X *********************************************************************
  1371. X * 
  1372. X *  Error Returns
  1373. X * 
  1374. X *   The program returns the value zero for  x > XMAX, and returns
  1375. X *     -XINF when BESLY1 is called with a negative or zero argument.
  1376. X * 
  1377. X * 
  1378. X *  Intrinsic functions required are:
  1379. X * 
  1380. X *      cos, fabs, floor, log, sin, sqrt
  1381. X * 
  1382. X * 
  1383. X *   Author: w. J. Cody
  1384. X *           Mathematics and Computer Science Division 
  1385. X *           Argonne National Laboratory
  1386. X *           Argonne, IL 60439
  1387. X * 
  1388. X *   Latest modification: November 10, 1987
  1389. X */
  1390. X
  1391. #include "fpumath.h"
  1392. X
  1393. X                    /* Machine-dependent constants */
  1394. #if defined(vax) || defined(tahoe)
  1395. #define    XMAX    (double)1.07e9
  1396. #define    XSMALL    (double)9.31e-10
  1397. #define    XINF    (double)1.7e38
  1398. #else    /* defined(vax) || defined(tahoe) */
  1399. #define    XMAX    (double)2.68e08
  1400. #define    XSMALL    (double)3.72e-09
  1401. #define    XINF    MAXFLOAT
  1402. #endif    /* defined(vax) || defined(tahoe) */
  1403. X                    /* Mathematical constants */
  1404. #define    EIGHT    (double)8
  1405. #define    FOUR    (double)4
  1406. #define    HALF    (double)0.5
  1407. #define    THROV8    (double)0.375
  1408. #define    PI2    (double)6.3661977236758134308e-1
  1409. #define    P17    (double)1.716e-1
  1410. #define    TWOPI    (double)6.2831853071795864769e0
  1411. #define    ZERO    (double)0
  1412. #define    TWOPI1    (double)6.28125
  1413. #define    TWOPI2    (double)1.9353071795864769253e-03
  1414. #define    TWO56    (double)256
  1415. #define    RTPI2    (double)7.9788456080286535588e-1
  1416. X                    /* Zeroes of Bessel functions */
  1417. #define    XJ0    (double)3.8317059702075123156e0
  1418. #define    XJ1    (double)7.0155866698156187535e0
  1419. #define    XY0    (double)2.1971413260310170351e0
  1420. #define    XY1    (double)5.4296810407941351328e0
  1421. #define    XJ01    (double)981
  1422. #define    XJ02    (double)(-3.2527979248768438556e-4)
  1423. #define    XJ11    (double)1796
  1424. #define    XJ12    (double)(-3.8330184381246462950e-5)
  1425. #define    XY01    (double)562
  1426. #define    XY02    (double)1.8288260310170351490e-3
  1427. #define    XY11    (double)1390
  1428. #define    XY12    (double)(-6.4592058648672279948e-6)
  1429. X
  1430. /*
  1431. X * Coefficients for rational approximation to ln(x/a)
  1432. X */
  1433. static double PLG[] = {
  1434. X    -2.4562334077563243311e01,
  1435. X     2.3642701335621505212e02,
  1436. X    -5.4989956895857911039e02,
  1437. X     3.5687548468071500413e02,
  1438. };
  1439. static double QLG[] = {
  1440. X    -3.5553900764052419184e01,
  1441. X     1.9400230218539473193e02,
  1442. X    -3.3442903192607538956e02,
  1443. X     1.7843774234035750207e02,
  1444. };
  1445. X
  1446. /*
  1447. X * Coefficients for rational approximation of
  1448. X * j1(x) / (x * (x**2 - XJ0**2)),  XSMALL  <  |x|  <=  4.0
  1449. X */
  1450. static double PJ0[] = {
  1451. X     9.8062904098958257677e05,
  1452. X    -1.1548696764841276794e08,
  1453. X     6.6781041261492395835e09,
  1454. X    -1.4258509801366645672e11,
  1455. X    -4.4615792982775076130e03,
  1456. X     1.0650724020080236441e01,
  1457. X    -1.0767857011487300348e-02,
  1458. };
  1459. static double QJ0[] = {
  1460. X    5.9117614494174794095e05,
  1461. X    2.0228375140097033958e08,
  1462. X    4.2091902282580133541e10,
  1463. X    4.1868604460820175290e12,
  1464. X    1.0742272239517380498e03,
  1465. };
  1466. X
  1467. /*
  1468. X * Coefficients for rational approximation of
  1469. X * j1(x) / (x * (x**2 - XJ1**2)),  4.0  <  |x|  <=  8.0
  1470. X */
  1471. static double PJ1[] = {
  1472. X     4.6179191852758252280e00,
  1473. X    -7.1329006872560947377e03,
  1474. X     4.5039658105749078904e06,
  1475. X    -1.4437717718363239107e09,
  1476. X     2.3569285397217157313e11,
  1477. X    -1.6324168293282543629e13,
  1478. X     1.1357022719979468624e14,
  1479. X     1.0051899717115285432e15,
  1480. };
  1481. static double QJ1[] = {
  1482. X    1.1267125065029138050e06,
  1483. X    6.4872502899596389593e08,
  1484. X    2.7622777286244082666e11,
  1485. X    8.4899346165481429307e13,
  1486. X    1.7128800897135812012e16,
  1487. X    1.7253905888447681194e18,
  1488. X    1.3886978985861357615e03,
  1489. };
  1490. X
  1491. /*
  1492. X * Coefficients for rational approximation of
  1493. X *   (y1(x) - 2 LN(x/XY0) j1(x)) / (x**2 - XY0**2),
  1494. X *       XSMALL  <  |x|  <=  4.0
  1495. X */
  1496. static double PY0[] = {
  1497. X     2.2157953222280260820e05,
  1498. X    -5.9157479997408395984e07,
  1499. X     7.2144548214502560419e09,
  1500. X    -3.7595974497819597599e11,
  1501. X     5.4708611716525426053e12,
  1502. X     4.0535726612579544093e13,
  1503. X    -3.1714424660046133456e02,
  1504. };
  1505. static double QY0[] = {
  1506. X    8.2079908168393867438e02,
  1507. X    3.8136470753052572164e05,
  1508. X    1.2250435122182963220e08,
  1509. X    2.7800352738690585613e10,
  1510. X    4.1272286200406461981e12,
  1511. X    3.0737873921079286084e14,
  1512. };
  1513. X
  1514. /*
  1515. X * Coefficients for rational approximation of
  1516. X *   (y1(x) - 2 LN(x/XY1) j1(x)) / (x**2 - XY1**2),
  1517. X *        .0  <  |x|  <=  8.0
  1518. X */
  1519. static double PY1[] = {
  1520. X     1.9153806858264202986e06,
  1521. X    -1.1957961912070617006e09,
  1522. X     3.7453673962438488783e11,
  1523. X    -5.9530713129741981618e13,
  1524. X     4.0686275289804744814e15,
  1525. X    -2.3638408497043134724e16,
  1526. X    -5.6808094574724204577e18,
  1527. X     1.1514276357909013326e19,
  1528. X    -1.2337180442012953128e03,
  1529. };
  1530. static double QY1[] = {
  1531. X    1.2855164849321609336e03,
  1532. X    1.0453748201934079734e06,
  1533. X    6.3550318087088919566e08,
  1534. X    3.0221766852960403645e11,
  1535. X    1.1187010065856971027e14,
  1536. X    3.0837179548112881950e16,
  1537. X    5.6968198822857178911e18,
  1538. X    5.3321844313316185697e20,
  1539. };
  1540. X
  1541. /*
  1542. X * Coefficients for Hart,s approximation,  |x| > 8.0
  1543. X */
  1544. static double P0[] = {
  1545. X    -1.0982405543459346727e05,
  1546. X    -1.5235293511811373833e06,
  1547. X    -6.6033732483649391093e06,
  1548. X    -9.9422465050776411957e06,
  1549. X    -4.4357578167941278571e06,
  1550. X    -1.6116166443246101165e03,
  1551. };
  1552. static double Q0[] = {
  1553. X    -1.0726385991103820119e05,
  1554. X    -1.5118095066341608816e06,
  1555. X    -6.5853394797230870728e06,
  1556. X    -9.9341243899345856590e06,
  1557. X    -4.4357578167941278568e06,
  1558. X    -1.4550094401904961825e03,
  1559. };
  1560. static double P1[] = {
  1561. X    1.7063754290207680021e03,
  1562. X    1.8494262873223866797e04,
  1563. X    6.6178836581270835179e04,
  1564. X    8.5145160675335701966e04,
  1565. X    3.3220913409857223519e04,
  1566. X    3.5265133846636032186e01,
  1567. };
  1568. static double Q1[] = {
  1569. X    3.7890229745772202641e04,
  1570. X    4.0029443582266975117e05,
  1571. X    1.4194606696037208929e06,
  1572. X    1.8194580422439972989e06,
  1573. X    7.0871281941028743574e05,
  1574. X    8.6383677696049909675e02,
  1575. };
  1576. X
  1577. static double
  1578. #if defined(__STDC__) || defined(__GNUC__)
  1579. caljy1(double x, int jint)
  1580. #else
  1581. caljy1(x,jint)
  1582. double x;
  1583. int jint;
  1584. #endif
  1585. {
  1586. X    int i;
  1587. X    double ax,resj,down,up,xden,xnum,w,wsq,z,zsq;
  1588. X
  1589. X    ax = fabs(x);                /* Check for error conditions */
  1590. X    if (jint && (x <= ZERO || x < HALF && ax*XINF < PI2))
  1591. X        return -XINF;
  1592. X    else if (ax > XMAX)
  1593. X        return ZERO;
  1594. /*
  1595. X * Calculate j1 or y1 for |x|  >  8.0
  1596. X */
  1597. X    if (ax > EIGHT) {
  1598. X        z = EIGHT/ax;
  1599. X        w = floor(ax/TWOPI)+THROV8;
  1600. X        w = (ax-w*TWOPI1)-w*TWOPI2;
  1601. X        zsq = z*z;
  1602. X        xnum = P0[5];
  1603. X        xden = zsq+Q0[5];
  1604. X        up = P1[5];
  1605. X        down = zsq+Q1[5];
  1606. X        for (i = 0; i <= 4; i++) {
  1607. X            xnum = xnum*zsq+P0[i];
  1608. X            xden = xden*zsq+Q0[i];
  1609. X            up = up*zsq+P1[i];
  1610. X            down = down*zsq+Q1[i];
  1611. X        }
  1612. #define    r0 xnum
  1613. #define    r1 up
  1614. X        r0 = xnum/xden;
  1615. X        r1 = up/down;
  1616. X        return RTPI2/sqrt(ax)*(jint ? r0*sin(w)+z*r1*cos(w) :
  1617. X            (x < ZERO ? z*r1*sin(w)-r0*cos(w) :
  1618. X            r0*cos(w)-z*r1*sin(w)));
  1619. #undef    r1
  1620. #undef    r0
  1621. X    }
  1622. X    else if (ax <= XSMALL)
  1623. X        return jint ? -PI2/ax : x*HALF;
  1624. /*
  1625. X * Calculate j1 for appropriate interval, preserving
  1626. X *    accuracy near the zero of j1
  1627. X */
  1628. X    zsq = ax*ax;
  1629. X    if (ax <= FOUR) {
  1630. X        xnum = (PJ0[6]*zsq+PJ0[5])*zsq+PJ0[4];
  1631. X        xden = zsq+QJ0[4];
  1632. X        for (i = 0; i <= 3; i++) {
  1633. X            xnum = xnum*zsq+PJ0[i];
  1634. X            xden = xden*zsq+QJ0[i];
  1635. X        }
  1636. #define    prod resj
  1637. X        prod = x*((ax-XJ01/TWO56)-XJ02)*(ax+XJ0);
  1638. X    }
  1639. X    else {
  1640. X        xnum = PJ1[0];
  1641. X        xden = (zsq+QJ1[6])*zsq+QJ1[0];
  1642. X        for (i = 1; i <= 5; i++) {
  1643. X            xnum = xnum*zsq+PJ1[i];
  1644. X            xden = xden*zsq+QJ1[i];
  1645. X        }
  1646. X        xnum = xnum*(ax-EIGHT)*(ax+EIGHT)+PJ1[6];
  1647. X        xnum = xnum*(ax-FOUR)*(ax+FOUR)+PJ1[7];
  1648. X        prod = x*((ax-XJ11/TWO56)-XJ12)*(ax+XJ1);
  1649. X    }
  1650. #define    result resj
  1651. X    result = prod*(xnum/xden);
  1652. #undef    prod
  1653. X    if (!jint)
  1654. X        return result;
  1655. /*
  1656. X * Calculate y1.  First find  resj = pi/2 ln(x/xn) j1(x),
  1657. X *   where xn is a zero of y1
  1658. X */
  1659. #define    xy z
  1660. X    if (ax <= FOUR) {
  1661. X        up = (ax-XY01/TWO56)-XY02;
  1662. X        xy = XY0;
  1663. X    }
  1664. X    else {
  1665. X        up = (ax-XY11/TWO56)-XY12;
  1666. X        xy = XY1;
  1667. X    }
  1668. X    down = ax+xy;
  1669. X    if (fabs(up) < P17*down) {
  1670. X        w = up/down;
  1671. X        wsq = w*w;
  1672. X        xnum = PLG[0];
  1673. X        xden = wsq+QLG[0];
  1674. X        for (i = 1; i <= 3; i++) {
  1675. X            xnum = xnum*wsq+PLG[i];
  1676. X            xden = xden*wsq+QLG[i];
  1677. X        }
  1678. X        resj = PI2*result*w*xnum/xden;
  1679. X    }
  1680. X    else
  1681. X        resj = PI2*result*log(ax/xy);
  1682. #undef    xy
  1683. #undef    result
  1684. /*
  1685. X * Now calculate y1 for appropriate interval, preserving
  1686. X *    accuracy near the zero of y1
  1687. X */
  1688. X    if (ax <= FOUR) {
  1689. X        xnum = PY0[6]*zsq+PY0[0];
  1690. X        xden = zsq+QY0[0];
  1691. X        for (i = 1; i <= 5; i++) {
  1692. X            xnum = xnum*zsq+PY0[i];
  1693. X            xden = xden*zsq+QY0[i];
  1694. X        }
  1695. X    }
  1696. X    else {
  1697. X        xnum = PY1[8]*zsq+PY1[0];
  1698. X        xden = zsq+QY1[0];
  1699. X        for (i = 1; i <= 7; i++) {
  1700. X            xnum = xnum*zsq+PY1[i];
  1701. X            xden = xden*zsq+QY1[i];
  1702. X        }
  1703. X    }
  1704. X    up *= down; up /= ax; up *= xnum; up /= xden; up += resj;
  1705. X    return up;
  1706. }
  1707. X
  1708. /*
  1709. X * This subprogram computes approximate values for Bessel functions
  1710. X *   of the first kind of order zero for arguments  |x| <= XMAX
  1711. X *   (see comments heading caljy1).
  1712. X */
  1713. float
  1714. j1f(float x)
  1715. {
  1716. X    return ((float)caljy1(x,0));
  1717. }
  1718. X
  1719. /*
  1720. X * This subprogram computes approximate values for Bessel functions
  1721. X *   of the second kind of order zero for arguments 0 < x <= XMAX
  1722. X *   (see comments heading caljy1).
  1723. X */
  1724. float
  1725. y1f(float x)
  1726. {
  1727. X    return ((float)caljy1(x,1));
  1728. }
  1729. SHAR_EOF
  1730. chmod 0644 j1f.c ||
  1731. echo 'restore of j1f.c failed'
  1732. Wc_c="`wc -c < 'j1f.c'`"
  1733. test 11769 -eq "$Wc_c" ||
  1734.     echo 'j1f.c: original size 11769, current size' "$Wc_c"
  1735. rm -f _shar_wnt_.tmp
  1736. fi
  1737. # ============= erff.c ==============
  1738. if test -f 'erff.c' -a X"$1" != X"-c"; then
  1739.     echo 'x - skipping erff.c (File already exists)'
  1740.     rm -f _shar_wnt_.tmp
  1741. else
  1742. > _shar_wnt_.tmp
  1743. echo 'x - extracting erff.c (Text)'
  1744. sed 's/^X//' << 'SHAR_EOF' > 'erff.c' &&
  1745. #ifndef lint
  1746. static char sccsid[] = "@(#)erf.c    1.3    (ucb.beef)    10/2/89";
  1747. #endif    /* !defined(lint) */
  1748. /* 
  1749. X * This packet evaluates erf(x), erfc(x), and exp(x*x)*erfc(x)
  1750. X *   for a "float" argument x.  It contains three subprograms:
  1751. X *   erff(), erfcf(), and erfcxf() and * one "static" subprogram,
  1752. X *   calerf.  The calling statements for the primary entries are:
  1753. X * 
  1754. X *                   y = erf(x),
  1755. X * 
  1756. X *                   y = erfc(x),
  1757. X *   and
  1758. X *                   y = erfcx(x).
  1759. X * 
  1760. X *   The routine calerf() is intended for internal packet use only,
  1761. X *   all computations within the packet being concentrated in this
  1762. X *   routine.  The 3 primary subprograms invoke calerf with the
  1763. X *   statement
  1764. X * 
  1765. X *          result = calerf(arg,jint);
  1766. X * 
  1767. X *   where the parameter usage is as follows
  1768. X * 
  1769. X *      Function                     Parameters for calerf
  1770. X *       call              arg                  result          jint
  1771. X * 
  1772. X *     erf(arg)      ANY "double" ARGUMENT      erf(arg)          0
  1773. X *     erfc(arg)     fabs(arg) < XBIG           erfc(arg)         1
  1774. X *     erfcx(arg)    XNEG < arg < XMAX          erfcx(arg)        2
  1775. X * 
  1776. X *   The main computation evaluates near-minimax approximations
  1777. X *   from "Rational Chebyshev approximations for the error function"
  1778. X *   by W. J. Cody, Math. Comp., 1969, PP. 631-638.  This
  1779. X *   transportable program uses rational functions that theoretically
  1780. X *   approximate  erf(x)  and  erfc(x)  to at least 18 significant
  1781. X *   decimal digits.  The accuracy achieved depends on the arithmetic
  1782. X *   system, the compiler, the intrinsic functions, and proper
  1783. X *   selection of the machine-dependent constants.
  1784. X * 
  1785. X ********************************************************************
  1786. X ********************************************************************
  1787. X * 
  1788. X * Explanation of machine-dependent constants
  1789. X * 
  1790. X *   XMIN   = the smallest positive floating-point number.
  1791. X *   XINF   = the largest positive finite floating-point number.
  1792. X *   XNEG   = the largest negative argument acceptable to erfcx;
  1793. X *            the negative of the solution to the equation
  1794. X *            2*exp(x*x) = XINF.
  1795. X *   XSMALL = argument below which erf(x) may be represented by
  1796. X *            2*x/sqrt(pi)  and above which  x*x  will not underflow.
  1797. X *            A conservative value is the largest machine number x
  1798. X *            such that   1.0 + x = 1.0   to machine precision.
  1799. X *   XBIG   = largest argument acceptable to erfc;  solution to
  1800. X *            the equation:  W(x) * (1-0.5/x**2) = XMIN,  where
  1801. X *            W(x) = exp(-x*x)/[x*sqrt(pi)].
  1802. X *   XHUGE  = argument above which  1.0 - 1/(2*x*x) = 1.0  to
  1803. X *            machine precision.  A conservative value is
  1804. X *            1/[2*sqrt(XSMALL)]
  1805. X *   XMAX   = largest acceptable argument to erfcx; the minimum
  1806. X *            of XINF and 1/[sqrt(pi)*XMIN].
  1807. X * 
  1808. X *   Approximate values for some important machines are:
  1809. X * 
  1810. X *                          XMIN       XINF        XNEG     XSMALL
  1811. X * 
  1812. X *  CDC 7600      (S.P.)  3.13E-294   1.26E+322   -27.220  7.11E-15
  1813. X *  CRAY-1        (S.P.)  4.58E-2467  5.45E+2465  -75.345  7.11E-15
  1814. X *  IEEE (IBM/XT,
  1815. X *    SUN, etc.)  (S.P.)  1.18E-38    3.40E+38     -9.382  5.96E-8
  1816. X *  IEEE (IBM/XT,
  1817. X *    SUN, etc.)  (D.P.)  2.23D-308   1.79D+308   -26.628  1.11D-16
  1818. X *  IBM 195       (D.P.)  5.40D-79    7.23E+75    -13.190  1.39D-17
  1819. X *  UNIVAC 1108   (D.P.)  2.78D-309   8.98D+307   -26.615  1.73D-18
  1820. X *  VAX D-Format  (D.P.)  2.94D-39    1.70D+38     -9.345  1.39D-17
  1821. X *  VAX G-Format  (D.P.)  5.56D-309   8.98D+307   -26.615  1.11D-16
  1822. X * 
  1823. X * 
  1824. X *                          XBIG       XHUGE       XMAX
  1825. X * 
  1826. X *  CDC 7600      (S.P.)  25.922      8.39E+6     1.80X+293
  1827. X *  CRAY-1        (S.P.)  75.326      8.39E+6     5.45E+2465
  1828. X *  IEEE (IBM/XT,
  1829. X *    SUN, etc.)  (S.P.)   9.194      2.90E+3     4.79E+37
  1830. X *  IEEE (IBM/XT,
  1831. X *    SUN, etc.)  (D.P.)  26.543      6.71D+7     2.53D+307
  1832. X *  IBM 195       (D.P.)  13.306      1.90D+8     7.23E+75
  1833. X *  UNIVAC 1108   (D.P.)  26.582      5.37D+8     8.98D+307
  1834. X *  VAX D-Format  (D.P.)   9.269      1.90D+8     1.70D+38
  1835. X *  VAX G-Format  (D.P.)  26.569      6.71D+7     8.98D+307
  1836. X * 
  1837. X ********************************************************************
  1838. X ********************************************************************
  1839. X * 
  1840. X * Error returns
  1841. X * 
  1842. X *  The program returns  erfc = 0      for  arg .GE. XBIG;
  1843. X * 
  1844. X *                       erfcx = XINF  for  arg .LT. XNEG;
  1845. X *      and
  1846. X *                       erfcx = 0     for  arg .GE. XMAX.
  1847. X * 
  1848. X * 
  1849. X * Intrinsic functions required are:
  1850. X * 
  1851. X *     fabs, exp
  1852. X * 
  1853. X * 
  1854. X *  Author: W. J. Cody
  1855. X *          Mathematics and Computer Science Division
  1856. X *          Argonne National Laboratory
  1857. X *          Argonne, IL 60439
  1858. X * 
  1859. X *  Latest modification: June 16, 1988
  1860. X */
  1861. X
  1862. #include "fpumath.h"
  1863. X
  1864. X                    /* Machine-dependent constants */
  1865. #if defined(vax) || defined(tahoe)
  1866. #define    XINF    (double)1.70e38
  1867. #define    XNEG    (double)(-9.345e0)
  1868. #define    XSMALL    (double)1.39e-17
  1869. #define    XBIG    (double)9.269e0
  1870. #define    XHUGE    (double)1.90e8
  1871. #define    XMAX    (double)1.70e38
  1872. #else    /* defined(vax) || defined(tahoe) */
  1873. #define    XINF    MAXFLOAT
  1874. #define    XNEG    (double)(-26.628e0)
  1875. #define    XSMALL    (double)1.11e-16
  1876. #define    XBIG    (double)26.543e0
  1877. #define    XHUGE    (double)6.71e7
  1878. #define    XMAX    (double)2.53e307
  1879. #endif    /* defined(vax) || defined(tahoe) */
  1880. X                    /* Mathematical constants */
  1881. #define    FOUR    (double)4
  1882. #define    ONE    (double)1
  1883. #define    HALF    (double)0.5
  1884. #define    TWO    (double)2
  1885. #define    ZERO    (double)0
  1886. #define    SQRPI    (double)5.6418958354775628695e-1
  1887. #define    THRESH    (double)0.46875
  1888. #define    SIXTEN    (double)16
  1889. X
  1890. /*
  1891. X * Coefficients for approximation to  erf  in first interval
  1892. X */
  1893. static double A[] = {
  1894. X    3.16112374387056560e00,
  1895. X    1.13864154151050156e02,
  1896. X    3.77485237685302021e02,
  1897. X    3.20937758913846947e03,
  1898. X    1.85777706184603153e-1,
  1899. };
  1900. static double B[] = {
  1901. X    2.36012909523441209e01,
  1902. X    2.44024637934444173e02,
  1903. X    1.28261652607737228e03,
  1904. X    2.84423683343917062e03,
  1905. };
  1906. X
  1907. /*
  1908. X * Coefficients for approximation to  erfc  in second interval
  1909. X */
  1910. static double C[] = {
  1911. X    5.64188496988670089e-1,
  1912. X    8.88314979438837594e0,
  1913. X    6.61191906371416295e01,
  1914. X    2.98635138197400131e02,
  1915. X    8.81952221241769090e02,
  1916. X    1.71204761263407058e03,
  1917. X    2.05107837782607147e03,
  1918. X    1.23033935479799725e03,
  1919. X    2.15311535474403846e-8,
  1920. };
  1921. static double D[] = {
  1922. X    1.57449261107098347e01,
  1923. X    1.17693950891312499e02,
  1924. X    5.37181101862009858e02,
  1925. X    1.62138957456669019e03,
  1926. X    3.29079923573345963e03,
  1927. X    4.36261909014324716e03,
  1928. X    3.43936767414372164e03,
  1929. X    1.23033935480374942e03,
  1930. };
  1931. X
  1932. /*
  1933. X * Coefficients for approximation to  erfc  in third interval
  1934. X */
  1935. static double P[] = {
  1936. X    3.05326634961232344e-1,
  1937. X    3.60344899949804439e-1,
  1938. X    1.25781726111229246e-1,
  1939. X    1.60837851487422766e-2,
  1940. X    6.58749161529837803e-4,
  1941. X    1.63153871373020978e-2,
  1942. };
  1943. static double Q[] = {
  1944. X    2.56852019228982242e00,
  1945. X    1.87295284992346047e00,
  1946. X    5.27905102951428412e-1,
  1947. X    6.05183413124413191e-2,
  1948. X    2.33520497626869185e-3,
  1949. };
  1950. X
  1951. static double
  1952. #if defined(__STDC__) || defined(__GNUC__)
  1953. calerf(double x,int jint)
  1954. #else
  1955. calerf(x,jint)
  1956. double x;
  1957. int jint;
  1958. #endif
  1959. {
  1960. X    register i,skip;
  1961. X    double y,ysq,xnum,xden,result;
  1962. X
  1963. X    y = fabs(x);
  1964. X    if (y <= THRESH) {    /* Evaluate erf for |x| <= 0.46875 */
  1965. X        ysq = y > XSMALL ? y*y : ZERO;
  1966. X        xnum = A[4]*ysq;
  1967. X        xden = ysq;
  1968. X        for (i = 0; i <= 2; i++) {
  1969. X            xnum = (xnum+A[i])*ysq;
  1970. X            xden = (xden+B[i])*ysq;
  1971. X        }
  1972. X        result = x*(xnum+A[3])/(xden+B[3]);
  1973. X        if (jint)
  1974. X            result = ONE-result;
  1975. X        if (jint == 2)
  1976. X            result *= exp(ysq);
  1977. X        return result;
  1978. X    }
  1979. X    else if (y <= FOUR) {    /* Evaluate erfc for 0.46875 <= |x| <= 4.0 */
  1980. X        ysq = y*y;
  1981. X        xnum = C[8]*y;
  1982. X        xden = y;
  1983. X        for (i = 0; i <= 6; i++) {
  1984. X            xnum = (xnum+C[i])*y;
  1985. X            xden = (xden+D[i])*y;
  1986. X        }
  1987. X        result = (xnum+C[7])/(xden+D[7]);
  1988. X        if (jint != 2) {
  1989. X            i = (int)(y*SIXTEN); ysq = (double)i/SIXTEN;
  1990. X            result *= exp(-ysq*ysq)*exp(-(y-ysq)*(y+ysq));
  1991. X        }
  1992. X    }
  1993. X    else {            /* Evaluate erfc for |x| > 4.0 */
  1994. X        result = ZERO;
  1995. X        skip = 0;
  1996. X        if (y >= XBIG) {
  1997. X            if (jint != 2 || y >= XMAX)
  1998. X                skip++;
  1999. X            else if (y >= XHUGE) {
  2000. X                result = SQRPI/y;
  2001. SHAR_EOF
  2002. true || echo 'restore of erff.c failed'
  2003. fi
  2004. echo 'End of mathlib2.0 part 2'
  2005. echo 'File erff.c is continued in part 3'
  2006. echo 3 > _shar_seq_.tmp
  2007. exit 0
  2008. --
  2009. Glenn Geers                       | "So when it's over, we're back to people.
  2010. Department of Theoretical Physics |  Just to prove that human touch can have
  2011. The University of Sydney          |  no equal."
  2012. Sydney NSW 2006 Australia         |  - Basia Trzetrzelewska, 'Prime Time TV'
  2013.