summaryrefslogtreecommitdiff
path: root/devdocs/c/numeric%2Fmath%2Flgamma.html
blob: 5dbfcbd3f5cb94b3543077add0fdd54b60e72191 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
    <h1 id="firstHeading" class="firstHeading">lgamma, lgammaf, lgammal</h1>            <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;math.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">float       lgammaf( float arg );</pre>
</td> <td> (1) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">double      lgamma( double arg );</pre>
</td> <td> (2) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">long double lgammal( long double arg );</pre>
</td> <td> (3) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;tgmath.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">#define lgamma( arg )</pre>
</td> <td> (4) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr>  </table> <div class="t-li1">
<span class="t-li">1-3)</span> Computes the natural logarithm of the absolute value of the <a href="https://en.wikipedia.org/wiki/Gamma_function" class="extiw" title="enwiki:Gamma function">gamma function</a> of <code>arg</code>.</div> <div class="t-li1">
<span class="t-li">4)</span> Type-generic macro: If <code>arg</code> has type <code>long double</code>, <code>lgammal</code> is called. Otherwise, if <code>arg</code> has integer type or the type <code>double</code>, <code>lgamma</code> is called. Otherwise, <code>lgammaf</code> is called.</div>  <h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> arg </td> <td> - </td> <td> floating point value </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p>If no errors occur, the value of the logarithm of the gamma function of <code>arg</code>, that is \(\log_{e}|{\int_0^\infty t^{arg-1} e^{-t} \mathsf{d}t}|\)log<sub class="t-su t-su-b">e</sub>|∫<sub class="t-su t-su-b">∞0</sub><i>t</i><sup class="t-su">arg-1</sup> <i>e</i><sup>-t</sup> d<i>t</i>|, is returned.</p>
<p>If a pole error occurs, <code><a href="huge_val" title="c/numeric/math/HUGE VAL">+HUGE_VAL</a></code>, <code>+HUGE_VALF</code>, or <code>+HUGE_VALL</code> is returned.</p>
<p>If a range error due to overflow occurs, <code><a href="huge_val" title="c/numeric/math/HUGE VAL">±HUGE_VAL</a></code>, <code>±HUGE_VALF</code>, or <code>±HUGE_VALL</code> is returned.</p>
<h3 id="Error_handling"> Error handling</h3> <p>Errors are reported as specified in <a href="math_errhandling" title="c/numeric/math/math errhandling"><code>math_errhandling</code></a>.</p>
<p>If <code>arg</code> is zero or is an integer less than zero, a pole error may occur.</p>
<p>If the implementation supports IEEE floating-point arithmetic (IEC 60559),</p>
<ul>
<li> If the argument is 1, +0 is returned. </li>
<li> If the argument is 2, +0 is returned. </li>
<li> If the argument is ±0, +∞ is returned and <code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_DIVBYZERO</a></code> is raised. </li>
<li> If the argument is a negative integer, +∞ is returned and <code><a href="../fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">FE_DIVBYZERO</a></code> is raised. </li>
<li> If the argument is ±∞, +∞ is returned. </li>
<li> If the argument is NaN, NaN is returned. </li>
</ul> <h3 id="Notes"> Notes</h3> <p>If <code>arg</code> is a natural number, <code>lgamma(arg)</code> is the logarithm of the factorial of <code>arg - 1</code>.</p>
<p>The <a rel="nofollow" class="external text" href="https://pubs.opengroup.org/onlinepubs/9699919799/functions/lgamma.html">POSIX version of <code>lgamma</code></a> is not thread-safe: each execution of the function stores the sign of the gamma function of <code>arg</code> in the static external variable <code>signgam</code>. Some implementations provide <code>lgamma_r</code>, which takes a pointer to user-provided storage for singgam as the second parameter, and is thread-safe.</p>
<p>There is a non-standard function named <code>gamma</code> in various implementations, but its definition is inconsistent. For example, glibc and 4.2BSD version of <code>gamma</code> executes <code>lgamma</code>, but 4.4BSD version of <code>gamma</code> executes <code>tgamma</code>.</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;stdio.h&gt;
#include &lt;math.h&gt;
#include &lt;float.h&gt;
#include &lt;errno.h&gt;
#include &lt;fenv.h&gt;
// #pragma STDC FENV_ACCESS ON
 
int main(void)
{
    printf("lgamma(10) = %f, log(9!) = %f\n", lgamma(10), log(2*3*4*5*6*7*8*9));
    const double pi = acos(-1);
    printf("lgamma(0.5) = %f, log(sqrt(pi)) = %f\n", log(sqrt(pi)), lgamma(0.5));
    // special values
    printf("lgamma(1) = %f\n", lgamma(1));
    printf("lgamma(+Inf) = %f\n", lgamma(INFINITY));
 
    // error handling
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("lgamma(0) = %f\n", lgamma(0));
    if (errno == ERANGE)
        perror("    errno == ERANGE");
    if (fetestexcept(FE_DIVBYZERO))
        puts("    FE_DIVBYZERO raised");
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">lgamma(10) = 12.801827, log(9!) = 12.801827
lgamma(0.5) = 0.572365, log(sqrt(pi)) = 0.572365
lgamma(1) = 0.000000
lgamma(+Inf) = inf
lgamma(0) = inf
    errno == ERANGE: Numerical result out of range
    FE_DIVBYZERO raised</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul>
<li> 7.12.8.3 The lgamma functions (p: 182) </li>
<li> 7.25 Type-generic math &lt;tgmath.h&gt; (p: 272-273) </li>
<li> F.10.5.3 The lgamma functions (p: 383) </li>
</ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.12.8.3 The lgamma functions (p: 250) </li>
<li> 7.25 Type-generic math &lt;tgmath.h&gt; (p: 373-375) </li>
<li> F.10.5.3 The lgamma functions (p: 525) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul>
<li> 7.12.8.3 The lgamma functions (p: 231) </li>
<li> 7.22 Type-generic math &lt;tgmath.h&gt; (p: 335-337) </li>
<li> F.9.5.3 The lgamma functions (p: 462) </li>
</ul>
</ul>                 <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="tgamma" title="c/numeric/math/tgamma"> <span class="t-lines"><span>tgamma</span><span>tgammaf</span><span>tgammal</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c99">(C99)</span></span></span></div> </td> <td> computes gamma function <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/numeric/math/lgamma" title="cpp/numeric/math/lgamma">C++ documentation</a></span> for <code>lgamma</code> </td>
</tr> </table> <h3 id="External_links"> External links</h3> <table> <tr> <td>
<a rel="nofollow" class="external text" href="https://mathworld.wolfram.com/LogGammaFunction.html">Weisstein, Eric W. "Log Gamma Function."</a> From MathWorld — A Wolfram Web Resource. </td>
</tr>
</table>           <div class="_attribution">
  <p class="_attribution-p">
    &copy; cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
    <a href="https://en.cppreference.com/w/c/numeric/math/lgamma" class="_attribution-link">https://en.cppreference.com/w/c/numeric/math/lgamma</a>
  </p>
</div>