gamma.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. /* gamma.c
  2. *
  3. * Gamma function
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * double x, y, gamma();
  10. * extern int sgngam;
  11. *
  12. * y = gamma( x );
  13. *
  14. *
  15. *
  16. * DESCRIPTION:
  17. *
  18. * Returns gamma function of the argument. The result is
  19. * correctly signed, and the sign (+1 or -1) is also
  20. * returned in a global (extern) variable named sgngam.
  21. * This variable is also filled in by the logarithmic gamma
  22. * function lgam().
  23. *
  24. * Arguments |x| <= 34 are reduced by recurrence and the function
  25. * approximated by a rational function of degree 6/7 in the
  26. * interval (2,3). Large arguments are handled by Stirling's
  27. * formula. Large negative arguments are made positive using
  28. * a reflection formula.
  29. *
  30. *
  31. * ACCURACY:
  32. *
  33. * Relative error:
  34. * arithmetic domain # trials peak rms
  35. * DEC -34, 34 10000 1.3e-16 2.5e-17
  36. * IEEE -170,-33 20000 2.3e-15 3.3e-16
  37. * IEEE -33, 33 20000 9.4e-16 2.2e-16
  38. * IEEE 33, 171.6 20000 2.3e-15 3.2e-16
  39. *
  40. * Error for arguments outside the test range will be larger
  41. * owing to error amplification by the exponential function.
  42. *
  43. */
  44. /* lgam()
  45. *
  46. * Natural logarithm of gamma function
  47. *
  48. *
  49. *
  50. * SYNOPSIS:
  51. *
  52. * double x, y, lgam();
  53. * extern int sgngam;
  54. *
  55. * y = lgam( x );
  56. *
  57. *
  58. *
  59. * DESCRIPTION:
  60. *
  61. * Returns the base e (2.718...) logarithm of the absolute
  62. * value of the gamma function of the argument.
  63. * The sign (+1 or -1) of the gamma function is returned in a
  64. * global (extern) variable named sgngam.
  65. *
  66. * For arguments greater than 13, the logarithm of the gamma
  67. * function is approximated by the logarithmic version of
  68. * Stirling's formula using a polynomial approximation of
  69. * degree 4. Arguments between -33 and +33 are reduced by
  70. * recurrence to the interval [2,3] of a rational approximation.
  71. * The cosecant reflection formula is employed for arguments
  72. * less than -33.
  73. *
  74. * Arguments greater than MAXLGM return MAXNUM and an error
  75. * message. MAXLGM = 2.035093e36 for DEC
  76. * arithmetic or 2.556348e305 for IEEE arithmetic.
  77. *
  78. *
  79. *
  80. * ACCURACY:
  81. *
  82. *
  83. * arithmetic domain # trials peak rms
  84. * DEC 0, 3 7000 5.2e-17 1.3e-17
  85. * DEC 2.718, 2.035e36 5000 3.9e-17 9.9e-18
  86. * IEEE 0, 3 28000 5.4e-16 1.1e-16
  87. * IEEE 2.718, 2.556e305 40000 3.5e-16 8.3e-17
  88. * The error criterion was relative when the function magnitude
  89. * was greater than one but absolute when it was less than one.
  90. *
  91. * The following test used the relative error criterion, though
  92. * at certain points the relative error could be much higher than
  93. * indicated.
  94. * IEEE -200, -4 10000 4.8e-16 1.3e-16
  95. *
  96. */
  97. /* gamma.c */
  98. /* gamma function */
  99. /*
  100. Cephes Math Library Release 2.8: June, 2000
  101. Copyright 1984, 1987, 1989, 1992, 2000 by Stephen L. Moshier
  102. */
  103. #include <math.h>
  104. #ifdef UNK
  105. static double P[] = {
  106. 1.60119522476751861407E-4,
  107. 1.19135147006586384913E-3,
  108. 1.04213797561761569935E-2,
  109. 4.76367800457137231464E-2,
  110. 2.07448227648435975150E-1,
  111. 4.94214826801497100753E-1,
  112. 9.99999999999999996796E-1
  113. };
  114. static double Q[] = {
  115. -2.31581873324120129819E-5,
  116. 5.39605580493303397842E-4,
  117. -4.45641913851797240494E-3,
  118. 1.18139785222060435552E-2,
  119. 3.58236398605498653373E-2,
  120. -2.34591795718243348568E-1,
  121. 7.14304917030273074085E-2,
  122. 1.00000000000000000320E0
  123. };
  124. #define MAXGAM 171.624376956302725
  125. static double LOGPI = 1.14472988584940017414;
  126. #endif
  127. #ifdef DEC
  128. static unsigned short P[] = {
  129. 0035047,0162701,0146301,0005234,
  130. 0035634,0023437,0032065,0176530,
  131. 0036452,0137157,0047330,0122574,
  132. 0037103,0017310,0143041,0017232,
  133. 0037524,0066516,0162563,0164605,
  134. 0037775,0004671,0146237,0014222,
  135. 0040200,0000000,0000000,0000000
  136. };
  137. static unsigned short Q[] = {
  138. 0134302,0041724,0020006,0116565,
  139. 0035415,0072121,0044251,0025634,
  140. 0136222,0003447,0035205,0121114,
  141. 0036501,0107552,0154335,0104271,
  142. 0037022,0135717,0014776,0171471,
  143. 0137560,0034324,0165024,0037021,
  144. 0037222,0045046,0047151,0161213,
  145. 0040200,0000000,0000000,0000000
  146. };
  147. #define MAXGAM 34.84425627277176174
  148. static unsigned short LPI[4] = {
  149. 0040222,0103202,0043475,0006750,
  150. };
  151. #define LOGPI *(double *)LPI
  152. #endif
  153. #ifdef IBMPC
  154. static unsigned short P[] = {
  155. 0x2153,0x3998,0xfcb8,0x3f24,
  156. 0xbfab,0xe686,0x84e3,0x3f53,
  157. 0x14b0,0xe9db,0x57cd,0x3f85,
  158. 0x23d3,0x18c4,0x63d9,0x3fa8,
  159. 0x7d31,0xdcae,0x8da9,0x3fca,
  160. 0xe312,0x3993,0xa137,0x3fdf,
  161. 0x0000,0x0000,0x0000,0x3ff0
  162. };
  163. static unsigned short Q[] = {
  164. 0xd3af,0x8400,0x487a,0xbef8,
  165. 0x2573,0x2915,0xae8a,0x3f41,
  166. 0xb44a,0xe750,0x40e4,0xbf72,
  167. 0xb117,0x5b1b,0x31ed,0x3f88,
  168. 0xde67,0xe33f,0x5779,0x3fa2,
  169. 0x87c2,0x9d42,0x071a,0xbfce,
  170. 0x3c51,0xc9cd,0x4944,0x3fb2,
  171. 0x0000,0x0000,0x0000,0x3ff0
  172. };
  173. #define MAXGAM 171.624376956302725
  174. static unsigned short LPI[4] = {
  175. 0xa1bd,0x48e7,0x50d0,0x3ff2,
  176. };
  177. #define LOGPI *(double *)LPI
  178. #endif
  179. #ifdef MIEEE
  180. static unsigned short P[] = {
  181. 0x3f24,0xfcb8,0x3998,0x2153,
  182. 0x3f53,0x84e3,0xe686,0xbfab,
  183. 0x3f85,0x57cd,0xe9db,0x14b0,
  184. 0x3fa8,0x63d9,0x18c4,0x23d3,
  185. 0x3fca,0x8da9,0xdcae,0x7d31,
  186. 0x3fdf,0xa137,0x3993,0xe312,
  187. 0x3ff0,0x0000,0x0000,0x0000
  188. };
  189. static unsigned short Q[] = {
  190. 0xbef8,0x487a,0x8400,0xd3af,
  191. 0x3f41,0xae8a,0x2915,0x2573,
  192. 0xbf72,0x40e4,0xe750,0xb44a,
  193. 0x3f88,0x31ed,0x5b1b,0xb117,
  194. 0x3fa2,0x5779,0xe33f,0xde67,
  195. 0xbfce,0x071a,0x9d42,0x87c2,
  196. 0x3fb2,0x4944,0xc9cd,0x3c51,
  197. 0x3ff0,0x0000,0x0000,0x0000
  198. };
  199. #define MAXGAM 171.624376956302725
  200. static unsigned short LPI[4] = {
  201. 0x3ff2,0x50d0,0x48e7,0xa1bd,
  202. };
  203. #define LOGPI *(double *)LPI
  204. #endif
  205. /* Stirling's formula for the gamma function */
  206. #if UNK
  207. static double STIR[5] = {
  208. 7.87311395793093628397E-4,
  209. -2.29549961613378126380E-4,
  210. -2.68132617805781232825E-3,
  211. 3.47222221605458667310E-3,
  212. 8.33333333333482257126E-2,
  213. };
  214. #define MAXSTIR 143.01608
  215. static double SQTPI = 2.50662827463100050242E0;
  216. #endif
  217. #if DEC
  218. static unsigned short STIR[20] = {
  219. 0035516,0061622,0144553,0112224,
  220. 0135160,0131531,0037460,0165740,
  221. 0136057,0134460,0037242,0077270,
  222. 0036143,0107070,0156306,0027751,
  223. 0037252,0125252,0125252,0146064,
  224. };
  225. #define MAXSTIR 26.77
  226. static unsigned short SQT[4] = {
  227. 0040440,0066230,0177661,0034055,
  228. };
  229. #define SQTPI *(double *)SQT
  230. #endif
  231. #if IBMPC
  232. static unsigned short STIR[20] = {
  233. 0x7293,0x592d,0xcc72,0x3f49,
  234. 0x1d7c,0x27e6,0x166b,0xbf2e,
  235. 0x4fd7,0x07d4,0xf726,0xbf65,
  236. 0xc5fd,0x1b98,0x71c7,0x3f6c,
  237. 0x5986,0x5555,0x5555,0x3fb5,
  238. };
  239. #define MAXSTIR 143.01608
  240. static unsigned short SQT[4] = {
  241. 0x2706,0x1ff6,0x0d93,0x4004,
  242. };
  243. #define SQTPI *(double *)SQT
  244. #endif
  245. #if MIEEE
  246. static unsigned short STIR[20] = {
  247. 0x3f49,0xcc72,0x592d,0x7293,
  248. 0xbf2e,0x166b,0x27e6,0x1d7c,
  249. 0xbf65,0xf726,0x07d4,0x4fd7,
  250. 0x3f6c,0x71c7,0x1b98,0xc5fd,
  251. 0x3fb5,0x5555,0x5555,0x5986,
  252. };
  253. #define MAXSTIR 143.01608
  254. static unsigned short SQT[4] = {
  255. 0x4004,0x0d93,0x1ff6,0x2706,
  256. };
  257. #define SQTPI *(double *)SQT
  258. #endif
  259. int sgngam = 0;
  260. extern int sgngam;
  261. extern double MAXLOG, MAXNUM, PI;
  262. #ifdef ANSIPROT
  263. extern double pow ( double, double );
  264. extern double log ( double );
  265. extern double exp ( double );
  266. extern double sin ( double );
  267. extern double polevl ( double, void *, int );
  268. extern double p1evl ( double, void *, int );
  269. extern double floor ( double );
  270. extern double fabs ( double );
  271. extern int isnan ( double );
  272. extern int isfinite ( double );
  273. static double stirf ( double );
  274. double lgam ( double );
  275. #else
  276. double pow(), log(), exp(), sin(), polevl(), p1evl(), floor(), fabs();
  277. int isnan(), isfinite();
  278. static double stirf();
  279. double lgam();
  280. #endif
  281. #ifdef INFINITIES
  282. extern double INFINITY;
  283. #endif
  284. #ifdef NANS
  285. extern double NAN;
  286. #endif
  287. /* Gamma function computed by Stirling's formula.
  288. * The polynomial STIR is valid for 33 <= x <= 172.
  289. */
  290. static double stirf(x)
  291. double x;
  292. {
  293. double y, w, v;
  294. w = 1.0/x;
  295. w = 1.0 + w * polevl( w, STIR, 4 );
  296. y = exp(x);
  297. if( x > MAXSTIR )
  298. { /* Avoid overflow in pow() */
  299. v = pow( x, 0.5 * x - 0.25 );
  300. y = v * (v / y);
  301. }
  302. else
  303. {
  304. y = pow( x, x - 0.5 ) / y;
  305. }
  306. y = SQTPI * y * w;
  307. return( y );
  308. }
  309. double gamma(x)
  310. double x;
  311. {
  312. double p, q, z;
  313. int i;
  314. sgngam = 1;
  315. #ifdef NANS
  316. if( isnan(x) )
  317. return(x);
  318. #endif
  319. #ifdef INFINITIES
  320. #ifdef NANS
  321. if( x == INFINITY )
  322. return(x);
  323. if( x == -INFINITY )
  324. return(NAN);
  325. #else
  326. if( !isfinite(x) )
  327. return(x);
  328. #endif
  329. #endif
  330. q = fabs(x);
  331. if( q > 33.0 )
  332. {
  333. if( x < 0.0 )
  334. {
  335. p = floor(q);
  336. if( p == q )
  337. {
  338. #ifdef NANS
  339. gamnan:
  340. mtherr( "gamma", DOMAIN );
  341. return (NAN);
  342. #else
  343. goto goverf;
  344. #endif
  345. }
  346. i = p;
  347. if( (i & 1) == 0 )
  348. sgngam = -1;
  349. z = q - p;
  350. if( z > 0.5 )
  351. {
  352. p += 1.0;
  353. z = q - p;
  354. }
  355. z = q * sin( PI * z );
  356. if( z == 0.0 )
  357. {
  358. #ifdef INFINITIES
  359. return( sgngam * INFINITY);
  360. #else
  361. goverf:
  362. mtherr( "gamma", OVERFLOW );
  363. return( sgngam * MAXNUM);
  364. #endif
  365. }
  366. z = fabs(z);
  367. z = PI/(z * stirf(q) );
  368. }
  369. else
  370. {
  371. z = stirf(x);
  372. }
  373. return( sgngam * z );
  374. }
  375. z = 1.0;
  376. while( x >= 3.0 )
  377. {
  378. x -= 1.0;
  379. z *= x;
  380. }
  381. while( x < 0.0 )
  382. {
  383. if( x > -1.E-9 )
  384. goto small;
  385. z /= x;
  386. x += 1.0;
  387. }
  388. while( x < 2.0 )
  389. {
  390. if( x < 1.e-9 )
  391. goto small;
  392. z /= x;
  393. x += 1.0;
  394. }
  395. if( x == 2.0 )
  396. return(z);
  397. x -= 2.0;
  398. p = polevl( x, P, 6 );
  399. q = polevl( x, Q, 7 );
  400. return( z * p / q );
  401. small:
  402. if( x == 0.0 )
  403. {
  404. #ifdef INFINITIES
  405. #ifdef NANS
  406. goto gamnan;
  407. #else
  408. return( INFINITY );
  409. #endif
  410. #else
  411. mtherr( "gamma", SING );
  412. return( MAXNUM );
  413. #endif
  414. }
  415. else
  416. return( z/((1.0 + 0.5772156649015329 * x) * x) );
  417. }
  418. /* A[]: Stirling's formula expansion of log gamma
  419. * B[], C[]: log gamma function between 2 and 3
  420. */
  421. #ifdef UNK
  422. static double A[] = {
  423. 8.11614167470508450300E-4,
  424. -5.95061904284301438324E-4,
  425. 7.93650340457716943945E-4,
  426. -2.77777777730099687205E-3,
  427. 8.33333333333331927722E-2
  428. };
  429. static double B[] = {
  430. -1.37825152569120859100E3,
  431. -3.88016315134637840924E4,
  432. -3.31612992738871184744E5,
  433. -1.16237097492762307383E6,
  434. -1.72173700820839662146E6,
  435. -8.53555664245765465627E5
  436. };
  437. static double C[] = {
  438. /* 1.00000000000000000000E0, */
  439. -3.51815701436523470549E2,
  440. -1.70642106651881159223E4,
  441. -2.20528590553854454839E5,
  442. -1.13933444367982507207E6,
  443. -2.53252307177582951285E6,
  444. -2.01889141433532773231E6
  445. };
  446. /* log( sqrt( 2*pi ) ) */
  447. static double LS2PI = 0.91893853320467274178;
  448. #define MAXLGM 2.556348e305
  449. #endif
  450. #ifdef DEC
  451. static unsigned short A[] = {
  452. 0035524,0141201,0034633,0031405,
  453. 0135433,0176755,0126007,0045030,
  454. 0035520,0006371,0003342,0172730,
  455. 0136066,0005540,0132605,0026407,
  456. 0037252,0125252,0125252,0125132
  457. };
  458. static unsigned short B[] = {
  459. 0142654,0044014,0077633,0035410,
  460. 0144027,0110641,0125335,0144760,
  461. 0144641,0165637,0142204,0047447,
  462. 0145215,0162027,0146246,0155211,
  463. 0145322,0026110,0010317,0110130,
  464. 0145120,0061472,0120300,0025363
  465. };
  466. static unsigned short C[] = {
  467. /*0040200,0000000,0000000,0000000*/
  468. 0142257,0164150,0163630,0112622,
  469. 0143605,0050153,0156116,0135272,
  470. 0144527,0056045,0145642,0062332,
  471. 0145213,0012063,0106250,0001025,
  472. 0145432,0111254,0044577,0115142,
  473. 0145366,0071133,0050217,0005122
  474. };
  475. /* log( sqrt( 2*pi ) ) */
  476. static unsigned short LS2P[] = {040153,037616,041445,0172645,};
  477. #define LS2PI *(double *)LS2P
  478. #define MAXLGM 2.035093e36
  479. #endif
  480. #ifdef IBMPC
  481. static unsigned short A[] = {
  482. 0x6661,0x2733,0x9850,0x3f4a,
  483. 0xe943,0xb580,0x7fbd,0xbf43,
  484. 0x5ebb,0x20dc,0x019f,0x3f4a,
  485. 0xa5a1,0x16b0,0xc16c,0xbf66,
  486. 0x554b,0x5555,0x5555,0x3fb5
  487. };
  488. static unsigned short B[] = {
  489. 0x6761,0x8ff3,0x8901,0xc095,
  490. 0xb93e,0x355b,0xf234,0xc0e2,
  491. 0x89e5,0xf890,0x3d73,0xc114,
  492. 0xdb51,0xf994,0xbc82,0xc131,
  493. 0xf20b,0x0219,0x4589,0xc13a,
  494. 0x055e,0x5418,0x0c67,0xc12a
  495. };
  496. static unsigned short C[] = {
  497. /*0x0000,0x0000,0x0000,0x3ff0,*/
  498. 0x12b2,0x1cf3,0xfd0d,0xc075,
  499. 0xd757,0x7b89,0xaa0d,0xc0d0,
  500. 0x4c9b,0xb974,0xeb84,0xc10a,
  501. 0x0043,0x7195,0x6286,0xc131,
  502. 0xf34c,0x892f,0x5255,0xc143,
  503. 0xe14a,0x6a11,0xce4b,0xc13e
  504. };
  505. /* log( sqrt( 2*pi ) ) */
  506. static unsigned short LS2P[] = {
  507. 0xbeb5,0xc864,0x67f1,0x3fed
  508. };
  509. #define LS2PI *(double *)LS2P
  510. #define MAXLGM 2.556348e305
  511. #endif
  512. #ifdef MIEEE
  513. static unsigned short A[] = {
  514. 0x3f4a,0x9850,0x2733,0x6661,
  515. 0xbf43,0x7fbd,0xb580,0xe943,
  516. 0x3f4a,0x019f,0x20dc,0x5ebb,
  517. 0xbf66,0xc16c,0x16b0,0xa5a1,
  518. 0x3fb5,0x5555,0x5555,0x554b
  519. };
  520. static unsigned short B[] = {
  521. 0xc095,0x8901,0x8ff3,0x6761,
  522. 0xc0e2,0xf234,0x355b,0xb93e,
  523. 0xc114,0x3d73,0xf890,0x89e5,
  524. 0xc131,0xbc82,0xf994,0xdb51,
  525. 0xc13a,0x4589,0x0219,0xf20b,
  526. 0xc12a,0x0c67,0x5418,0x055e
  527. };
  528. static unsigned short C[] = {
  529. 0xc075,0xfd0d,0x1cf3,0x12b2,
  530. 0xc0d0,0xaa0d,0x7b89,0xd757,
  531. 0xc10a,0xeb84,0xb974,0x4c9b,
  532. 0xc131,0x6286,0x7195,0x0043,
  533. 0xc143,0x5255,0x892f,0xf34c,
  534. 0xc13e,0xce4b,0x6a11,0xe14a
  535. };
  536. /* log( sqrt( 2*pi ) ) */
  537. static unsigned short LS2P[] = {
  538. 0x3fed,0x67f1,0xc864,0xbeb5
  539. };
  540. #define LS2PI *(double *)LS2P
  541. #define MAXLGM 2.556348e305
  542. #endif
  543. /* Logarithm of gamma function */
  544. double lgam(x)
  545. double x;
  546. {
  547. double p, q, u, w, z;
  548. int i;
  549. sgngam = 1;
  550. #ifdef NANS
  551. if( isnan(x) )
  552. return(x);
  553. #endif
  554. #ifdef INFINITIES
  555. if( !isfinite(x) )
  556. return(INFINITY);
  557. #endif
  558. if( x < -34.0 )
  559. {
  560. q = -x;
  561. w = lgam(q); /* note this modifies sgngam! */
  562. p = floor(q);
  563. if( p == q )
  564. {
  565. lgsing:
  566. #ifdef INFINITIES
  567. mtherr( "lgam", SING );
  568. return (INFINITY);
  569. #else
  570. goto loverf;
  571. #endif
  572. }
  573. i = p;
  574. if( (i & 1) == 0 )
  575. sgngam = -1;
  576. else
  577. sgngam = 1;
  578. z = q - p;
  579. if( z > 0.5 )
  580. {
  581. p += 1.0;
  582. z = p - q;
  583. }
  584. z = q * sin( PI * z );
  585. if( z == 0.0 )
  586. goto lgsing;
  587. /* z = log(PI) - log( z ) - w;*/
  588. z = LOGPI - log( z ) - w;
  589. return( z );
  590. }
  591. if( x < 13.0 )
  592. {
  593. z = 1.0;
  594. p = 0.0;
  595. u = x;
  596. while( u >= 3.0 )
  597. {
  598. p -= 1.0;
  599. u = x + p;
  600. z *= u;
  601. }
  602. while( u < 2.0 )
  603. {
  604. if( u == 0.0 )
  605. goto lgsing;
  606. z /= u;
  607. p += 1.0;
  608. u = x + p;
  609. }
  610. if( z < 0.0 )
  611. {
  612. sgngam = -1;
  613. z = -z;
  614. }
  615. else
  616. sgngam = 1;
  617. if( u == 2.0 )
  618. return( log(z) );
  619. p -= 2.0;
  620. x = x + p;
  621. p = x * polevl( x, B, 5 ) / p1evl( x, C, 6);
  622. return( log(z) + p );
  623. }
  624. if( x > MAXLGM )
  625. {
  626. #ifdef INFINITIES
  627. return( sgngam * INFINITY );
  628. #else
  629. loverf:
  630. mtherr( "lgam", OVERFLOW );
  631. return( sgngam * MAXNUM );
  632. #endif
  633. }
  634. q = ( x - 0.5 ) * log(x) - x + LS2PI;
  635. if( x > 1.0e8 )
  636. return( q );
  637. p = 1.0/(x*x);
  638. if( x >= 1000.0 )
  639. q += (( 7.9365079365079365079365e-4 * p
  640. - 2.7777777777777777777778e-3) *p
  641. + 0.0833333333333333333333) / x;
  642. else
  643. q += polevl( p, A, 4 ) / x;
  644. return( q );
  645. }