in reply to Stirling Approx to N! for large Number?
The correction is that you misunderstood what I suggested would be more accurate. Try this line for the log(n!):
Try it, it should be better.my $log_nfact = $n * log ($n) - $n + 0.5 * (log(2*PI*$n + 1/3));
The suggestion is that your problem was about binomial probabilities. Rather than try to calculate the factorials and work with them directly, estimate the logs of the factorials and use them to calculate the log of n choose m. Only then use the exponential.
For n and m large enough you'll again run into problems with Perl's support for large numbers. But since the final number is more modest than the intermediate factorials, you'll be able to go much farther. And when you get beyond what Perl can handle natively, if you have the log, you can divide it by log(10) to get the log base 10, and now you can take the integer and fractional parts of that and readily convert your answer into scientific notation for display even though Perl's native scientific notation doesn't go that high.
And that latter strategy will work for any n and m that you're likely to encounter in practice.
Update: Fixed algebra per kvale's note. (What I get for posting fast on a late night.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Stirling Approx to N! for large Number?
by kvale (Monsignor) on Mar 25, 2005 at 06:59 UTC |