e_tgammal.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /* $OpenBSD: e_tgammal.c,v 1.4 2013/11/12 20:35:19 martynas Exp $ */
  2. /*
  3. * Copyright (c) 2008 Stephen L. Moshier <[email protected]>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* tgammal.c
  18. *
  19. * Gamma function
  20. *
  21. *
  22. *
  23. * SYNOPSIS:
  24. *
  25. * long double x, y, tgammal();
  26. * extern int signgam;
  27. *
  28. * y = tgammal( x );
  29. *
  30. *
  31. *
  32. * DESCRIPTION:
  33. *
  34. * Returns gamma function of the argument. The result is
  35. * correctly signed, and the sign (+1 or -1) is also
  36. * returned in a global (extern) variable named signgam.
  37. * This variable is also filled in by the logarithmic gamma
  38. * function lgamma().
  39. *
  40. * Arguments |x| <= 13 are reduced by recurrence and the function
  41. * approximated by a rational function of degree 7/8 in the
  42. * interval (2,3). Large arguments are handled by Stirling's
  43. * formula. Large negative arguments are made positive using
  44. * a reflection formula.
  45. *
  46. *
  47. * ACCURACY:
  48. *
  49. * Relative error:
  50. * arithmetic domain # trials peak rms
  51. * IEEE -40,+40 10000 3.6e-19 7.9e-20
  52. * IEEE -1755,+1755 10000 4.8e-18 6.5e-19
  53. *
  54. * Accuracy for large arguments is dominated by error in powl().
  55. *
  56. */
  57. #include <float.h>
  58. #include <openlibm.h>
  59. #include "math_private.h"
  60. extern int signgam;
  61. /*
  62. tgamma(x+2) = tgamma(x+2) P(x)/Q(x)
  63. 0 <= x <= 1
  64. Relative error
  65. n=7, d=8
  66. Peak error = 1.83e-20
  67. Relative error spread = 8.4e-23
  68. */
  69. static long double P[8] = {
  70. 4.212760487471622013093E-5L,
  71. 4.542931960608009155600E-4L,
  72. 4.092666828394035500949E-3L,
  73. 2.385363243461108252554E-2L,
  74. 1.113062816019361559013E-1L,
  75. 3.629515436640239168939E-1L,
  76. 8.378004301573126728826E-1L,
  77. 1.000000000000000000009E0L,
  78. };
  79. static long double Q[9] = {
  80. -1.397148517476170440917E-5L,
  81. 2.346584059160635244282E-4L,
  82. -1.237799246653152231188E-3L,
  83. -7.955933682494738320586E-4L,
  84. 2.773706565840072979165E-2L,
  85. -4.633887671244534213831E-2L,
  86. -2.243510905670329164562E-1L,
  87. 4.150160950588455434583E-1L,
  88. 9.999999999999999999908E-1L,
  89. };
  90. /*
  91. static long double P[] = {
  92. -3.01525602666895735709e0L,
  93. -3.25157411956062339893e1L,
  94. -2.92929976820724030353e2L,
  95. -1.70730828800510297666e3L,
  96. -7.96667499622741999770e3L,
  97. -2.59780216007146401957e4L,
  98. -5.99650230220855581642e4L,
  99. -7.15743521530849602425e4L
  100. };
  101. static long double Q[] = {
  102. 1.00000000000000000000e0L,
  103. -1.67955233807178858919e1L,
  104. 8.85946791747759881659e1L,
  105. 5.69440799097468430177e1L,
  106. -1.98526250512761318471e3L,
  107. 3.31667508019495079814e3L,
  108. 1.60577839621734713377e4L,
  109. -2.97045081369399940529e4L,
  110. -7.15743521530849602412e4L
  111. };
  112. */
  113. #define MAXGAML 1755.455L
  114. /*static const long double LOGPI = 1.14472988584940017414L;*/
  115. /* Stirling's formula for the gamma function
  116. tgamma(x) = sqrt(2 pi) x^(x-.5) exp(-x) (1 + 1/x P(1/x))
  117. z(x) = x
  118. 13 <= x <= 1024
  119. Relative error
  120. n=8, d=0
  121. Peak error = 9.44e-21
  122. Relative error spread = 8.8e-4
  123. */
  124. static long double STIR[9] = {
  125. 7.147391378143610789273E-4L,
  126. -2.363848809501759061727E-5L,
  127. -5.950237554056330156018E-4L,
  128. 6.989332260623193171870E-5L,
  129. 7.840334842744753003862E-4L,
  130. -2.294719747873185405699E-4L,
  131. -2.681327161876304418288E-3L,
  132. 3.472222222230075327854E-3L,
  133. 8.333333333333331800504E-2L,
  134. };
  135. #define MAXSTIR 1024.0L
  136. static const long double SQTPI = 2.50662827463100050242E0L;
  137. /* 1/tgamma(x) = z P(z)
  138. * z(x) = 1/x
  139. * 0 < x < 0.03125
  140. * Peak relative error 4.2e-23
  141. */
  142. static long double S[9] = {
  143. -1.193945051381510095614E-3L,
  144. 7.220599478036909672331E-3L,
  145. -9.622023360406271645744E-3L,
  146. -4.219773360705915470089E-2L,
  147. 1.665386113720805206758E-1L,
  148. -4.200263503403344054473E-2L,
  149. -6.558780715202540684668E-1L,
  150. 5.772156649015328608253E-1L,
  151. 1.000000000000000000000E0L,
  152. };
  153. /* 1/tgamma(-x) = z P(z)
  154. * z(x) = 1/x
  155. * 0 < x < 0.03125
  156. * Peak relative error 5.16e-23
  157. * Relative error spread = 2.5e-24
  158. */
  159. static long double SN[9] = {
  160. 1.133374167243894382010E-3L,
  161. 7.220837261893170325704E-3L,
  162. 9.621911155035976733706E-3L,
  163. -4.219773343731191721664E-2L,
  164. -1.665386113944413519335E-1L,
  165. -4.200263503402112910504E-2L,
  166. 6.558780715202536547116E-1L,
  167. 5.772156649015328608727E-1L,
  168. -1.000000000000000000000E0L,
  169. };
  170. static const long double PIL = 3.1415926535897932384626L;
  171. static long double stirf ( long double );
  172. /* Gamma function computed by Stirling's formula.
  173. */
  174. static long double stirf(long double x)
  175. {
  176. long double y, w, v;
  177. w = 1.0L/x;
  178. /* For large x, use rational coefficients from the analytical expansion. */
  179. if( x > 1024.0L )
  180. w = (((((6.97281375836585777429E-5L * w
  181. + 7.84039221720066627474E-4L) * w
  182. - 2.29472093621399176955E-4L) * w
  183. - 2.68132716049382716049E-3L) * w
  184. + 3.47222222222222222222E-3L) * w
  185. + 8.33333333333333333333E-2L) * w
  186. + 1.0L;
  187. else
  188. w = 1.0L + w * __polevll( w, STIR, 8 );
  189. y = expl(x);
  190. if( x > MAXSTIR )
  191. { /* Avoid overflow in pow() */
  192. v = powl( x, 0.5L * x - 0.25L );
  193. y = v * (v / y);
  194. }
  195. else
  196. {
  197. y = powl( x, x - 0.5L ) / y;
  198. }
  199. y = SQTPI * y * w;
  200. return( y );
  201. }
  202. long double
  203. tgammal(long double x)
  204. {
  205. long double p, q, z;
  206. int i;
  207. signgam = 1;
  208. if( isnan(x) )
  209. return(NAN);
  210. if(x == INFINITY)
  211. return(INFINITY);
  212. if(x == -INFINITY)
  213. return(x - x);
  214. if( x == 0.0L )
  215. return( 1.0L / x );
  216. q = fabsl(x);
  217. if( q > 13.0L )
  218. {
  219. if( q > MAXGAML )
  220. goto goverf;
  221. if( x < 0.0L )
  222. {
  223. p = floorl(q);
  224. if( p == q )
  225. return (x - x) / (x - x);
  226. i = p;
  227. if( (i & 1) == 0 )
  228. signgam = -1;
  229. z = q - p;
  230. if( z > 0.5L )
  231. {
  232. p += 1.0L;
  233. z = q - p;
  234. }
  235. z = q * sinl( PIL * z );
  236. z = fabsl(z) * stirf(q);
  237. if( z <= PIL/LDBL_MAX )
  238. {
  239. goverf:
  240. return( signgam * INFINITY);
  241. }
  242. z = PIL/z;
  243. }
  244. else
  245. {
  246. z = stirf(x);
  247. }
  248. return( signgam * z );
  249. }
  250. z = 1.0L;
  251. while( x >= 3.0L )
  252. {
  253. x -= 1.0L;
  254. z *= x;
  255. }
  256. while( x < -0.03125L )
  257. {
  258. z /= x;
  259. x += 1.0L;
  260. }
  261. if( x <= 0.03125L )
  262. goto small;
  263. while( x < 2.0L )
  264. {
  265. z /= x;
  266. x += 1.0L;
  267. }
  268. if( x == 2.0L )
  269. return(z);
  270. x -= 2.0L;
  271. p = __polevll( x, P, 7 );
  272. q = __polevll( x, Q, 8 );
  273. z = z * p / q;
  274. if( z < 0 )
  275. signgam = -1;
  276. return z;
  277. small:
  278. if( x == 0.0L )
  279. return (x - x) / (x - x);
  280. else
  281. {
  282. if( x < 0.0L )
  283. {
  284. x = -x;
  285. q = z / (x * __polevll( x, SN, 8 ));
  286. signgam = -1;
  287. }
  288. else
  289. q = z / (x * __polevll( x, S, 8 ));
  290. }
  291. return q;
  292. }