s14ab returns the value of the logarithm of the gamma function, $\mathrm{ln} \Gamma \left(x\right)$.

# Syntax

C#
```public static double s14ab(
double x,
out int ifail
)```
Visual Basic
```Public Shared Function s14ab ( _
x As Double, _
<OutAttribute> ByRef ifail As Integer _
) As Double```
Visual C++
```public:
static double s14ab(
double x,
[OutAttribute] int% ifail
)```
F#
```static member s14ab :
x : float *
ifail : int byref -> float
```

#### Parameters

x
Type: System..::..Double
On entry: the argument $x$ of the function.
Constraint: ${\mathbf{x}}>0.0$.
ifail
Type: System..::..Int32%
On exit: ${\mathbf{ifail}}={0}$ unless the method detects an error or a warning has been flagged (see [Error Indicators and Warnings]).

#### Return Value

s14ab returns the value of the logarithm of the gamma function, $\mathrm{ln} \Gamma \left(x\right)$.

# Description

s14ab calculates an approximate value for $\mathrm{ln} \Gamma \left(x\right)$. It is based on rational Chebyshev expansions.
Denote by ${R}_{n,m}^{i}\left(x\right)={P}_{n}^{i}\left(x\right)/{Q}_{m}^{i}\left(x\right)$ a ratio of polynomials of degree $n$ in the numerator and $m$ in the denominator. Then:
• for $0,
 $ln Γx≈-lnx+xRn,m1x+1;$
• for $1/2,
 $ln Γx≈x-1Rn,m1x;$
• for $3/2,
 $ln Γx≈x-2Rn,m2x;$
• for $4,
 $ln Γx≈Rn,m3x;$
• and for $12,
 $ln Γx≈x-12lnx-x+ln2π+1xRn,m41/x2.$ (1)
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 x02be).
Let $\epsilon$ denote machine precision and let ${x}_{\mathrm{huge}}$ denote the largest positive model number (see x02al). For $x<0.0$ the value $\mathrm{ln} \Gamma \left(x\right)$ is not defined; s14ab returns zero and exits with ${\mathbf{ifail}}={1}$. It also exits with ${\mathbf{ifail}}={1}$ when $x=0.0$, and in this case the value ${x}_{\mathrm{huge}}$ is returned. For $x$ in the interval $\left(0.0,\epsilon \right]$, the function $\mathrm{ln} \Gamma \left(x\right)=-\mathrm{ln}\left(x\right)$ to machine accuracy.
Now denote by ${x}_{\mathrm{big}}$ the largest allowable argument for $\mathrm{ln} \Gamma \left(x\right)$ on the machine. For ${\left({x}_{\mathrm{big}}\right)}^{1/4} the ${R}_{n,m}^{4}\left(1/{x}^{2}\right)$ term in Equation (1) is negligible. For $x>{x}_{\mathrm{big}}$ there is a danger of setting overflow, and so s14ab exits with ${\mathbf{ifail}}={2}$ and returns ${x}_{\mathrm{huge}}$. The value of ${x}_{\mathrm{big}}$ is given in the Users' Note for your implementation.

# 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

# Error Indicators and Warnings

Errors or warnings detected by the method:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{x}}\le 0.0$. If ${\mathbf{x}}<0.0$ the function is undefined; on failure, the function value returned is zero. If ${\mathbf{x}}=0.0$ and failure is selected, the function value returned is the largest machine number (see x02al).
${\mathbf{ifail}}=2$
On entry, ${\mathbf{x}}>{x}_{\mathrm{big}}$ (see [Description]). On failure, the function value returned is the largest machine number (see x02al).
${\mathbf{ifail}}=-9000$
An error occured, see message report.

# Accuracy

Let $\delta$ and $\epsilon$ be the relative errors in the argument and result respectively, and $E$ be the absolute error in the result.
If $\delta$ is somewhat larger than machine precision, then
 $E≃x×Ψxδ and ε≃x×Ψxln Γxδ$
where $\Psi \left(x\right)$ is the digamma function $\frac{{\Gamma }^{\prime }\left(x\right)}{\Gamma \left(x\right)}$. Figure 1 and Figure 2 show the behaviour of these error amplification factors.
Figure 1
Figure 2
These show that relative error can be controlled, since except near $x=1\text{​ or ​}2$ relative error is attenuated by the function or at least is not greatly amplified.
For large $x$, $\epsilon \simeq \left(1+\frac{1}{\mathrm{ln} x}\right)\delta$ and for small $x$, $\epsilon \simeq \frac{1}{\mathrm{ln} x}\delta$.
The function $\mathrm{ln} \Gamma \left(x\right)$ 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, $\delta$ is of the order of machine precision, then rounding errors in the method'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.

None.

None.

# Example

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

Example program (C#): s14abe.cs

Example program data: s14abe.d

Example program results: s14abe.r