gammal.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. /* gammal.c
  2. *
  3. * Gamma function
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * long double x, y, gammal();
  10. * extern int sgngam;
  11. *
  12. * y = gammal( 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| <= 13 are reduced by recurrence and the function
  25. * approximated by a rational function of degree 7/8 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. * IEEE -40,+40 10000 3.6e-19 7.9e-20
  36. * IEEE -1755,+1755 10000 4.8e-18 6.5e-19
  37. *
  38. * Accuracy for large arguments is dominated by error in powl().
  39. *
  40. */
  41. /* lgaml()
  42. *
  43. * Natural logarithm of gamma function
  44. *
  45. *
  46. *
  47. * SYNOPSIS:
  48. *
  49. * long double x, y, lgaml();
  50. * extern int sgngam;
  51. *
  52. * y = lgaml( x );
  53. *
  54. *
  55. *
  56. * DESCRIPTION:
  57. *
  58. * Returns the base e (2.718...) logarithm of the absolute
  59. * value of the gamma function of the argument.
  60. * The sign (+1 or -1) of the gamma function is returned in a
  61. * global (extern) variable named sgngam.
  62. *
  63. * For arguments greater than 33, the logarithm of the gamma
  64. * function is approximated by the logarithmic version of
  65. * Stirling's formula using a polynomial approximation of
  66. * degree 4. Arguments between -33 and +33 are reduced by
  67. * recurrence to the interval [2,3] of a rational approximation.
  68. * The cosecant reflection formula is employed for arguments
  69. * less than -33.
  70. *
  71. * Arguments greater than MAXLGML (10^4928) return MAXNUML.
  72. *
  73. *
  74. *
  75. * ACCURACY:
  76. *
  77. *
  78. * arithmetic domain # trials peak rms
  79. * IEEE -40, 40 100000 2.2e-19 4.6e-20
  80. * IEEE 10^-2000,10^+2000 20000 1.6e-19 3.3e-20
  81. * The error criterion was relative when the function magnitude
  82. * was greater than one but absolute when it was less than one.
  83. *
  84. */
  85. /* gamma.c */
  86. /* gamma function */
  87. /*
  88. Copyright 1994 by Stephen L. Moshier
  89. */
  90. #include <math.h>
  91. /*
  92. gamma(x+2) = gamma(x+2) P(x)/Q(x)
  93. 0 <= x <= 1
  94. Relative error
  95. n=7, d=8
  96. Peak error = 1.83e-20
  97. Relative error spread = 8.4e-23
  98. */
  99. #if UNK
  100. static long double P[8] = {
  101. 4.212760487471622013093E-5L,
  102. 4.542931960608009155600E-4L,
  103. 4.092666828394035500949E-3L,
  104. 2.385363243461108252554E-2L,
  105. 1.113062816019361559013E-1L,
  106. 3.629515436640239168939E-1L,
  107. 8.378004301573126728826E-1L,
  108. 1.000000000000000000009E0L,
  109. };
  110. static long double Q[9] = {
  111. -1.397148517476170440917E-5L,
  112. 2.346584059160635244282E-4L,
  113. -1.237799246653152231188E-3L,
  114. -7.955933682494738320586E-4L,
  115. 2.773706565840072979165E-2L,
  116. -4.633887671244534213831E-2L,
  117. -2.243510905670329164562E-1L,
  118. 4.150160950588455434583E-1L,
  119. 9.999999999999999999908E-1L,
  120. };
  121. #endif
  122. #if IBMPC
  123. static short P[] = {
  124. 0x434a,0x3f22,0x2bda,0xb0b2,0x3ff0, XPD
  125. 0xf5aa,0xe82f,0x335b,0xee2e,0x3ff3, XPD
  126. 0xbe6c,0x3757,0xc717,0x861b,0x3ff7, XPD
  127. 0x7f43,0x5196,0xb166,0xc368,0x3ff9, XPD
  128. 0x9549,0x8eb5,0x8c3a,0xe3f4,0x3ffb, XPD
  129. 0x8d75,0x23af,0xc8e4,0xb9d4,0x3ffd, XPD
  130. 0x29cf,0x19b3,0x16c8,0xd67a,0x3ffe, XPD
  131. 0x0000,0x0000,0x0000,0x8000,0x3fff, XPD
  132. };
  133. static short Q[] = {
  134. 0x5473,0x2de8,0x1268,0xea67,0xbfee, XPD
  135. 0x334b,0xc2f0,0xa2dd,0xf60e,0x3ff2, XPD
  136. 0xbeed,0x1853,0xa691,0xa23d,0xbff5, XPD
  137. 0x296e,0x7cb1,0x5dfd,0xd08f,0xbff4, XPD
  138. 0x0417,0x7989,0xd7bc,0xe338,0x3ff9, XPD
  139. 0x3295,0x3698,0xd580,0xbdcd,0xbffa, XPD
  140. 0x75ef,0x3ab7,0x4ad3,0xe5bc,0xbffc, XPD
  141. 0xe458,0x2ec7,0xfd57,0xd47c,0x3ffd, XPD
  142. 0x0000,0x0000,0x0000,0x8000,0x3fff, XPD
  143. };
  144. #endif
  145. #if MIEEE
  146. static long P[24] = {
  147. 0x3ff00000,0xb0b22bda,0x3f22434a,
  148. 0x3ff30000,0xee2e335b,0xe82ff5aa,
  149. 0x3ff70000,0x861bc717,0x3757be6c,
  150. 0x3ff90000,0xc368b166,0x51967f43,
  151. 0x3ffb0000,0xe3f48c3a,0x8eb59549,
  152. 0x3ffd0000,0xb9d4c8e4,0x23af8d75,
  153. 0x3ffe0000,0xd67a16c8,0x19b329cf,
  154. 0x3fff0000,0x80000000,0x00000000,
  155. };
  156. static long Q[27] = {
  157. 0xbfee0000,0xea671268,0x2de85473,
  158. 0x3ff20000,0xf60ea2dd,0xc2f0334b,
  159. 0xbff50000,0xa23da691,0x1853beed,
  160. 0xbff40000,0xd08f5dfd,0x7cb1296e,
  161. 0x3ff90000,0xe338d7bc,0x79890417,
  162. 0xbffa0000,0xbdcdd580,0x36983295,
  163. 0xbffc0000,0xe5bc4ad3,0x3ab775ef,
  164. 0x3ffd0000,0xd47cfd57,0x2ec7e458,
  165. 0x3fff0000,0x80000000,0x00000000,
  166. };
  167. #endif
  168. /*
  169. static long double P[] = {
  170. -3.01525602666895735709e0L,
  171. -3.25157411956062339893e1L,
  172. -2.92929976820724030353e2L,
  173. -1.70730828800510297666e3L,
  174. -7.96667499622741999770e3L,
  175. -2.59780216007146401957e4L,
  176. -5.99650230220855581642e4L,
  177. -7.15743521530849602425e4L
  178. };
  179. static long double Q[] = {
  180. 1.00000000000000000000e0L,
  181. -1.67955233807178858919e1L,
  182. 8.85946791747759881659e1L,
  183. 5.69440799097468430177e1L,
  184. -1.98526250512761318471e3L,
  185. 3.31667508019495079814e3L,
  186. 1.60577839621734713377e4L,
  187. -2.97045081369399940529e4L,
  188. -7.15743521530849602412e4L
  189. };
  190. */
  191. #define MAXGAML 1755.455L
  192. /*static long double LOGPI = 1.14472988584940017414L;*/
  193. /* Stirling's formula for the gamma function
  194. gamma(x) = sqrt(2 pi) x^(x-.5) exp(-x) (1 + 1/x P(1/x))
  195. z(x) = x
  196. 13 <= x <= 1024
  197. Relative error
  198. n=8, d=0
  199. Peak error = 9.44e-21
  200. Relative error spread = 8.8e-4
  201. */
  202. #if UNK
  203. static long double STIR[9] = {
  204. 7.147391378143610789273E-4L,
  205. -2.363848809501759061727E-5L,
  206. -5.950237554056330156018E-4L,
  207. 6.989332260623193171870E-5L,
  208. 7.840334842744753003862E-4L,
  209. -2.294719747873185405699E-4L,
  210. -2.681327161876304418288E-3L,
  211. 3.472222222230075327854E-3L,
  212. 8.333333333333331800504E-2L,
  213. };
  214. #endif
  215. #if IBMPC
  216. static short STIR[] = {
  217. 0x6ede,0x69f7,0x54e3,0xbb5d,0x3ff4, XPD
  218. 0xc395,0x0295,0x4443,0xc64b,0xbfef, XPD
  219. 0xba6f,0x7c59,0x5e47,0x9bfb,0xbff4, XPD
  220. 0x5704,0x1a39,0xb11d,0x9293,0x3ff1, XPD
  221. 0x30b7,0x1a21,0x98b2,0xcd87,0x3ff4, XPD
  222. 0xbef3,0x7023,0x6a08,0xf09e,0xbff2, XPD
  223. 0x3a1c,0x5ac8,0x3478,0xafb9,0xbff6, XPD
  224. 0xc3c9,0x906e,0x38e3,0xe38e,0x3ff6, XPD
  225. 0xa1d5,0xaaaa,0xaaaa,0xaaaa,0x3ffb, XPD
  226. };
  227. #endif
  228. #if MIEEE
  229. static long STIR[27] = {
  230. 0x3ff40000,0xbb5d54e3,0x69f76ede,
  231. 0xbfef0000,0xc64b4443,0x0295c395,
  232. 0xbff40000,0x9bfb5e47,0x7c59ba6f,
  233. 0x3ff10000,0x9293b11d,0x1a395704,
  234. 0x3ff40000,0xcd8798b2,0x1a2130b7,
  235. 0xbff20000,0xf09e6a08,0x7023bef3,
  236. 0xbff60000,0xafb93478,0x5ac83a1c,
  237. 0x3ff60000,0xe38e38e3,0x906ec3c9,
  238. 0x3ffb0000,0xaaaaaaaa,0xaaaaa1d5,
  239. };
  240. #endif
  241. #define MAXSTIR 1024.0L
  242. static long double SQTPI = 2.50662827463100050242E0L;
  243. /* 1/gamma(x) = z P(z)
  244. * z(x) = 1/x
  245. * 0 < x < 0.03125
  246. * Peak relative error 4.2e-23
  247. */
  248. #if UNK
  249. static long double S[9] = {
  250. -1.193945051381510095614E-3L,
  251. 7.220599478036909672331E-3L,
  252. -9.622023360406271645744E-3L,
  253. -4.219773360705915470089E-2L,
  254. 1.665386113720805206758E-1L,
  255. -4.200263503403344054473E-2L,
  256. -6.558780715202540684668E-1L,
  257. 5.772156649015328608253E-1L,
  258. 1.000000000000000000000E0L,
  259. };
  260. #endif
  261. #if IBMPC
  262. static short S[] = {
  263. 0xbaeb,0xd6d3,0x25e5,0x9c7e,0xbff5, XPD
  264. 0xfe9a,0xceb4,0xc74e,0xec9a,0x3ff7, XPD
  265. 0x9225,0xdfef,0xb0e9,0x9da5,0xbff8, XPD
  266. 0x10b0,0xec17,0x87dc,0xacd7,0xbffa, XPD
  267. 0x6b8d,0x7515,0x1905,0xaa89,0x3ffc, XPD
  268. 0xf183,0x126b,0xf47d,0xac0a,0xbffa, XPD
  269. 0x7bf6,0x57d1,0xa013,0xa7e7,0xbffe, XPD
  270. 0xc7a9,0x7db0,0x67e3,0x93c4,0x3ffe, XPD
  271. 0x0000,0x0000,0x0000,0x8000,0x3fff, XPD
  272. };
  273. #endif
  274. #if MIEEE
  275. static long S[27] = {
  276. 0xbff50000,0x9c7e25e5,0xd6d3baeb,
  277. 0x3ff70000,0xec9ac74e,0xceb4fe9a,
  278. 0xbff80000,0x9da5b0e9,0xdfef9225,
  279. 0xbffa0000,0xacd787dc,0xec1710b0,
  280. 0x3ffc0000,0xaa891905,0x75156b8d,
  281. 0xbffa0000,0xac0af47d,0x126bf183,
  282. 0xbffe0000,0xa7e7a013,0x57d17bf6,
  283. 0x3ffe0000,0x93c467e3,0x7db0c7a9,
  284. 0x3fff0000,0x80000000,0x00000000,
  285. };
  286. #endif
  287. /* 1/gamma(-x) = z P(z)
  288. * z(x) = 1/x
  289. * 0 < x < 0.03125
  290. * Peak relative error 5.16e-23
  291. * Relative error spread = 2.5e-24
  292. */
  293. #if UNK
  294. static long double SN[9] = {
  295. 1.133374167243894382010E-3L,
  296. 7.220837261893170325704E-3L,
  297. 9.621911155035976733706E-3L,
  298. -4.219773343731191721664E-2L,
  299. -1.665386113944413519335E-1L,
  300. -4.200263503402112910504E-2L,
  301. 6.558780715202536547116E-1L,
  302. 5.772156649015328608727E-1L,
  303. -1.000000000000000000000E0L,
  304. };
  305. #endif
  306. #if IBMPC
  307. static short SN[] = {
  308. 0x5dd1,0x02de,0xb9f7,0x948d,0x3ff5, XPD
  309. 0x989b,0xdd68,0xc5f1,0xec9c,0x3ff7, XPD
  310. 0x2ca1,0x18f0,0x386f,0x9da5,0x3ff8, XPD
  311. 0x783f,0x41dd,0x87d1,0xacd7,0xbffa, XPD
  312. 0x7a5b,0xd76d,0x1905,0xaa89,0xbffc, XPD
  313. 0x7f64,0x1234,0xf47d,0xac0a,0xbffa, XPD
  314. 0x5e26,0x57d1,0xa013,0xa7e7,0x3ffe, XPD
  315. 0xc7aa,0x7db0,0x67e3,0x93c4,0x3ffe, XPD
  316. 0x0000,0x0000,0x0000,0x8000,0xbfff, XPD
  317. };
  318. #endif
  319. #if MIEEE
  320. static long SN[27] = {
  321. 0x3ff50000,0x948db9f7,0x02de5dd1,
  322. 0x3ff70000,0xec9cc5f1,0xdd68989b,
  323. 0x3ff80000,0x9da5386f,0x18f02ca1,
  324. 0xbffa0000,0xacd787d1,0x41dd783f,
  325. 0xbffc0000,0xaa891905,0xd76d7a5b,
  326. 0xbffa0000,0xac0af47d,0x12347f64,
  327. 0x3ffe0000,0xa7e7a013,0x57d15e26,
  328. 0x3ffe0000,0x93c467e3,0x7db0c7aa,
  329. 0xbfff0000,0x80000000,0x00000000,
  330. };
  331. #endif
  332. int sgngaml = 0;
  333. extern int sgngaml;
  334. extern long double MAXLOGL, MAXNUML, PIL;
  335. /* #define PIL 3.14159265358979323846L */
  336. /* #define MAXNUML 1.189731495357231765021263853E4932L */
  337. #ifdef ANSIPROT
  338. extern long double fabsl ( long double );
  339. extern long double lgaml ( long double );
  340. extern long double logl ( long double );
  341. extern long double expl ( long double );
  342. extern long double gammal ( long double );
  343. extern long double sinl ( long double );
  344. extern long double floorl ( long double );
  345. extern long double powl ( long double, long double );
  346. extern long double polevll ( long double, void *, int );
  347. extern long double p1evll ( long double, void *, int );
  348. extern int isnanl ( long double );
  349. extern int isfinitel ( long double );
  350. static long double stirf ( long double );
  351. #else
  352. long double fabsl(), lgaml(), logl(), expl(), gammal(), sinl();
  353. long double floorl(), powl(), polevll(), p1evll(), isnanl(), isfinitel();
  354. static long double stirf();
  355. #endif
  356. #ifdef INFINITIES
  357. extern long double INFINITYL;
  358. #endif
  359. #ifdef NANS
  360. extern long double NANL;
  361. #endif
  362. /* Gamma function computed by Stirling's formula.
  363. */
  364. static long double stirf(x)
  365. long double x;
  366. {
  367. long double y, w, v;
  368. w = 1.0L/x;
  369. /* For large x, use rational coefficients from the analytical expansion. */
  370. if( x > 1024.0L )
  371. w = (((((6.97281375836585777429E-5L * w
  372. + 7.84039221720066627474E-4L) * w
  373. - 2.29472093621399176955E-4L) * w
  374. - 2.68132716049382716049E-3L) * w
  375. + 3.47222222222222222222E-3L) * w
  376. + 8.33333333333333333333E-2L) * w
  377. + 1.0L;
  378. else
  379. w = 1.0L + w * polevll( w, STIR, 8 );
  380. y = expl(x);
  381. if( x > MAXSTIR )
  382. { /* Avoid overflow in pow() */
  383. v = powl( x, 0.5L * x - 0.25L );
  384. y = v * (v / y);
  385. }
  386. else
  387. {
  388. y = powl( x, x - 0.5L ) / y;
  389. }
  390. y = SQTPI * y * w;
  391. return( y );
  392. }
  393. long double gammal(x)
  394. long double x;
  395. {
  396. long double p, q, z;
  397. int i;
  398. sgngaml = 1;
  399. #ifdef NANS
  400. if( isnanl(x) )
  401. return(NANL);
  402. #endif
  403. #ifdef INFINITIES
  404. if(x == INFINITYL)
  405. return(INFINITYL);
  406. #ifdef NANS
  407. if(x == -INFINITYL)
  408. goto gamnan;
  409. #endif
  410. #endif
  411. q = fabsl(x);
  412. if( q > 13.0L )
  413. {
  414. if( q > MAXGAML )
  415. goto goverf;
  416. if( x < 0.0L )
  417. {
  418. p = floorl(q);
  419. if( p == q )
  420. {
  421. gamnan:
  422. #ifdef NANS
  423. mtherr( "gammal", DOMAIN );
  424. return (NANL);
  425. #else
  426. goto goverf;
  427. #endif
  428. }
  429. i = p;
  430. if( (i & 1) == 0 )
  431. sgngaml = -1;
  432. z = q - p;
  433. if( z > 0.5L )
  434. {
  435. p += 1.0L;
  436. z = q - p;
  437. }
  438. z = q * sinl( PIL * z );
  439. z = fabsl(z) * stirf(q);
  440. if( z <= PIL/MAXNUML )
  441. {
  442. goverf:
  443. #ifdef INFINITIES
  444. return( sgngaml * INFINITYL);
  445. #else
  446. mtherr( "gammal", OVERFLOW );
  447. return( sgngaml * MAXNUML);
  448. #endif
  449. }
  450. z = PIL/z;
  451. }
  452. else
  453. {
  454. z = stirf(x);
  455. }
  456. return( sgngaml * z );
  457. }
  458. z = 1.0L;
  459. while( x >= 3.0L )
  460. {
  461. x -= 1.0L;
  462. z *= x;
  463. }
  464. while( x < -0.03125L )
  465. {
  466. z /= x;
  467. x += 1.0L;
  468. }
  469. if( x <= 0.03125L )
  470. goto small;
  471. while( x < 2.0L )
  472. {
  473. z /= x;
  474. x += 1.0L;
  475. }
  476. if( x == 2.0L )
  477. return(z);
  478. x -= 2.0L;
  479. p = polevll( x, P, 7 );
  480. q = polevll( x, Q, 8 );
  481. return( z * p / q );
  482. small:
  483. if( x == 0.0L )
  484. {
  485. goto gamnan;
  486. }
  487. else
  488. {
  489. if( x < 0.0L )
  490. {
  491. x = -x;
  492. q = z / (x * polevll( x, SN, 8 ));
  493. }
  494. else
  495. q = z / (x * polevll( x, S, 8 ));
  496. }
  497. return q;
  498. }
  499. /* A[]: Stirling's formula expansion of log gamma
  500. * B[], C[]: log gamma function between 2 and 3
  501. */
  502. /* log gamma(x) = ( x - 0.5 ) * log(x) - x + LS2PI + 1/x A(1/x^2)
  503. * x >= 8
  504. * Peak relative error 1.51e-21
  505. * Relative spread of error peaks 5.67e-21
  506. */
  507. #if UNK
  508. static long double A[7] = {
  509. 4.885026142432270781165E-3L,
  510. -1.880801938119376907179E-3L,
  511. 8.412723297322498080632E-4L,
  512. -5.952345851765688514613E-4L,
  513. 7.936507795855070755671E-4L,
  514. -2.777777777750349603440E-3L,
  515. 8.333333333333331447505E-2L,
  516. };
  517. #endif
  518. #if IBMPC
  519. static short A[] = {
  520. 0xd984,0xcc08,0x91c2,0xa012,0x3ff7, XPD
  521. 0x3d91,0x0304,0x3da1,0xf685,0xbff5, XPD
  522. 0x3bdc,0xaad1,0xd492,0xdc88,0x3ff4, XPD
  523. 0x8b20,0x9fce,0x844e,0x9c09,0xbff4, XPD
  524. 0xf8f2,0x30e5,0x0092,0xd00d,0x3ff4, XPD
  525. 0x4d88,0x03a8,0x60b6,0xb60b,0xbff6, XPD
  526. 0x9fcc,0xaaaa,0xaaaa,0xaaaa,0x3ffb, XPD
  527. };
  528. #endif
  529. #if MIEEE
  530. static long A[21] = {
  531. 0x3ff70000,0xa01291c2,0xcc08d984,
  532. 0xbff50000,0xf6853da1,0x03043d91,
  533. 0x3ff40000,0xdc88d492,0xaad13bdc,
  534. 0xbff40000,0x9c09844e,0x9fce8b20,
  535. 0x3ff40000,0xd00d0092,0x30e5f8f2,
  536. 0xbff60000,0xb60b60b6,0x03a84d88,
  537. 0x3ffb0000,0xaaaaaaaa,0xaaaa9fcc,
  538. };
  539. #endif
  540. /* log gamma(x+2) = x B(x)/C(x)
  541. * 0 <= x <= 1
  542. * Peak relative error 7.16e-22
  543. * Relative spread of error peaks 4.78e-20
  544. */
  545. #if UNK
  546. static long double B[7] = {
  547. -2.163690827643812857640E3L,
  548. -8.723871522843511459790E4L,
  549. -1.104326814691464261197E6L,
  550. -6.111225012005214299996E6L,
  551. -1.625568062543700591014E7L,
  552. -2.003937418103815175475E7L,
  553. -8.875666783650703802159E6L,
  554. };
  555. static long double C[7] = {
  556. /* 1.000000000000000000000E0L,*/
  557. -5.139481484435370143617E2L,
  558. -3.403570840534304670537E4L,
  559. -6.227441164066219501697E5L,
  560. -4.814940379411882186630E6L,
  561. -1.785433287045078156959E7L,
  562. -3.138646407656182662088E7L,
  563. -2.099336717757895876142E7L,
  564. };
  565. #endif
  566. #if IBMPC
  567. static short B[] = {
  568. 0x9557,0x4995,0x0da1,0x873b,0xc00a, XPD
  569. 0xfe44,0x9af8,0x5b8c,0xaa63,0xc00f, XPD
  570. 0x5aa8,0x7cf5,0x3684,0x86ce,0xc013, XPD
  571. 0x259a,0x258c,0xf206,0xba7f,0xc015, XPD
  572. 0xbe18,0x1ca3,0xc0a0,0xf80a,0xc016, XPD
  573. 0x168f,0x2c42,0x6717,0x98e3,0xc017, XPD
  574. 0x2051,0x9d55,0x92c8,0x876e,0xc016, XPD
  575. };
  576. static short C[] = {
  577. /*0x0000,0x0000,0x0000,0x8000,0x3fff, XPD*/
  578. 0xaa77,0xcf2f,0xae76,0x807c,0xc008, XPD
  579. 0xb280,0x0d74,0xb55a,0x84f3,0xc00e, XPD
  580. 0xa505,0xcd30,0x81dc,0x9809,0xc012, XPD
  581. 0x3369,0x4246,0xb8c2,0x92f0,0xc015, XPD
  582. 0x63cf,0x6aee,0xbe6f,0x8837,0xc017, XPD
  583. 0x26bb,0xccc7,0xb009,0xef75,0xc017, XPD
  584. 0x462b,0xbae8,0xab96,0xa02a,0xc017, XPD
  585. };
  586. #endif
  587. #if MIEEE
  588. static long B[21] = {
  589. 0xc00a0000,0x873b0da1,0x49959557,
  590. 0xc00f0000,0xaa635b8c,0x9af8fe44,
  591. 0xc0130000,0x86ce3684,0x7cf55aa8,
  592. 0xc0150000,0xba7ff206,0x258c259a,
  593. 0xc0160000,0xf80ac0a0,0x1ca3be18,
  594. 0xc0170000,0x98e36717,0x2c42168f,
  595. 0xc0160000,0x876e92c8,0x9d552051,
  596. };
  597. static long C[21] = {
  598. /*0x3fff0000,0x80000000,0x00000000,*/
  599. 0xc0080000,0x807cae76,0xcf2faa77,
  600. 0xc00e0000,0x84f3b55a,0x0d74b280,
  601. 0xc0120000,0x980981dc,0xcd30a505,
  602. 0xc0150000,0x92f0b8c2,0x42463369,
  603. 0xc0170000,0x8837be6f,0x6aee63cf,
  604. 0xc0170000,0xef75b009,0xccc726bb,
  605. 0xc0170000,0xa02aab96,0xbae8462b,
  606. };
  607. #endif
  608. /* log( sqrt( 2*pi ) ) */
  609. static long double LS2PI = 0.91893853320467274178L;
  610. #define MAXLGM 1.04848146839019521116e+4928L
  611. /* Logarithm of gamma function */
  612. long double lgaml(x)
  613. long double x;
  614. {
  615. long double p, q, w, z, f, nx;
  616. int i;
  617. sgngaml = 1;
  618. #ifdef NANS
  619. if( isnanl(x) )
  620. return(NANL);
  621. #endif
  622. #ifdef INFINITIES
  623. if( !isfinitel(x) )
  624. return(INFINITYL);
  625. #endif
  626. if( x < -34.0L )
  627. {
  628. q = -x;
  629. w = lgaml(q); /* note this modifies sgngam! */
  630. p = floorl(q);
  631. if( p == q )
  632. {
  633. #ifdef INFINITIES
  634. mtherr( "lgaml", SING );
  635. return (INFINITYL);
  636. #else
  637. goto loverf;
  638. #endif
  639. }
  640. i = p;
  641. if( (i & 1) == 0 )
  642. sgngaml = -1;
  643. else
  644. sgngaml = 1;
  645. z = q - p;
  646. if( z > 0.5L )
  647. {
  648. p += 1.0L;
  649. z = p - q;
  650. }
  651. z = q * sinl( PIL * z );
  652. if( z == 0.0L )
  653. goto loverf;
  654. /* z = LOGPI - logl( z ) - w; */
  655. z = logl( PIL/z ) - w;
  656. return( z );
  657. }
  658. if( x < 13.0L )
  659. {
  660. z = 1.0L;
  661. nx = floorl( x + 0.5L );
  662. f = x - nx;
  663. while( x >= 3.0L )
  664. {
  665. nx -= 1.0L;
  666. x = nx + f;
  667. z *= x;
  668. }
  669. while( x < 2.0L )
  670. {
  671. if( fabsl(x) <= 0.03125 )
  672. goto lsmall;
  673. z /= nx + f;
  674. nx += 1.0L;
  675. x = nx + f;
  676. }
  677. if( z < 0.0L )
  678. {
  679. sgngaml = -1;
  680. z = -z;
  681. }
  682. else
  683. sgngaml = 1;
  684. if( x == 2.0L )
  685. return( logl(z) );
  686. x = (nx - 2.0L) + f;
  687. p = x * polevll( x, B, 6 ) / p1evll( x, C, 7);
  688. return( logl(z) + p );
  689. }
  690. if( x > MAXLGM )
  691. {
  692. loverf:
  693. #ifdef INFINITIES
  694. return( sgngaml * INFINITYL );
  695. #else
  696. mtherr( "lgaml", OVERFLOW );
  697. return( sgngaml * MAXNUML );
  698. #endif
  699. }
  700. q = ( x - 0.5L ) * logl(x) - x + LS2PI;
  701. if( x > 1.0e10L )
  702. return(q);
  703. p = 1.0L/(x*x);
  704. q += polevll( p, A, 6 ) / x;
  705. return( q );
  706. lsmall:
  707. if( x == 0.0L )
  708. goto loverf;
  709. if( x < 0.0L )
  710. {
  711. x = -x;
  712. q = z / (x * polevll( x, SN, 8 ));
  713. }
  714. else
  715. q = z / (x * polevll( x, S, 8 ));
  716. if( q < 0.0L )
  717. {
  718. sgngaml = -1;
  719. q = -q;
  720. }
  721. else
  722. sgngaml = 1;
  723. q = logl( q );
  724. return(q);
  725. }