3 ms·
I added this comment there: You can remove the overflow problems by modifying the code so that it computes ln(x!) as lnfact = .5ln(math.pi)+x(ln(x)-1) lnfact
by mjcohen 4y ago
I added this comment there:
You can remove the overflow problems by modifying the code so that it computes ln(x!) as
lnfact = .5ln(math.pi)+x(ln(x)-1)
lnfact += ln(((8x + 4)x + 1)*x + 1/30.)/6.
- waynecochran 4y agoNice. The plot indicates that using ln should flatten things out. Helps avoid the (x/e)**x expression which is problematic.
- adgjlsfhk1 4y agothe way you actually are supposed to implement that is x*x exp(-x). Otherwise you will have bad accuracy for larger numbers.
- prox 4y agoToo afraid to ask : what’s a factorial approximation and why do we need it?
- czzr 4y agoDo you know what a factorial is? 3 factorial is written as 3! and is defined as 3! = 3x2x1. So three factorial equals 6. That’s easy. What about 100! Or 124! Obviously you could calculate that by multiplying all the numbers but that’s slow. A quicker way is to use Ramanujan’s formula which will give you an answer with much less calculation. The catch is that the answer may not be exactly correct, just a very good approximation. For many applications this is not an issue, and a faster calculation is more useful than a slightly more accurate answer.
- prox 4y agoCool! Now I get it!