C++ std::tgamma() Function

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

Definition

In C++, std::tgamma(x) calculates the gamma function, which is the factorial extended to decimal numbers. For whole numbers it matches the factorial with a shift of one: tgamma(5) is 4!, which is 24.

It also works between whole numbers. For example tgamma(0.5) is the square root of pi, about 1.77245. The name starts with "t" for "true", to tell it apart from lgamma(), which returns the logarithm of the gamma function.

std::tgamma() 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::tgamma. Start with these lines.

C++
double a = std::tgamma(5.0);     // 24 (which is 4!)
double b = std::tgamma(0.5);     // 1.77245 (square root of pi)
double c = std::tgamma(10.0);    // 362880 (which is 9!)
CallWhat it does
std::tgamma(5.0)The same as 4!. Returns 24.
std::tgamma(0.5)A half step. Returns about 1.77245, the square root of pi.
std::tgamma(0.0)A pole error. Returns infinity.

To get the factorial of n, call std::tgamma(n + 1). For big n the answer grows very fast. It overflows to inf for x above about 171.6.

Library declarations
C++
float       tgamma(float x);
double      tgamma(double x);
long double tgamma(long double x);
double      tgamma(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). Zero and negative whole numbers are not allowed.

Return Value

Return ValueDescription
Gamma valueThe gamma function of x. For a positive whole number n it is (n - 1)!.
infReturned for +0 or -0 (a pole error), and when the result is too big for the type.
NaNReturned for negative whole numbers (a domain error), and for NaN input.

C++ Examples

Example 1 — The Gamma Function and Factorials

Print tgamma(n) for several whole numbers. Each answer is (n - 1)!.

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

int main() {
    int numbers[] = {1, 2, 3, 4, 5, 6, 10};

    for (int n : numbers) {
        std::cout << "tgamma(" << n << ") = " << std::tgamma(n) << '\n';
    }
    return 0;
}

How It Works

  1. The int values are converted to double, and tgamma() is called for each one.
  2. The answers 1, 1, 2, 6, 24, 120 are the factorials 0!, 1!, 2!, 3!, 4!, 5!. The result is always one factorial behind the input.
  3. tgamma(10) is 9!, which is 362880. It still fits in six digits, so it prints in normal form.

Example 2 — Gamma at Half Steps

The gamma function also works for decimal numbers. At 0.5 it equals the square root of pi.

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

int main() {
    std::cout << "tgamma(0.5) = " << std::tgamma(0.5) << '\n';
    std::cout << "sqrt(pi)    = " << std::sqrt(std::numbers::pi) << '\n';
    std::cout << "tgamma(1.5) = " << std::tgamma(1.5) << '\n';
    std::cout << "tgamma(2.5) = " << std::tgamma(2.5) << '\n';
    std::cout << "tgamma(3.5) = " << std::tgamma(3.5) << '\n';
    return 0;
}

How It Works

  1. The first two lines compare tgamma(0.5) with sqrt(pi). Both print 1.77245, because they are the same number.
  2. The gamma function follows the rule gamma(x + 1) = x * gamma(x). So tgamma(1.5) is 0.5 * 1.77245 = 0.886227.
  3. Applying the rule again gives tgamma(2.5) = 1.5 * 0.886227 = 1.32934, and tgamma(3.5) = 2.5 * 1.32934 = 3.32335. The numbers between factorials fit smoothly in between.

Example 3 — Negative, Zero, and Very Large Inputs

See what tgamma() does at the edges: a negative fraction, a pole at zero, a negative whole number, and overflow.

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

int main() {
    std::cout << std::boolalpha;

    std::cout << "tgamma(-0.5) = " << std::tgamma(-0.5) << '\n';
    std::cout << "tgamma(0)    = " << std::tgamma(0.0) << '\n';
    std::cout << "tgamma(-1) is NaN: " << std::isnan(std::tgamma(-1.0)) << '\n';
    std::cout << "tgamma(171)  = " << std::tgamma(171.0) << '\n';
    std::cout << "tgamma(172)  = " << std::tgamma(172.0) << '\n';
    return 0;
}

How It Works

  1. Negative numbers that are not whole are allowed. tgamma(-0.5) is -3.54491, a negative answer.
  2. At 0 the function has a pole, so it returns infinity. At a negative whole number such as -1 there is no answer, so it returns NaN.
  3. tgamma(171) is 170!, about 7.25742e+306, which just fits in a double. tgamma(172) is too big, so the answer is inf.

Frequently Asked Questions

std::tgamma(x) returns the gamma function of x. For a whole number n, tgamma(n) equals (n - 1)!, so tgamma(5) is 24.
The gamma function of x. For a positive whole number n the result is (n - 1)!. Zero gives infinity, and negative whole numbers give NaN.
Use std::tgamma(n + 1). For n = 5 that is tgamma(6), which is 120. The result is a double, so for large n the answer is only approximate.
tgamma(x) returns the gamma value itself, and lgamma(x) returns its natural logarithm. lgamma() never overflows for big inputs, so it is better for large factorials.
Include <cmath> and write std::tgamma(). That always works and picks the C++ overloads. The global name ::tgamma often works too, but the standard does not promise it, and it may only take a double.

Next: trunc()

Keep going through the <cmath> functions. Cut off the decimal part of a number.

trunc() 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.

12 people found this page helpful