NAG Library Routine Document

s14abf (gamma_log_real)


    1  Purpose
    7  Accuracy


s14abf returns the value of the logarithm of the gamma function, lnΓx, via the function name.


Fortran Interface
Function s14abf ( x, ifail)
Real (Kind=nag_wp):: s14abf
Integer, Intent (Inout):: ifail
Real (Kind=nag_wp), Intent (In):: x
C Header Interface
#include nagmk26.h
double  s14abf_ (const double *x, Integer *ifail)


s14abf 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 x02bef).
Let ε denote machine precision and let xhuge denote the largest positive model number (see x02alf). For x<0.0 the value lnΓx is not defined; s14abf returns zero and exits with ifail=1. It also exits with ifail=1 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 s14abf exits with ifail=2 and returns xhuge. The value of xbig is given in the Users' Note for your implementation.


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


1:     x – Real (Kind=nag_wp)Input
On entry: the argument x of the function.
Constraint: x>0.0.
2:     ifail – IntegerInput/Output
On entry: ifail must be set to 0, -1​ or ​1. If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value -1​ or ​1 is recommended. If the output of error messages is undesirable, then the value 1 is recommended. Otherwise, if you are not familiar with this argument, the recommended value is 0. When the value -1​ or ​1 is used it is essential to test the value of ifail on exit.
On exit: ifail=0 unless the routine detects an error or a warning has been flagged (see Section 6).

Error Indicators and Warnings

If on entry ifail=0 or -1, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
On entry, x0.0. If x<0.0 the function is undefined; on soft failure, the function value returned is zero. If x=0.0 and soft failure is selected, the function value returned is the largest machine number (see x02alf).
On entry, x>xbig (see Section 3). On soft failure, the function value returned is the largest machine number (see x02alf).
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.


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 routine'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.

Parallelism and Performance

s14abf is not threaded in any implementation.

Further Comments



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

Program Text

Program Text (s14abfe.f90)

Program Data

Program Data (s14abfe.d)

Program Results

Program Results (s14abfe.r)

GnuplotProduced by GNUPLOT 4.6 patchlevel 3 0 2 4 6 8 0 1 2 3 4 5 6 7 8 lnΓ(x) x Example Program Returned Values for the Logarithm of the Gamma Function, lnΓ(x) gnuplot_plot_1
© The Numerical Algorithms Group Ltd, Oxford, UK. 2017