During development of a data compression routine, I needed to implement $log(x!)$ as it came up in entropy estimation.
As $x!$ is impractical to represent as an intermediate result due to its magnitude growing very quickly with $x$, I set out to find an approximation as that's good enough for my use case.
My first idea was that $log(x!) \lt log(x^x)$, and thus $x\cdot log(x)$ shouldn't be too far off, just a bit too large. But then I noticed that the difference between the true value and this approximation was very nearly equal to $x$
Thus, my approximation at that point was:
$$log(x!) \approx x(log(x) - 1)$$
(Which is honestly good enough for my usecase as-is)
However, by plotting the error for large values of $x$, I noticed the error was roughly proportional to $log(x)$
From that, I obtained a better approximation:
$$log(x!) \approx x(log(x) - 1) + \frac{log(x)}{2}$$
Again using incrementally larger values of $x$ to see how the error behaved, I noticed the error seemed to decrease as the values grew larger, seemingly converging towards a constant. As I had no idea what the closed form of this constant could be, I put the decimal expansion into a search engine to see if anything probable came up. This was in fact the case, and $log(2\pi)/2$ matches all digits of floating point precision I could obtain.
Thus, I finally have:
$$log(x!) \approx x(log(x) - 1) + \frac{log(x)}{2} + \frac{log(2\pi)}{2}$$
My question is then:
- Can it be shown that this approximation does indeed not stray away from the exact solution even if $x$ grows very large?
(I strongly suspect this must have name since it's immediately useful. Knowing what this approximation strategy is called will most likely solve my problem, since I will then be able to easily look up relevant information)
- Has my very informal quest for an approximation missed an exact closed form that could be used instead?