Namespaces
Variants
Views
Actions

lgamma

From cppreference.com
< c‎ | numeric‎ | math
 
 
 
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99)
(C99)
(C99)(C99)(C99)
Exponential functions
(C99)
(C99)
(C99)
(C99)
Power functions
(C99)
(C99)
Trigonometric and hyperbolic functions
(C99)
(C99)
(C99)
Error and gamma functions
(C99)
(C99)
lgamma
(C99)
(C99)
Nearest integer floating point operations
(C99)(C99)(C99)
(C99)
(C99)(C99)(C99)
Floating point manipulation functions
(C99)(C99)
(C99)
(C99)
Classification
(C99)
(C99)
(C99)
Macro constants
 
Defined in header <math.h>
float       lgammaf( float arg );
(since C99)
double      lgamma( double arg );
(since C99)
long double lgammal( long double arg );
(since C99)

Computes the natural logarithm of the absolute value of the gamma function of arg.

Contents

[edit] Parameters

arg - floating point value

[edit] Return value

The value of the logarithm of the gamma function of arg, that is log
e
|
0
targ-1
e-t dt|
.

If arg is a natural number, lgamma(arg) is the logarithm of the factorial of arg-1.

[edit] Exceptions

If arg is zero or a negative integer, pole error may occur and FE_INVALID or FE_DIVBYZERO may be raised.

If arg is too large, range error may occur and FE_OVERFLOW may be raised.

[edit] Notes

POSIX specification additionally requires that each execution of lgamma stores the sign of the gamma function of arg in the external variable signgam.

[edit] Example

#include <stdio.h>
#include <math.h>
#include <errno.h>
#include <fenv.h>
#include <float.h>
#include <string.h>
 
#pragma STDC FENV_ACCESS ON
 
void show_fe_exceptions(void)
{
    printf("current exceptions raised: ");
    if(fetestexcept(FE_DIVBYZERO))     printf(" FE_DIVBYZERO");
    if(fetestexcept(FE_INEXACT))       printf(" FE_INEXACT");
    if(fetestexcept(FE_INVALID))       printf(" FE_INVALID");
    if(fetestexcept(FE_OVERFLOW))      printf(" FE_OVERFLOW");
    if(fetestexcept(FE_UNDERFLOW))     printf(" FE_UNDERFLOW");
    if(fetestexcept(FE_ALL_EXCEPT)==0) printf(" none");
    printf("\n");
}
 
int main(void)
{
    printf("lgamma(6.0) = %f\n", lgamma(6.0));     // 4.787492
    printf("log(5!)     = %f\n", log(5*4*3*2*1));  // 4.787492
    printf("\n");
 
    printf("lgamma(1.0)       = %+.1f\n", lgamma(1.0));
    printf("lgamma(2.0)       = %+.1f\n", lgamma(2.0));
    printf("lgamma(+INFINITY) = %+f\n",   lgamma(+INFINITY));
    printf("lgamma(-INFINITY) = %+f\n",   lgamma(-INFINITY));
    printf("lgamma(NAN)       = %f\n",    lgamma(NAN));
    printf("\n");
 
    /* range error                         */
    /* "inexact" floating-point exception  */
    /* "overflow" floating-point exception */
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("lgamma(DBL_MAX)   = %f\n",   lgamma(DBL_MAX));
    printf("%s\n",strerror(errno));
    show_fe_exceptions();
    printf("\n");
 
    /* pole error                                */
    /* "divide-by-zero" floating-point exception */
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("lgamma(-1.0)      = %f\n",   lgamma(-1.0));
    printf("%s\n",strerror(errno));
    show_fe_exceptions();
    printf("\n");
 
    /* pole error                                */
    /* "divide-by-zero" floating-point exception */
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("lgamma(0.0)       = %f\n",   lgamma(0.0));
    printf("%s\n",strerror(errno));
    show_fe_exceptions();
 
    return 0;
}

Possible output:

lgamma(6.0) = 4.787492
log(5!)     = 4.787492
 
lgamma(1.0)       = +0.0
lgamma(2.0)       = +0.0
lgamma(+INFINITY) = +inf
lgamma(-INFINITY) = +inf
lgamma(NAN)       = nan
 
lgamma(DBL_MAX)   = inf
Numerical result out of range
current exceptions raised:  FE_INEXACT FE_OVERFLOW
 
lgamma(-1.0)      = inf
Numerical result out of range
current exceptions raised:  FE_DIVBYZERO
 
lgamma(0.0)       = inf
Numerical result out of range
current exceptions raised:  FE_DIVBYZERO

[edit] See also

(C99)
gamma function
(function) [edit]

[edit] External links

Weisstein, Eric W. "Log Gamma Function." From MathWorld--A Wolfram Web Resource.