overflow upper incomplete gamma function

I want to calculate the following equation: $$\frac{\theta \Gamma \left(\kappa+1,\frac{o}{\theta }\right)-o \Gamma \left(\kappa,\frac{o}{\theta }\right)}{\Gamma (\kappa)}+o+s$$ with $$s>0, o>0, \kappa>0, \theta>0$$ and in which $$\Gamma(\kappa,x)$$ represents the upper incomplete gamma function and $$\Gamma(\kappa)$$ the (true) gamma function.

My current implementation of this equation in c++ using boost tgamma returns an overflow for large $$\kappa$$ values. Is there a way to transform this equation?

• DLMF 8.8.2 might help here, in addition to gammatester's answer: $$\Gamma(k+1,z) = k \,\Gamma(k,z)+z^k e^{-z}.$$ Oct 22, 2015 at 20:45

This should be obvious, but the first thing you should try is to use the normalized incomplete gamma function $Q(a,x)=\frac{\Gamma(a,x)}{\Gamma(a)}$ for the two summands in the numerator of your fraction (for the first you have to add one recurrence step for $k+1$). In Boost $Q(a,x)$ is called gamma_q.

Using the recurrence step and the normalized incomplete gamma function definition I simplified the formula to:

$$\theta e^{k \log \left(\frac{o}{\theta }\right)-\frac{o}{\theta }- \ln{\Gamma (\kappa)}} +\theta \kappa Q \left(\kappa,\frac{o}{\theta }\right)-o Q \left(\kappa,\frac{o}{\theta }\right)+o+s$$

In this equation $$Q$$ stands for the normalized upper incomplete gamma function ( gamma_q in boost): $$Q_{\kappa,x}=\frac{\Gamma(\kappa,x)}{\Gamma(\kappa)}$$

The $$ln(\Gamma(\kappa))$$ can be implemented by using lgamma in boost which so far gives me no overflow error for large $$\kappa$$ values.