...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
#include <boost/math/special_functions/gamma.hpp>
namespace boost{ namespace math{ template <class T1, class T2> calculatedresulttype tgamma_ratio(T1 a, T2 b); template <class T1, class T2, class Policy> calculatedresulttype tgamma_ratio(T1 a, T2 b, const Policy&); template <class T1, class T2> calculatedresulttype tgamma_delta_ratio(T1 a, T2 delta); template <class T1, class T2, class Policy> calculatedresulttype tgamma_delta_ratio(T1 a, T2 delta, const Policy&); }} // namespaces
template <class T1, class T2> calculatedresulttype tgamma_ratio(T1 a, T2 b); template <class T1, class T2, class Policy> calculatedresulttype tgamma_ratio(T1 a, T2 b, const Policy&);
Returns the ratio of gamma functions:
The final Policy argument is optional and can be used to control the behaviour of the function: how it handles errors, what level of precision to use etc. Refer to the policy documentation for more details.
Internally this just calls tgamma_delta_ratio(a,
ba)
.
template <class T1, class T2> calculatedresulttype tgamma_delta_ratio(T1 a, T2 delta); template <class T1, class T2, class Policy> calculatedresulttype tgamma_delta_ratio(T1 a, T2 delta, const Policy&);
Returns the ratio of gamma functions:
The final Policy argument is optional and can be used to control the behaviour of the function: how it handles errors, what level of precision to use etc. Refer to the policy documentation for more details.
Note that the result is calculated accurately even when delta is small compared to a: indeed even if a+delta ~ a. The function is typically used when a is large and delta is very small.
The return type of these functions is computed using the result type calculation rules when T1 and T2 are different types, otherwise the result type is simple T1.
The following table shows the peak errors (in units of epsilon) found on various platforms with various floating point types. Unless otherwise specified any floating point type that is narrower than the one shown will have effectively zero error.
Table 6.7. Error rates for tgamma_delta_ratio
Microsoft Visual C++ version 12.0 
GNU C++ version 5.1.0 
GNU C++ version 5.1.0 
Sun compiler version 0x5130 


tgamma + small delta ratios 
Max = 10.1ε (Mean = 1.25ε) 
Max = 0ε (Mean = 0ε) 
Max = 5.56ε (Mean = 0.969ε) 
Max = 15.4ε (Mean = 2.09ε) 
tgamma + small delta ratios (negative delta) 
Max = 8.04ε (Mean = 1.31ε) 
Max = 0ε (Mean = 0ε) 
Max = 8.67ε (Mean = 1.29ε) 
Max = 18.3ε (Mean = 2.03ε) 
tgamma + small integer ratios 
Max = 2.74ε (Mean = 0.736ε) 
Max = 0ε (Mean = 0ε) 
Max = 1.96ε (Mean = 0.677ε) 
Max = 1.96ε (Mean = 0.677ε) 
tgamma + small integer ratios (negative delta) 
Max = 2.15ε (Mean = 0.685ε) 
Max = 0ε (Mean = 0ε) 
Max = 1.62ε (Mean = 0.451ε) 
Max = 1.62ε (Mean = 0.451ε) 
integer tgamma ratios 
Max = 0.968ε (Mean = 0.386ε) 
Max = 0ε (Mean = 0ε) 
Max = 0.997ε (Mean = 0.4ε) 
Max = 0.997ε (Mean = 0.4ε) 
integer tgamma ratios (negative delta) 
Max = 0.974ε (Mean = 0.184ε) 
Max = 0ε (Mean = 0ε) 
Max = 0.853ε (Mean = 0.176ε) 
Max = 0.853ε (Mean = 0.176ε) 
Table 6.8. Error rates for tgamma_ratio
Microsoft Visual C++ version 12.0 
GNU C++ version 5.1.0 
GNU C++ version 5.1.0 
Sun compiler version 0x5130 


tgamma ratios 
Max = 3.66ε (Mean = 1.27ε) 
Max = 0ε (Mean = 0ε) 
Max = 3.09ε (Mean = 1.15ε) 
Max = 174ε (Mean = 61.2ε) 
Accuracy tests use data generated at very high precision (with NTL RR class set at 1000bit precision: about 300 decimal digits) and a deliberately naive calculation of Γ(x)/Γ(y).
The implementation of these functions is very similar to that of beta, and is based on combining similar power terms to improve accuracy and avoid spurious overflow/underflow.
In addition there are optimisations for the situation where delta is a small integer: in which case this function is basically the reciprocal of a rising factorial, or where both arguments are smallish integers: in which case table lookup of factorials can be used to calculate the ratio.