I’ve been working on a statistics library in Typst recently, distro. One fun outcome of this has been the chance to revisit some special functions in mathematics, and explore how to implement them in a numerically safe way. For instance, Typst uses i64 under the hood when implementing the calc.binom function, and it overflows for moderately large n (Issue #4993). Now, the factorial function grows super-exponentially, so the recursive computation of $$ n! = \begin{cases}n(n-1)!& n \ge 1 \\ 1& n = 0 \end{cases} $$quickly exceeds the range of fixed-width integer values. I was curious about just how quickly this actually occurs. That is, for what values of $n$ does $n!$ overflow a $b$-bit integer? # An inequality using Stirling’s approximation Stirling’s approximation gives an asymptotic approximation*Asymptotic here means that the approximation becomes increasingly accurate as $n\to\infty$ for the factorial function: $$ n!\sim {\sqrt {2\pi n}}\left({\frac {n}{e}}\right)^{n} $$I’ve been…
No comments yet. Log in to reply on the Fediverse. Comments will appear here.