2

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

double myFactorial = gamma(6.4);

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

4

2 回答 2

3

你有没有尝试过:

tgamma(6.4)

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

还有:

double tgamma (double x)
float tgammaf (float x)
long double tgammal (long double x)
于 2013-10-03T14:40:52.123 回答
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)));
}
于 2013-10-03T15:53:20.007 回答