No, no! Please, don't use that approximation for statistical tests!
If you want to implement a routine for other purposes, where you precision is not that important you can use it. If you need precision, however, (i.e. to compute exact P-values for some statistical tests), the best compromise between efficiency and accuracy is to use the gamma approximation:
Code:
#include <stdio.h>
#include <math.h>
int x;
long double f;
long double lnfactorial( int a);
int main(void)
{
printf("Enter an integer :");
scanf("%d" , &x);
f = lnfactorial(x);
printf("\nThe ln factorial of %d is %9.12Lf\n", x, f);
return 0;
}
long double lnfactorial( int a)
{
long double z;
if (a == 1)
return 0;
else
{
z = lgamma(a+1);
return (z);
}
}
The code above calculates the natural logarithm of n! for very large numbers of n using the gamma approximation. For me, (32-bits, linux) the maximum n can be 2147483646, since 2147483647 is the limit for a 32-bits system.