我之前发布过这个,用户告诉我要在codereview上发布。是的,他们把它关了…所以再来一次:(我删除了旧问题)
我有以下公式:
c - 泊松计算(erlang C)-LMLPHP
我需要erlangC公式的Poisson公式:
c - 泊松计算(erlang C)-LMLPHP
我试着用C重新构建公式:

double getPoisson(double m, double u, bool cumu)
{
    double ret = 0;
    if(!cumu)
    {
        ret = (exp(-u)*pow(u,m)) / (factorial(m));
    }
    else
    {
        double facto = 1;
        double ehu = exp(-u);
        for(int i = 0; i < m; i++)
        {
            ret = ret + (ehu * pow(u,i)) / facto;
            facto *= (i+1);
        }
     }
     return ret;
}

Erlang C公式:
double getErlangC(double m, double u, double p)
{
    double numerator = getPoisson(m, u, false);
    double denominator = getPoisson(m, u, false) + (1-p) * getPoisson(m, u, true);
    return numerator/denominator;
}

主要问题是,m中的getPoisson参数是一个大值(>170)
所以它想计算>170!但它无法处理。我认为原始数据类型太小,无法正常工作,或者你怎么说?
顺便说一句:这是我第一个泊松方程的阶乘函数:
double factorial(double n)
{
    if(n >= 1)
        return n*factorial(n-1);
    else
        return 1;
}

一些样品:
输入:
double l = getErlangC(50, 48, 0.96);
printf("%g", l);

输出:
0.694456 (correct)

输入:
double l = getErlangC(100, 96, 0.96);
printf("%g", l);

输出:
0.5872811 (correct)

如果对getErlangC的第一个参数(m)使用大于170的值,例如:
输入:
double l = getErlangC(500, 487, 0.974);
printf("%g", l);

输出:
naN (incorrect)

除外:
0.45269

我的方法怎么样?有没有更好的方法来计算Poisson和erlangC?
一些信息:Excel有POISSON函数,在Excel上它可以执行。。。有没有办法查看EXCEL用于POISSON的算法(代码)?

最佳答案

(pow(u, m)/factorial(m))可以表示为递归循环,每个元素显示为u/n,其中每个n是m的元素!.

double ratio(double u, int n)
{
    if(n > 0)
     {
        // Avoid the ratio overflow by calculating each ratio element
        double val;
        val = u/n;
        return val*ratio(u, n-1);
      }
    else
      {
         // Avoid division by 0 as power and factorial of 0 are 1
        return 1;
      }
}

注意,如果要避免递归,也可以将其作为循环来执行
double ratio(double u, int n)
{
    int i;
    // Avoid the ratio overflow by calculating each ratio element
    // default the ratio to 1 for n == 0
    double val = 1;
    // calculate the next n-1 ratios and put them into the total
    for (i = 1; i<=n; i++)
      {
        // Put in the next element of the ratio
        val *=  u/i;
      }
    // return the final value of the ratio
    return val;
}

关于c - 泊松计算(erlang C),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39110184/

10-09 18:09