In C, lgamma() returns the natural logarithm of the absolute value of the gamma function. In short form, it computes log(|Γ(x)|) for a double argument.
Gamma grows very fast, so the log form is safer for large inputs. The gamma value itself can be negative for some non-integer negatives, so libraries also expose a simple sign. Many platforms set the global signgam to +1 or -1 for that sign.
Pass one double. Include <math.h>. For beginners, start with positive arguments such as integers, where the sign is positive and the result is easy to check.
Syntax
Pass the input to the log-gamma function. Start with this call.
C
lgamma(x);
Call
What it does
lgamma(1.0)
Returns 0 because Γ(1) is 1 and log(1) is 0.
lgamma(3.0)
Returns about 0.693 because Γ(3) is 2 and log(2) is ln 2.
lgamma(0.5)
Returns a positive log value for Γ(0.5).
Include <math.h>. On GCC, link with -lm. Related helpers are lgammaf, lgammal, and the plain gamma function tgamma.
Library declarations
C
double lgamma(double x);
float lgammaf(float x);
long double lgammal(long double x);
/* many platforms also expose: extern int signgam; */
Parameters
Parameter
Description
x
Required. The double input to the log-gamma calculation.
Return Value
Return Value
Description
double
The value of log(|Γ(x)|). On error, a library-defined error value may be returned.
C Examples
Example 1 — Basic Log-Gamma Values
Compute lgamma for small positive integers and print the results.