C++ std::lgamma() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cmath>

Definition

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!)
CallWhat 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

ParameterDescription
xRequired. The number to evaluate. It can be float, double, long double, or an integer type (converted to double).

Return Value

Return ValueDescription
ln(|gamma(x)|)The natural logarithm of the absolute gamma value. It can be negative, for example for x between 1 and 2.
infReturned for 0 and for negative whole numbers (a pole error), and for infinite input.
NaNReturned 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;
}

How It Works

  1. The gamma values of 1 and 2 are both 1, so their logarithm is 0.
  2. lgamma(3) is ln(2), which is 0.693147, and lgamma(5) is ln(24), which is 3.17805.
  3. lgamma(0.5) is ln(1.77245), about 0.572365. lgamma(10) is ln(362880), about 12.8018.

Example 2 — The Log of Big Factorials Without Overflow

Compare lgamma(n + 1) with tgamma(n + 1) for three values of n. The factorial of 1000 is too big for a double, but its logarithm is easy.

C++
#include <cmath>
#include <iostream>

int main() {
    for (int n : {10, 100, 1000}) {
        std::cout << "n = " << n
                  << ": ln(n!) = " << std::lgamma(n + 1.0)
                  << ", n! = " << std::tgamma(n + 1.0) << '\n';
    }
    return 0;
}

How It Works

  1. Both functions get n + 1.0, because the factorial of n is the gamma function of n + 1.
  2. For n = 10 and n = 100 both columns work: 100! is about 9.33262e+157 and its logarithm is 363.739.
  3. The factorial of 1000 is too large for a double (the limit is about 1.8e308), so tgamma() returns inf. The logarithm 5912.13 is still correct.

Example 3 — Count Combinations with Logarithms

The number of ways to choose k items from n is n! / (k! * (n - k)!). Doing it with logarithms avoids huge middle values.

C++
#include <cmath>
#include <iostream>

double choose(int n, int k) {
    double logResult = std::lgamma(n + 1.0)
                     - std::lgamma(k + 1.0)
                     - std::lgamma(n - k + 1.0);
    return std::exp(logResult);
}

int main() {
    std::cout << "C(10, 3)  = " << std::llround(choose(10, 3)) << '\n';
    std::cout << "C(52, 5)  = " << std::llround(choose(52, 5)) << '\n';
    std::cout << "C(100, 50) is about " << choose(100, 50) << '\n';
    return 0;
}

How It Works

  1. choose() adds and subtracts the logarithms of the three factorials, and then calls exp() once to get back to a normal number.
  2. 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.
  3. 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.

Next: llrint()

Keep going through the <cmath> functions. Round to the nearest long long integer.

llrint() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful