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.

It has the following syntax.
x: It is a floating point value.
It returns the logarithm of a gamma function of value x.
| Parameter | Return value |
|---|---|
| x= 1 or x=2 | 0 |
| x= ±0 | +∞ |
| x= -ve integer or ±∞ | +∞ |
| x= nan | nan |
Here, we are going to discuss several examples to demonstrate the lgamma() Function.
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.
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 +∞.
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 +∞.
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.
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India