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

时间:2013-10-03 14:37:11

标签: ios math ios7

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

double myFactorial = gamma(6.4);

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

2 个答案:

答案 0 :(得分:3)

你试过了吗?

tgamma(6.4)

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

还有:

double tgamma (double x)
float tgammaf (float x)
long double tgammal (long double x)

答案 1 :(得分:0)

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)));
}