繁体   English   中英

在iOS上计算小数的阶乘(即Gamma函数)

[英]Calculate factorial of a decimal (i.e. Gamma function) on iOS

我需要在iOS上计算十进制数的阶乘,比如说6.4。 我试过了

double myFactorial = gamma(6.4);

但得到错误“'伽玛不可用':在iOS上不可用”。 有没有办法将伽玛功能添加到iOS?

你有没有尝试过:

tgamma(6.4)

我看到它在我的代码中工作。

还有:

double tgamma (double x)
float tgammaf (float x)
long double tgammal (long double x)
you can try like this logic may be this will well you.
- (int)factorial:(int)operand
{
    if`enter code here`(operand < 0)
        return -1;
    else if (operand > 1)
        return operand * [self factorial:operand - 1];
    else
        return 1;
}

接着

- (double)factorial:(double)operand
{
    double output = operand;

    if      (output == 0) output = 1; // factorial of 0 is 1
    else if (output < 0)  output = NAN;
    else if (output > 0)
    {
        if (fmod(output, floor(output)) == 0) // integer
            output = round(exp(lgamma(output + 1)));
        else // natural number
            output = exp(lgamma(output + 1));
    }

    return output;
}

- (double)n:(double)n chooseR:(double)r
{
    return round(exp((lgamma(n+1)) - (lgamma(r+1) + lgamma(n-r+1))));
}

- (double)n:(double)n pickR:(double)r
{
    return round(exp(lgamma(n+1) - lgamma(n-r+1)));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM