Reputation: 2151
I need to calculate the factorial of a decimal number, say 6.4, on iOS. I tried
double myFactorial = gamma(6.4);
But get the error "'gamma is unavailable': not avaiable on iOS". Is there a way to add the gamma function into iOS?
Upvotes: 2
Views: 1197
Reputation: 8965
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;
}
and then
- (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)));
}
Upvotes: 0
Reputation: 20410
Have you tried:
tgamma(6.4)
I see it working in my code.
There's also:
double tgamma (double x)
float tgammaf (float x)
long double tgammal (long double x)
Upvotes: 3