Gamma function: Difference between revisions

Content deleted Content added
Tikkanz (talk | contribs)
m →‎{{header|J}}: fix links and tags
→‎{{header|C}}: -improve spouge implementation , double # of terms calcd
Line 352:
}
 
#define A 12
int factorial(int n)
double sp_gamma(double x, double **fmz)
{
const int a = 6A;
if ( (n==0) || (n==1) ) return 1;
static double c_space[A];
if ( n==2 ) return 2;
return n * factorial(n-1);
}
 
double sp_gamma(double x, double **fm)
{
const int a = 6;
static double *c = NULL;
int k;
double resaccm;
 
if ( c == NULL ) {
double k1_factrl = 1.0; /* (k - 1)!*(-1)^k with 0!==1*/
c = malloc(sizeof(double) * a);
*fmc = cc_space;
c[0] = sqrt(2.0*M_PI);
for(k=1; k < a; k++) {
c[k] = exp(((a-k-1)%2==0)?1:-1) * exppow(a-k, k-0.5) */ k1_factrl;
k1_factrl *= -k;
pow(a-k, k-1.0/2.0) / (double)factorial(k-1);
}
}
resaccm = c[0];
for(k=1; k < a; k++) {
resaccm += c[k] / ( xz + k );
}
resaccm *= exp(-(xz+a)) * pow(xz+a, x z+ 1.0/2.05); /* Gamma(z+1) */
return resaccm/xz;
}
 
int main()
{
double x, *fm;
 
 
printf("%15s%15s%15s\n", "Stirling", "Spouge", "GSL");
for(x=1.0; x <= 10.0; x+=1.0) {
printf("%15.8lf%15.8lf%15.8lf\n", st_gamma(x/3.0), sp_gamma(x/3.0, &fm),
gsl_sf_gamma(x/3.0));
}
free(fm);
return 0;
}</lang>
Line 400 ⟶ 394:
 
<pre> Stirling Spouge GSL
2.15697602 2.6789385167893853 2.67893853
1.20285073 1.3541179235411794 1.35411794
0.92213701 01.9999999800000000 1.00000000
0.83974270 0.8929795089297951 0.89297951
0.85919025 0.9027452790274529 0.90274529
0.95950218 01.9999999800000000 1.00000000
1.14910642 1.1906393219063935 1.19063935
1.45849038 1.5045754450457549 1.50457549
1.94540320 12.9999999400000000 2.00000000
2.70976382 2.7781583977815848 2.77815848</pre>
 
=={{header|Fortran}}==