我曾尝试为卡方PDF编写函数;但是我收到以下错误:
Q4.c: In function ‘GAMMA’: Q4.c:31:2: error: expected identifier or ‘(’ before ‘long’ Q4.c:37:2: error: expected ‘(’ before ‘{’ token Q4.c:38:3: error: ‘factor’ undeclared (first use in this function) Q4.c:38:3: note: each undeclared identifier is reported only once for each function it appears in Q4.c:43:4: error: ‘multiplier’ undeclared (first use in this function)
我无法弄清楚为什么会收到此错误。如果您提供一些建议或见解,我将不胜感激。
#include <stdio.h>
#include <math.h>
#include <limits.h>
#include <stdlib.h>
long double factorial (long double);
//Now we define it,
long double
factorial(long double n)
{
//Here s is the free parameter which is increased by one in each step and
//pro is the initial product and by setting pro to be 0 we also cover the
//case of zero factorial.
int s = 1;
long double pro = 1;
//Here pro stands for product.
if (n < 0)
printf("Factorial is not defined for a negative number \n");
else {
while (n >= s) {
pro *= s;
s++;
}
return pro;
}
}
long double GAMMA(int);
long double
GAMMA(int v)
{
int i = 1,
long double factor, multiplier = sqrtl(M_PI);
if((v % 2) == 0)
{
return factorial((v / 2) - 1);
}
else if
{
factor = (v / 2) - i;
while(v / 2 - (i + 1) > 0)
{
i++;
factor = v / 2 - i;
multiplier *= factor;
}
return multiplier;
}
}
long double ChisquarePDF(long double, int);
long double
ChisquarePDF(long double x, int v)
{
return powl(x, v/2 - 1) / ( powl(2, v/2) * GAMMA(v / 2) * expl(x / 2)) ;
}
int main()
{
printf("%Lf \n", GAMMA(5));
}
答案 0 :(得分:3)
此声明:
int i = 1,
long double factor, multiplier = sqrtl(M_PI);
应该用分号分隔:
int i = 1;
long double factor, multiplier = sqrtl(M_PI);
答案 1 :(得分:2)
在这一行:
int i = 1,
long double factor, multiplier = sqrtl(M_PI);
,
之后,您有一个逗号;
而不是分号int i=1
。
接下来这一行:
else if
if缺少它的表达式来评估。如果没有任何表达,请在那里只写else
。