C lgamma() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math.h

Definition

In C, lgamma() returns the natural logarithm of the absolute value of the gamma function. In short form, it computes log(|Γ(x)|) for a double argument.

Gamma grows very fast, so the log form is safer for large inputs. The gamma value itself can be negative for some non-integer negatives, so libraries also expose a simple sign. Many platforms set the global signgam to +1 or -1 for that sign.

Pass one double. Include <math.h>. For beginners, start with positive arguments such as integers, where the sign is positive and the result is easy to check.

Syntax

Pass the input to the log-gamma function. Start with this call.

C
lgamma(x);
CallWhat it does
lgamma(1.0)Returns 0 because Γ(1) is 1 and log(1) is 0.
lgamma(3.0)Returns about 0.693 because Γ(3) is 2 and log(2) is ln 2.
lgamma(0.5)Returns a positive log value for Γ(0.5).

Include <math.h>. On GCC, link with -lm. Related helpers are lgammaf, lgammal, and the plain gamma function tgamma.

Library declarations
C
double lgamma(double x);
float lgammaf(float x);
long double lgammal(long double x);
/* many platforms also expose: extern int signgam; */

Parameters

ParameterDescription
xRequired. The double input to the log-gamma calculation.

Return Value

Return ValueDescription
doubleThe value of log(|Γ(x)|). On error, a library-defined error value may be returned.

C Examples

Example 1 — Basic Log-Gamma Values

Compute lgamma for small positive integers and print the results.

C
#include <stdio.h>
#include <math.h>

int main(void) {
    printf("%.6f\n", lgamma(1.0));
    printf("%.6f\n", lgamma(2.0));
    printf("%.6f\n", lgamma(3.0));
    return 0;
}

How It Works

  1. Include math.h. That declares lgamma.
  2. Γ(1) and Γ(2). Both equal 1, so log(1) prints 0.
  3. Γ(3). Γ(3) is 2, so the third line is about 0.693147.
  4. Positive inputs. These beginner values keep the gamma sign positive.

Example 2 — Simple Sign Handling

Read signgam after lgamma to learn whether Γ(x) was positive or negative.

C
#include <stdio.h>
#include <math.h>

int main(void) {
    double y = lgamma(2.5);

    printf("logabs=%.6f\n", y);
    printf("signgam=%d\n", signgam);
    return 0;
}

How It Works

  1. Call lgamma. The function returns log(|Γ(x)|).
  2. Print the log value. You see the magnitude in log space.
  3. Read signgam. On common platforms this global holds +1 or -1.
  4. Beginner tip. For positive x in this range, the sign is usually +1.

Example 3 — Compare with tgamma

Check that exp(lgamma(x)) is close to fabs(tgamma(x)).

C
#include <stdio.h>
#include <math.h>

int main(void) {
    double x = 4.0;
    double from_log = exp(lgamma(x));
    double from_gamma = fabs(tgamma(x));

    printf("%.6f\n", from_log);
    printf("%.6f\n", from_gamma);
    return 0;
}

How It Works

  1. Pick x = 4. Γ(4) is 3! = 6.
  2. Use lgamma path. exp(lgamma(x)) rebuilds the absolute gamma value.
  3. Use tgamma path. fabs(tgamma(x)) should match.
  4. See both print 6. That confirms the log and plain forms agree here.

Frequently Asked Questions

lgamma() returns the natural log of the absolute value of the gamma function, log(|Γ(x)|).
x is required. It is the double input to the log-gamma calculation.
A double equal to log(|Γ(x)|). Use signgam on many platforms if you also need the sign of Γ(x).
Include <math.h>. On GCC, link with -lm.
lgamma itself returns a log of an absolute value. Many platforms set signgam to +1 or -1 for the sign of Γ(x).

Next: llrint()

Round a floating value to a long long using the current rounding mode.

llrint() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

9 people found this page helpful