tgammal.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /* origin: OpenBSD /usr/src/lib/libm/src/ld80/e_tgammal.c */
  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. /*
  18. * Gamma function
  19. *
  20. *
  21. * SYNOPSIS:
  22. *
  23. * long double x, y, tgammal();
  24. *
  25. * y = tgammal( x );
  26. *
  27. *
  28. * DESCRIPTION:
  29. *
  30. * Returns gamma function of the argument. The result is
  31. * correctly signed.
  32. *
  33. * Arguments |x| <= 13 are reduced by recurrence and the function
  34. * approximated by a rational function of degree 7/8 in the
  35. * interval (2,3). Large arguments are handled by Stirling's
  36. * formula. Large negative arguments are made positive using
  37. * a reflection formula.
  38. *
  39. *
  40. * ACCURACY:
  41. *
  42. * Relative error:
  43. * arithmetic domain # trials peak rms
  44. * IEEE -40,+40 10000 3.6e-19 7.9e-20
  45. * IEEE -1755,+1755 10000 4.8e-18 6.5e-19
  46. *
  47. * Accuracy for large arguments is dominated by error in powl().
  48. *
  49. */
  50. #include "libm.h"
  51. #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
  52. long double tgammal(long double x)
  53. {
  54. return tgamma(x);
  55. }
  56. #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384
  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 const 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 const 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 const 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 const 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 const 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 const 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 const 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. /* Gamma function computed by Stirling's formula.
  168. */
  169. static long double stirf(long double x)
  170. {
  171. long double y, w, v;
  172. w = 1.0/x;
  173. /* For large x, use rational coefficients from the analytical expansion. */
  174. if (x > 1024.0)
  175. w = (((((6.97281375836585777429E-5L * w
  176. + 7.84039221720066627474E-4L) * w
  177. - 2.29472093621399176955E-4L) * w
  178. - 2.68132716049382716049E-3L) * w
  179. + 3.47222222222222222222E-3L) * w
  180. + 8.33333333333333333333E-2L) * w
  181. + 1.0;
  182. else
  183. w = 1.0 + w * __polevll(w, STIR, 8);
  184. y = expl(x);
  185. if (x > MAXSTIR) { /* Avoid overflow in pow() */
  186. v = powl(x, 0.5L * x - 0.25L);
  187. y = v * (v / y);
  188. } else {
  189. y = powl(x, x - 0.5L) / y;
  190. }
  191. y = SQTPI * y * w;
  192. return y;
  193. }
  194. long double tgammal(long double x)
  195. {
  196. long double p, q, z;
  197. int i, sign;
  198. if (isnan(x))
  199. return NAN;
  200. if (x == INFINITY)
  201. return INFINITY;
  202. if (x == -INFINITY)
  203. return x - x;
  204. q = fabsl(x);
  205. if (q > 13.0) {
  206. sign = 1;
  207. if (q > MAXGAML)
  208. goto goverf;
  209. if (x < 0.0) {
  210. p = floorl(q);
  211. if (p == q)
  212. return (x - x) / (x - x);
  213. i = p;
  214. if ((i & 1) == 0)
  215. sign = -1;
  216. z = q - p;
  217. if (z > 0.5L) {
  218. p += 1.0;
  219. z = q - p;
  220. }
  221. z = q * sinl(PIL * z);
  222. z = fabsl(z) * stirf(q);
  223. if (z <= PIL/LDBL_MAX) {
  224. goverf:
  225. return sign * INFINITY;
  226. }
  227. z = PIL/z;
  228. } else {
  229. z = stirf(x);
  230. }
  231. return sign * z;
  232. }
  233. z = 1.0;
  234. while (x >= 3.0) {
  235. x -= 1.0;
  236. z *= x;
  237. }
  238. while (x < -0.03125L) {
  239. z /= x;
  240. x += 1.0;
  241. }
  242. if (x <= 0.03125L)
  243. goto small;
  244. while (x < 2.0) {
  245. z /= x;
  246. x += 1.0;
  247. }
  248. if (x == 2.0)
  249. return z;
  250. x -= 2.0;
  251. p = __polevll(x, P, 7);
  252. q = __polevll(x, Q, 8);
  253. z = z * p / q;
  254. return z;
  255. small:
  256. if (x == 0.0)
  257. return (x - x) / (x - x);
  258. if (x < 0.0) {
  259. x = -x;
  260. q = z / (x * __polevll(x, SN, 8));
  261. } else
  262. q = z / (x * __polevll(x, S, 8));
  263. return q;
  264. }
  265. #endif