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