Hi,
I have the following code that uses Inline::C to compute factorial. The problem is that whenever I use large N (>30) it began to return "INF".
I thougtht my C implementation already cater large number by using "float"? Additionally I have already added Math::Pari as an attempt to solve the problem, but again no avail.
Can anybody suggest what's the way to deal with this?
Here is my complete code:
#----------Beginning of my code ---------------
#!/usr/bin/perl -w use Inline C; use strict; use Data::Dumper; use Math::Pari qw/ :int/;
my $n = factrl(30); # it fails if I try factrl(40) print "$n\n"
__END__ __C__
#include <stdio.h>
float factrl(int n) { float gammln(float xx); void nerror(char error_text[]); static int ntop=4; static float a[33]={1.0,1.0,2.0,6.0,24.0}; int j;
if (n<0) nerror("Negative factorial in routine factrl"); if (n>32) return exp(gammln(n+1.0));
while (ntop<n){ j = ntop++; a[ntop]=a[j]*ntop; }
return a[n];
}
float gammln(float xx) { double x,y, tmp,ser; static double cof[6] = {76.18009172947146, -86.50532032941677, 24.01409824083091, -1.231739572450155, 0.12086509738661e-2, -0.5395239384953e-5}; int j;
y = x = xx; tmp= x+5.5; tmp -= (x+0.5)*log(tmp); ser=1.000000000190015;
for (j=0;j<=5;j++) ser += cof[j]/++y; return -tmp+log(2.5066282746310005*ser/x); }
#-----End of My Code--------------
-- Edward WIJAYA Singapore
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>