# NAG C Library Function Document

## 1Purpose

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

## 2Specification

 #include #include
 double nag_log_gamma (double x, NagError *fail)

## 3Description

nag_log_gamma (s14abc) 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 + x R n,m 1 x+1 ;$
• for $1/2,
 $ln⁡Γx ≈ x-1 R n,m 1 x ;$
• for $3/2,
 $ln⁡Γx ≈ x-2 R n,m 2 x;$
• for $4,
 $ln⁡Γx ≈ R n,m 3 x ;$
• and for $12,
 $ln⁡Γx ≈ x-12 lnx - x + ln2π + 1x R n,m 4 1/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 nag_decimal_digits (X02BEC)).
Let $\epsilon$ denote machine precision and let ${x}_{\mathrm{huge}}$ denote the largest positive model number (see nag_real_largest_number (X02ALC)). For $x<0.0$ the value $\mathrm{ln}\Gamma \left(x\right)$ is not defined; nag_log_gamma (s14abc) returns zero and exits with ${\mathbf{fail}}\mathbf{.}\mathbf{code}=$ NE_REAL_ARG_LE. It also exits with ${\mathbf{fail}}\mathbf{.}\mathbf{code}=$ NE_REAL_ARG_LE 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 nag_log_gamma (s14abc) exits with ${\mathbf{fail}}\mathbf{.}\mathbf{code}=$ NE_REAL_ARG_GT and returns ${x}_{\mathrm{huge}}$. The value of ${x}_{\mathrm{big}}$ is given in the Users' Note for your implementation.
NIST Digital Library of Mathematical Functions
Cody W J and Hillstrom K E (1967) Chebyshev approximations for the natural logarithm of the gamma function Math.Comp. 21 198–203

## 5Arguments

1:    $\mathbf{x}$doubleInput
On entry: the argument $x$ of the function.
Constraint: ${\mathbf{x}}>0.0$.
2:    $\mathbf{fail}$NagError *Input/Output
The NAG error argument (see Section 3.7 in How to Use the NAG Library and its Documentation).

## 6Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 2.3.1.2 in How to Use the NAG Library and its Documentation for further information.
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.
See Section 2.7.6 in How to Use the NAG Library and its Documentation for further information.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 2.7.5 in How to Use the NAG Library and its Documentation for further information.
NE_REAL_ARG_GT
On entry, ${\mathbf{x}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{x}}\le {x}_{\mathrm{big}}$.
NE_REAL_ARG_LE
On entry, ${\mathbf{x}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{x}}>0.0$.

## 7Accuracy

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×Ψx ln⁡Γ 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  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 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.

## 8Parallelism and Performance

nag_log_gamma (s14abc) is not threaded in any implementation.

None.

## 10Example

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

### 10.1Program Text

Program Text (s14abce.c)

### 10.2Program Data

Program Data (s14abce.d)

### 10.3Program Results

Program Results (s14abce.r)
{"ft_lang_label":"__label__en","ft_lang_prob":0.85935175,"math_prob":0.9990394,"size":3483,"snap":"2021-31-2021-39","text_gpt3_token_len":779,"char_repetition_ratio":0.1218741,"word_repetition_ratio":0.0670194,"special_character_ratio":0.21935113,"punctuation_ratio":0.121875,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9998865,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,1,null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-19T23:57:13Z\",\"WARC-Record-ID\":\"<urn:uuid:5ebee252-06df-4bb1-abca-9bc388ff44bd>\",\"Content-Length\":\"32988\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:6b641f17-7deb-41db-a71a-b127d4ea22bd>\",\"WARC-Concurrent-To\":\"<urn:uuid:bb0de1e6-7416-4c90-a37a-4f8cd2b87e0a>\",\"WARC-IP-Address\":\"78.129.168.4\",\"WARC-Target-URI\":\"https://www.nag.com/numeric/cl/nagdoc_latest/html/s/s14abc.html\",\"WARC-Payload-Digest\":\"sha1:GJQ4JDFSKGJZRXO2A7AGN3PXMZT573PU\",\"WARC-Block-Digest\":\"sha1:D2DB3BNWWZ4DEKNZDAGSPQMX36BAKZGG\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780056902.22_warc_CC-MAIN-20210919220343-20210920010343-00575.warc.gz\"}"}