Hi,

I have the following code that uses Inline::C to compute factorial.
The problem is that whenever I use really large N (>500) it began to return
"INF". On average I need to deal with N=1000-2000.

Additionally I have already done these things as an attempt to solve
the problem, but again no avail:

1. added Math::Pari module
2. use "double" and even "long double" for type declaration on C section.

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 = factoriall(30);  # it fails if I try factrl(500)
print "$n\n"


__END__ __C__

#include <stdio.h>


double factorial_inline(int n) { double gammln(double xx); void nerror(char error_text[]); static int ntop=4; static double 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];

}

double gammln(double 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

Reply via email to