e_tgammal.c 6.4 KB

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