Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Gamma

Synopsis
#include <boost/math/special_functions/gamma.hpp>
namespace boost{ namespace math{

template <class T>
calculated-result-type tgamma(T z);

template <class T, class Policy>
calculated-result-type tgamma(T z, const Policy&);

template <class T>
calculated-result-type tgamma1pm1(T dz);

template <class T, class Policy>
calculated-result-type tgamma1pm1(T dz, const Policy&);

}} // namespaces
Description
template <class T>
calculated-result-type tgamma(T z);

template <class T, class Policy>
calculated-result-type tgamma(T z, const Policy&);

Returns the "true gamma" (hence name tgamma) of value z:

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.

The return type of this function is computed using the result type calculation rules: the result is double when T is an integer type, and T otherwise.

template <class T>
calculated-result-type tgamma1pm1(T dz);

template <class T, class Policy>
calculated-result-type tgamma1pm1(T dz, const Policy&);

Returns tgamma(dz + 1) - 1. Internally the implementation does not make use of the addition and subtraction implied by the definition, leading to accurate results even for very small dz.

The return type of this function is computed using the result type calculation rules: the result is double when T is an integer type, and T otherwise.

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.

Accuracy

The following table shows the peak errors (in units of epsilon) found on various platforms with various floating point types, along with comparisons to other common libraries. Unless otherwise specified any floating point type that is narrower than the one shown will have effectively zero error.

Table 8.1. Error rates for tgamma

GNU C++ version 7.1.0
linux
double

GNU C++ version 7.1.0
linux
long double

Sun compiler version 0x5150
Sun Solaris
long double

Microsoft Visual C++ version 14.1
Win32
double

factorials

Max = 0ε (Mean = 0ε)

(GSL 2.1: Max = 3.95ε (Mean = 0.783ε))
(Rmath 3.2.3: Max = 314ε (Mean = 93.4ε))

Max = 2.67ε (Mean = 0.617ε)

(<cmath>: Max = 1.66ε (Mean = 0.584ε))
(<math.h>: Max = 1.66ε (Mean = 0.584ε))

Max = 172ε (Mean = 41ε)

(<math.h>: Max = 0ε (Mean = 0ε))

Max = 1.85ε (Mean = 0.566ε)

(<math.h>: Max = 3.17ε (Mean = 0.928ε))

near 0

Max = 0ε (Mean = 0ε)

(GSL 2.1: Max = 4.51ε (Mean = 1.92ε))
(Rmath 3.2.3: Max = 1ε (Mean = 0.335ε))

Max = 2ε (Mean = 0.608ε)

(<cmath>: Max = 1ε (Mean = 0.376ε))
(<math.h>: Max = 1ε (Mean = 0.376ε))

Max = 2ε (Mean = 0.647ε)

(<math.h>: Max = 0.5ε (Mean = 0.0791ε))

Max = 1.5ε (Mean = 0.635ε)

(<math.h>: Max = 1ε (Mean = 0.405ε))

near 1

Max = 0ε (Mean = 0ε)

(GSL 2.1: Max = 4.41ε (Mean = 1.81ε))
(Rmath 3.2.3: Max = 1ε (Mean = 0.32ε))

Max = 2.51ε (Mean = 1.02ε)

(<cmath>: Max = 0.918ε (Mean = 0.203ε))
(<math.h>: Max = 0.918ε (Mean = 0.203ε))

Max = 3.01ε (Mean = 1.06ε)

(<math.h>: Max = 1ε (Mean = 0.175ε))

Max = 1.1ε (Mean = 0.59ε)

(<math.h>: Max = 1ε (Mean = 0.4ε))

near 2

Max = 0ε (Mean = 0ε)

(GSL 2.1: Max = 7.95ε (Mean = 3.12ε))
(Rmath 3.2.3: Max = 1ε (Mean = 0.191ε))

Max = 4.1ε (Mean = 1.55ε)

(<cmath>: Max = 0.558ε (Mean = 0.298ε))
(<math.h>: Max = 0.558ε (Mean = 0.298ε))

Max = 5.01ε (Mean = 1.89ε)

(<math.h>: Max = 0ε (Mean = 0ε))

Max = 2ε (Mean = 0.733ε)

(<math.h>: Max = 0ε (Mean = 0ε))

near -10

Max = 0ε (Mean = 0ε)

(GSL 2.1: Max = 2.6ε (Mean = 1.05ε))
(Rmath 3.2.3: Max = 34.9ε (Mean = 9.2ε))

Max = 1.75ε (Mean = 0.895ε)

(<cmath>: Max = 2.26ε (Mean = 1.08ε))
(<math.h>: Max = 2.26ε (Mean = 1.08ε))

Max = 1.75ε (Mean = 0.819ε)

(<math.h>: Max = 0ε (Mean = 0ε))

Max = 1.86ε (Mean = 0.881ε)

(<math.h>: Max = 0.866ε (Mean = 0.445ε))

near -55

Max = 0ε (Mean = 0ε)

(GSL 2.1: Max = 1.8ε (Mean = 0.782ε))
(Rmath 3.2.3: Max = 3.89e+04ε (Mean = 9.52e+03ε))

Max = 2.69ε (Mean = 1.09ε)

(<cmath>: Max = 1.79ε (Mean = 0.75ε))
(<math.h>: Max = 1.79ε (Mean = 0.75ε))

Max = 98.5ε (Mean = 53.4ε)

(<math.h>: Max = 0ε (Mean = 0ε))

Max = 2.7ε (Mean = 1.35ε)

(<math.h>: Max = 3.87e+04ε (Mean = 6.71e+03ε))


Table 8.2. Error rates for tgamma1pm1

GNU C++ version 7.1.0
linux
double

GNU C++ version 7.1.0
linux
long double

Sun compiler version 0x5150
Sun Solaris
long double

Microsoft Visual C++ version 14.1
Win32
double

tgamma1pm1(dz)

Max = 0ε (Mean = 0ε)

Max = 1.12ε (Mean = 0.49ε)

Max = 6.61ε (Mean = 0.84ε)

Max = 3.31ε (Mean = 0.517ε)


The following error plot are based on an exhaustive search of the functions domain, MSVC-15.5 at double precision, and GCC-7.1/Ubuntu for long double and __float128.

Testing

The gamma is relatively easy to test: factorials and half-integer factorials can be calculated exactly by other means and compared with the gamma function. In addition, some accuracy tests in known tricky areas were computed at high precision using the generic version of this function.

The function tgamma1pm1 is tested against values calculated very naively using the formula tgamma(1+dz)-1 with a lanczos approximation accurate to around 100 decimal digits.

Implementation

The generic version of the tgamma function is implemented Sterling's approximation for lgamma for large z:

Following exponentiation, downward recursion is then used for small values of z.

For types of known precision the Lanczos approximation is used, a traits class boost::math::lanczos::lanczos_traits maps type T to an appropriate approximation.

For z in the range -20 < z < 1 then recursion is used to shift to z > 1 via:

For very small z, this helps to preserve the identity:

For z < -20 the reflection formula:

is used. Particular care has to be taken to evaluate the z * sin(π * z) part: a special routine is used to reduce z prior to multiplying by π to ensure that the result in is the range [0, π/2]. Without this an excessive amount of error occurs in this region (which is hard enough already, as the rate of change near a negative pole is exceptionally high).

Finally if the argument is a small integer then table lookup of the factorial is used.

The function tgamma1pm1 is implemented using rational approximations devised by JM in the region -0.5 < dz < 2. These are the same approximations (and internal routines) that are used for lgamma, and so aren't detailed further here. The result of the approximation is log(tgamma(dz+1)) which can fed into expm1 to give the desired result. Outside the range -0.5 < dz < 2 then the naive formula tgamma1pm1(dz) = tgamma(dz+1)-1 can be used directly.


PrevUpHomeNext