Home C Math Functions tgamma() C tgamma() Function Definition In C, tgamma() computes the gamma function Γ(x). For positive integers, Γ(n) = (n - 1)!. For example, tgamma(5) is 24 because 4! is 24.
You pass one double. The result is a double. Include <math.h>. The name starts with t for “true gamma,” to tell it apart from lgamma, which returns the log of the absolute gamma value.
Gamma grows very quickly. Large inputs can overflow. Non-positive integers are poles where gamma is undefined; those calls are domain errors.
Syntax Pass the floating-point argument. Start with this call.
Call What it does tgamma(5.0);Returns 24, which is 4!.
Include <math.h>. On GCC, link with -lm. Avoid non-positive integers.
Library declarations double tgamma(double x);
float tgammaf(float x);
long double tgammal(long double x); Parameters Parameter Description x Required. The double argument to the gamma function.
Return Value Return Value Description Gamma(x) The value of Γ(x) as a double. Domain / range errors Non-positive integers are invalid; large |x| may overflow.
Important
Do not call tgamma with 0, -1, -2, … Those points are poles. For huge magnitudes, prefer working with lgamma if you only need log-scale values.
C Examples 1. Basic tgamma 2. Half argument 3. Recurrence check Example 1 — Basic Factorial Connection Show that gamma at positive integers matches factorials.
#include <stdio.h>
#include <math.h>
int main(void) {
printf("tgamma(5) = %.1f\n", tgamma(5.0));
printf("tgamma(6) = %.1f\n", tgamma(6.0));
return 0;
} tgamma(5) = 24.0
tgamma(6) = 120.0 How It Works Include math.h. That declares tgamma.Gamma(5). Equals 4! = 24.Gamma(6). Equals 5! = 120.Mental map. Shift by one from ordinary factorial.Example 2 — Non-Integer Argument Gamma also works for many non-integers. A classic value is Γ(0.5) = √π.
#include <stdio.h>
#include <math.h>
int main(void) {
double g = tgamma(0.5);
double s = sqrt(3.141592653589793);
printf("tgamma(0.5) = %.10f\n", g);
printf("sqrt(pi) = %.10f\n", s);
return 0;
} tgamma(0.5) = 1.7724538509
sqrt(pi) = 1.7724538509 How It Works Call tgamma(0.5). A famous closed form.Build sqrt(pi). Use a portable pi constant.Compare prints. The digits should match closely.Beyond integers. Gamma extends factorial ideas to reals.Example 3 — Check Gamma(x+1) = x * Gamma(x) Verify the classic recurrence on a small non-integer.
#include <stdio.h>
#include <math.h>
int main(void) {
double x = 3.5;
double a = tgamma(x + 1.0);
double b = x * tgamma(x);
printf("Gamma(x+1) = %.10f\n", a);
printf("x*Gamma(x) = %.10f\n", b);
return 0;
} Gamma(x+1) = 11.6317283966
x*Gamma(x) = 11.6317283966 How It Works Pick x. 3.5 avoids integer poles.Compute both sides. Direct gamma and the recurrence form.Compare. Floating values should agree closely.Useful identity. This relation is how gamma generalizes factorials.Frequently Asked Questions What does tgamma() do in C? tgamma(x) returns the gamma function Gamma(x). For positive integers, Gamma(n) equals (n - 1)!.
What is the parameter of tgamma()? x is required. It is the double argument passed to gamma.
What does tgamma() return? A double equal to Gamma(x), within floating-point limits.
Which header file does tgamma() need? Include <math.h>. On GCC, link with -lm.
How is tgamma() different from lgamma()? tgamma returns Gamma(x). lgamma returns the natural log of |Gamma(x)|, which is safer for very large magnitudes.
CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful
Helpful Share Copy link Suggestion