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!)
Call
What 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
Parameter
Description
x
Required. 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 Value
Description
Gamma value
The gamma function of x. For a positive whole number n it is (n - 1)!.
inf
Returned for +0 or -0 (a pole error), and when the result is too big for the type.
NaN
Returned 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;
}
The first two lines compare tgamma(0.5) with sqrt(pi). Both print 1.77245, because they are the same number.
The gamma function follows the rule gamma(x + 1) = x * gamma(x). So tgamma(1.5) is 0.5 * 1.77245 = 0.886227.
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.
Negative numbers that are not whole are allowed. tgamma(-0.5) is -3.54491, a negative answer.
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.
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.