[Previous] [Contents] [Next]

lgamma(), lgamma_r(), lgammaf(), lgammaf_r()

Log gamma function

Synopsis:

#include <math.h>

double lgamma( double x );

double lgamma_r( double x,
                 int* signgamp);

float lgammaf( float x );

float lgammaf_r( float x,
                 int* signgamp);

Library:

libm

Description:

The lgamma() and lgamma_r() functions return the natural log (ln) of the gamma() function and is equivalent to gamma(). These functions return ln|Gamma(x)|, where Gamma(x) is defined as follows:

For x > 0:
integral from 0 to +Infinity of pow(t,x-1)*exp(-t) dt

For x < 1:
n / (Gamma( 1-x ) * sin( nx ))

(The results converge when x is between 0 and 1.)

The gamma math function has the property G(N) = G(N-1)*N (it grows fast, so that's why log is used).

     lgamma(x) = ln (lgamma(x)) = gamma(x)

The lgamma() and lgammaf() functions use the external integer signgam to return the sign of Gamma(x) while lgamma_r() and lgammaf_r() use the user-allocated space addressed by signgamp.


Note: The signgam variable isn't set until lgamma() or lgammaf() returns. For example, don't use the expression:
g = signgam * exp( lgamma( x ));

to compute g = Gamma(x)'. Instead, compute lgamma() first:

lg = lgamma(x); 
g = signgam * exp( lg );

Note that Gamma(x) must overflow when x is large enough, underflow when -x is large enough, and generate a division by 0 exception at the singularities x a nonpositive integer.

Returns:

The gamma math function.

Examples:

#include <stdio.h>
#include <errno.h>
#include <inttypes.h>
#include <math.h>
#include <fpstatus.h>

int main(int argc, char** argv) 
{
    double a, b;

    errno = EOK;
    a = 0.5;
    b = lgamma(a);
    printf("lgamma(%f) = %f %d \n", a, b, errno);

    return(0);
}

produces the output:

lgamma(0.500000) = 0.572365 0

Classification:

lgamma() is standard Unix; lgamma_r(), lgammaf(), and lgammaf_r() are ANSI (draft)

Safety:
Cancellation point No
Interrupt handler No
Signal handler No
Thread Yes

See also:

gamma()


[Previous] [Contents] [Next]