In C++, std::lgamma(x) returns ln(|gamma(x)|), the natural logarithm of the gamma function. The gamma function grows so fast that tgamma() overflows for x above about 171. Its logarithm grows slowly, so lgamma() never has that problem.
This is the standard trick for big factorials. Since n! = gamma(n + 1), the value lgamma(n + 1) is ln(n!). You can add and subtract these logarithms to work with huge numbers such as binomial coefficients, and use std::exp() at the end.
std::lgamma() is declared in <cmath> and is part of the group Special functions. Special functions appear in statistics and science. They are ready to use in <cmath>, so you do not need to write them yourself.
Syntax
Include <cmath> and call std::lgamma. Start with these lines.
C++
double a = std::lgamma(1.0); // 0 (ln 1)
double b = std::lgamma(5.0); // 3.17805 (ln 24)
double c = std::lgamma(101.0); // 363.739 (ln of 100!)
Call
What it does
std::lgamma(1.0)
The gamma of 1 is 1, and ln(1) is 0. Returns 0.
std::lgamma(5.0)
The gamma of 5 is 24. Returns ln(24), about 3.17805.
std::lgamma(101.0)
The logarithm of 100!. Returns about 363.739, with no overflow.
The result is the logarithm of the absolute value, so the sign of the gamma value is lost. For negative non-whole numbers, tgamma() may be negative while lgamma() shows only its size.
Library declarations
C++
float lgamma(float x);
double lgamma(double x);
long double lgamma(long double x);
double lgamma(Integer x); // for integer types
Parameters
Parameter
Description
x
Required. The number to evaluate. It can be float, double, long double, or an integer type (converted to double).
Return Value
Return Value
Description
ln(|gamma(x)|)
The natural logarithm of the absolute gamma value. It can be negative, for example for x between 1 and 2.
inf
Returned for 0 and for negative whole numbers (a pole error), and for infinite input.
NaN
Returned if x is NaN.
C++ Examples
Example 1 — Logarithm of Gamma Values
Print lgamma() for whole numbers and one half step. The results are logarithms of 1, 1, 2, 24, 362880 and 1.77245.
C++
#include <cmath>
#include <iostream>
int main() {
double values[] = {1.0, 2.0, 3.0, 5.0, 0.5, 10.0};
for (double x : values) {
std::cout << "lgamma(" << x << ") = " << std::lgamma(x) << '\n';
}
return 0;
}
C(10, 3) = 120
C(52, 5) = 2598960
C(100, 50) is about 1.00891e+29
How It Works
choose() adds and subtracts the logarithms of the three factorials, and then calls exp() once to get back to a normal number.
The answers are rounded with std::llround(), because exp() can leave a tiny error. C(10, 3) is 120 and C(52, 5) is 2598960, the number of five-card poker hands.
C(100, 50) is about 1.00891e+29. Computing 100! directly would give a value near 1e158 first, but the logarithm sum never gets that big.
Frequently Asked Questions
std::lgamma(x) returns the natural logarithm of the absolute value of the gamma function. For a whole number n, lgamma(n + 1) is ln(n!).
ln(|gamma(x)|), the natural logarithm of the absolute gamma value. It is 0 for x = 1 and x = 2, and infinity for zero and negative whole numbers.
tgamma(x) overflows to infinity for x above about 171.6, and then log(inf) is useless. lgamma(x) computes the logarithm directly, so it works for huge x such as 1000000.
Call std::lgamma(n + 1.0). For example, lgamma(101.0) is ln(100!), about 363.739.
Include <cmath> and write std::lgamma(). That always works and picks the C++ overloads. The global name ::lgamma often works too, but the standard does not promise it, and it may only take a double.