nag_log_gamma (s14abc) (PDF version)
s Chapter Contents
s Chapter Introduction
NAG C Library Manual

NAG Library Function Document

nag_log_gamma (s14abc)

+ Contents

    1  Purpose
    7  Accuracy

1  Purpose

nag_log_gamma (s14abc) returns the value of the logarithm of the gamma function, lnΓx.

2  Specification

#include <nag.h>
#include <nags.h>
double  nag_log_gamma (double x, NagError *fail)

3  Description

nag_log_gamma (s14abc) calculates an approximate value for lnΓx. It is based on rational Chebyshev expansions.
Denote by Rn,mix=Pnix/Qmix a ratio of polynomials of degree n in the numerator and m in the denominator. Then:
For each expansion, the specific values of n and m are selected to be minimal such that the maximum relative error in the expansion is of the order 10-d, where d is the maximum number of decimal digits that can be accurately represented for the particular implementation (see nag_decimal_digits (X02BEC)).
Let ε denote machine precision and let xhuge denote the largest positive model number (see nag_real_largest_number (X02ALC)). For x<0.0 the value lnΓx is not defined; nag_log_gamma (s14abc) returns zero and exits with fail.code= NE_REAL_ARG_LE. It also exits with fail.code= NE_REAL_ARG_LE when x=0.0, and in this case the value xhuge is returned. For x in the interval 0.0,ε, the function lnΓx=-lnx to machine accuracy.
Now denote by xbig the largest allowable argument for lnΓx on the machine. For xbig1/4<xxbig the Rn,m41/x2 term in Equation (1) is negligible. For x>xbig there is a danger of setting overflow, and so nag_log_gamma (s14abc) exits with fail.code= NE_REAL_ARG_GT and returns xhuge. The value of xbig is given in the Users' Note for your implementation.

4  References

Abramowitz M and Stegun I A (1972) Handbook of Mathematical Functions (3rd Edition) Dover Publications
Cody W J and Hillstrom K E (1967) Chebyshev approximations for the natural logarithm of the gamma function Math.Comp. 21 198–203

5  Arguments

1:     xdoubleInput
On entry: the argument x of the function.
Constraint: x>0.0.
2:     failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

6  Error Indicators and Warnings

NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
NE_REAL_ARG_GT
On entry, x=value.
Constraint: xxbig.
NE_REAL_ARG_LE
On entry, x=value.
Constraint: x>0.0.

7  Accuracy

Let δ and ε be the relative errors in the argument and result respectively, and E be the absolute error in the result.
If δ is somewhat larger than machine precision, then
E x×Ψx δ   and   ε x×Ψx lnΓ x δ
where Ψx is the digamma function Γx Γx . Figure 1 and Figure 2 show the behaviour of these error amplification factors.
Figure 1
Figure 1
Figure 2
Figure 2
These show that relative error can be controlled, since except near x=1​ or ​2 relative error is attenuated by the function or at least is not greatly amplified.
For large x, ε1+ 1lnx δ and for small x, ε 1lnx δ.
The function lnΓx has zeros at x=1 and 2 and hence relative accuracy is not maintainable near those points. However absolute accuracy can still be provided near those zeros as is shown above.
If however, δ is of the order of machine precision, then rounding errors in the function's internal arithmetic may result in errors which are slightly larger than those predicted by the equalities. It should be noted that even in areas where strong attenuation of errors is predicted the relative precision is bounded by the effective machine precision.

8  Further Comments

None.

9  Example

This example reads values of the argument x from a file, evaluates the function at each value of x and prints the results.

9.1  Program Text

Program Text (s14abce.c)

9.2  Program Data

Program Data (s14abce.d)

9.3  Program Results

Program Results (s14abce.r)


nag_log_gamma (s14abc) (PDF version)
s Chapter Contents
s Chapter Introduction
NAG C Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2012