C++ Math lgamma()

Last Updated : 18 May 2026

Math lgamma() Function

In C++, the lgamma() function is defined in the <cmath> or <math.h> header file and is used to calculate the natural logarithm of the absolute value of the gamma function of a given number.

Mathematically, the lgamma() function is represented as:

The lgamma() function supports float, double, long double, and integer values. It is commonly used where very large gamma values are involved.

C++ Math lgamma() function

Syntax

It has the following syntax.

Parameters

x: It is a floating point value.

Return value

It returns the logarithm of a gamma function of value x.

ParameterReturn value
x= 1 or x=20
x= ±0+∞
x= -ve integer or ±∞+∞
x= nannan

Examples of lgamma() Function

Here, we are going to discuss several examples to demonstrate the lgamma() Function.

Example 1: Calculate lgamma() for the Value 2

This example demonstrates how the lgamma() function behaves when the input value is 2.

Output:

Value of x is : 2
lgamma(x) :0

Explanation:

In the above example, the value of x is 2. Therefore, the function lgamma() returns 0 value.

Example 2: Calculate lgamma() for Zero

This example demonstrates how the lgamma() function behaves when the input value is zero.

Output:

Value of x is : 0
lgamma(x) : inf

Explanation:

In the above example, the value of x is zero. Therefore, the function lgamma() returns +∞.

Example 3: Calculate lgamma() for a Negative Integer

This example demonstrates how the lgamma() function behaves when the input value is a negative integer.

Output:

Value of x is : -5
lgamma(x) : inf

Explanation:

In the above example, the value of x is a negative integer. Therefore, the function lgamma() returns +∞.

Example 4: Calculate lgamma() for NaN

This example demonstrates how the lgamma() function behaves when the input value is NaN (Not a Number).

Output:

Value of x is : -nan
lgamma(x) :-nan

Explanation:

In the above example, the value of x is nan. Therefore, the function lgamma() returns nan.