[C] - 伽马函数计算(可求小数)

来源:http://zhidao.baidu.com/question/90734366.html

 

简单的说就是整数阶乘的推广,它有一个积分的表达式:
Γ(x)=∫e^(-t)*t^(x-1)dt (积分的下限式0,上限式+∞)
算法源自《常用算法程序集》徐士良

#include "stdio.h"

double Gamma(x)
double x;
{
int i;
double y,t,s,u;
static double a[11]={ 0.0000677106,-0.0003442342, 0.0015397681,
-0.0024467480, 0.0109736958,-0.0002109075,
0.0742379071, 0.0815782188, 0.4118402518,
0.4227843370, 1.0};
if (x<=0.0)
{
printf(
"err**x<=0!\n");
return(-1.0);
}

y
=x;
if (y<=1.0)
{
t
=1.0/(y*(y+1.0));
y
=y+2.0;
}
else
if(y<=2.0)
{
t
=1.0/y;
y
=y+1.0;
}
else
if(y<=3.0)
{
t
=1.0;
}
else
{
t
=1.0;
while (y>3.0)
{
y
=y-1.0;
t
=t*y;
}
}
s
=a[0];
u
=y-2.0;
for (i=1; i<=10; i++)
{
s
=s*u+a[i];
}
s
=s*t;
return(s);
}

double DecimalFactorial(double x)
{
return Gamma(x+1);
}

void main()
{
printf(
"%f\n",DecimalFactorial(3.5));
}

 

 

求小数阶乘: 3.5!=Γ(x+1)=11.631730

posted @ 2010-04-26 12:11  炎峰森林影  阅读(3432)  评论(0编辑  收藏  举报