lgamma(), lgammaf(), lgammal() — Log gamma function
Standards
Standards / Extensions | C or C++ | Dependencies |
---|---|---|
XPG4 |
both |
Format
#define _XOPEN_SOURCE
#include <math.h>
double lgamma(double x);
extern int signgam;
int *__signgam(void);
C99:
#define _ISOC99_SOURCE
#include <math.h>
double lgamma(double x);
float lgammaf(float x);
long double lgammal(long double x);
C++
TR1 C99:
#define _TR1_C99
#include <math.h>
float lgamma(float x);
long double lgamma(long double x);
General description
The lgamma()
function computes the
where
is defined as
The sign of
is returned in the external integer signgam. The argument x may not be a non-positive integer.
In a multithreaded process, each thread has its own
instance of the signgam variable. Threads
access their instances of the variable by calling the __signgam()
function. See __signgam() — Return signgam reference. The math.h
header (see math.h) redefines the string
“signgam” to an invocation of
the __signham function. The actual signgam external
variable is used to store the signgam value
for the IPT.
Note: The following table shows the viable formats for
these functions. See IEEE binary floating-point for
more information about IEEE Binary Floating-Point.
Function | Hex | IEEE |
---|---|---|
lgamma | X | X |
lgammaf | X | X |
lgammal | X | X |
Returned value
If successful, lgamma() returns the above function of its argument.
lgamma() will fail under
the following conditions:
- If the result overflows, the function will return HUGE_VAL and set errno to ERANGE.
- If x is a non-positive integer and _XOPEN_SOURCE is defined, lgamma() returns HUGE_VAL and sets errno to EDOM.
- If x is a non-positive integer and _ISOC99_SOURCE is defined, lgamma() returns HUGE_VAL and sets errno to ERANGE.
Note: If both _XOPEN_SOURCE and _ISOC99_SOURCE are defined, the
_ISOC99_SOURCE behavior will take precedence.
Special behavior for IEEE: Even when _XOPEN_SOURCE is defined and _ISOC99_SOURCE not, lgamma() returns HUGE_VAL and sets errno to ERANGE.
Example
/*
This example uses lgamma() to calculate ln(|G(x)|), where x = 42.
*/
#include <math.h>
#include <stdio.h>
int main(void)
{
double x=42, g_at_x;
g_at_x = exp(lgamma(x)); /* g_at_x = 3.345253e+49 */
printf ("The value of G(%4.2f) is %7.2e\n", x, g_at_x);
}
Output
The value of G(42.00) is 3.35e+49