# How do I overcome an Overflow?

I'm trying to calculate entropies for an absolutely giant system by counting states, and this means I have to use some obscenely large numbers. I'm running

Nn = 5*10^20
- 1.381 * 10^-23  Log[Multinomial[Nn/3, Nn/3, Nn/3]]


and (of COURSE) it comes out to an obscenely large number. Honestly, I would be alright with just the order of magnitude without having to do each factorial estimation separately by hand... Are there any little tricks to getting around this Overflow[]? I'm sure Mathematica has some workarounds for avoiding machine precision errors.... there's no way this number is eating all my 8 gigs of ram.

For reference, the output of $MaxNumber is 1.605216761933662*10^1355718576299609. • LogGamma[1 + Nn] - 3 LogGamma[1 + Nn/3] is quite huge ($\approx 5.5\times 10^{20}$); no wonder you're hitting overflow when it's the exponent. – J. M.'s torpor May 12 '16 at 2:25 • Yeah.... Is there any gross hacky way to get it to happily store bigger numbers? – laudiacay May 12 '16 at 2:29 • Also, @J.M. it's not the exponent, it's just being multiplied by the Boltzmann constant up front. – laudiacay May 12 '16 at 2:33 • I see; in any event, I've already shown you an expression you can use instead of Log[Multinomial[Nn/3, Nn/3, Nn/3]]; LogGamma[] is often the thing to use in situations like yours. – J. M.'s torpor May 12 '16 at 2:35 ## 2 Answers Since your number is quite large, you can use Stirling's approximation to do this. It's also very common to use this approximation in statistical mechanics: For large number$n\$

$$\log(n!)\approx n\log(n)-n$$

So in your case

$$n=\frac{Nn}{3}$$

then

$$\log\left[\frac{(3n)!}{n!\times n!\times n!}\right]\\ =\log[(3n)!]-3\log[n!]\\ \approx 3n \log(3n)-3n \log(n)$$

So

-1.381*10^-23 Nn*(Log[Nn] - Log[Nn/3])
(*-0.00758592*)

• As expected, this is close to the result of -QuantityMagnitude[UnitConvert[Quantity[1, "BoltzmannConstant"]]] (LogGamma[1 + Nn] - 3 LogGamma[1 + Nn/3]) /. Nn -> 5.*^20 – J. M.'s torpor May 12 '16 at 2:42

It looks like there isn't a good way to bypass Overflow[]. However, for very large factorial calculations, it's useful and incredibly accurate to use Stirling or Nemes approximations, depending on the size of the factorial.

@Mathematica devs, an idea- maybe catch overflow errors, tell Factorial to substitute the Stirling or Nemes approximation, then try to make some cancellations with the rest of the calculation before plugging in the factorial argument? This would work SUPER well on Bi/Multinomial calculations, because a bunch of Stirling cancels out.

• ...or, work with expressions in terms of LogGamma[] as much as possible, and delay the exponentiation as long as you can. – J. M.'s torpor May 12 '16 at 2:30
• That also works. I wonder, do you know how are they calculating Log(Gamma()) well? – laudiacay May 12 '16 at 2:34
• This claims that "recursion, functional equations, and the Binet asymptotic formula" are used for Gamma[]; probably suitable modifications of these are done for LogGamma[]. – J. M.'s torpor May 12 '16 at 2:39
• Oh wow, Binet is like tailor-made for taking logs. That's so cool- thanks! – laudiacay May 12 '16 at 2:57