C++ Standard Library C++ STL Library

C++ <cmath> - lgamma() Function



The C++ <cmath> lgamma() function returns the natural logarithm of the absolute value of gamma function (log gamma function) of the argument. The log gamma function of x is defined as:

lgamma
Log-gamma Function

Syntax

double lgamma (double x);
float lgamma (float x);
long double lgamma (long double x);
double lgamma (T x);             

Parameters

x Specify the value.

Return Value

  • If no errors occur, returns the natural logarithm of the absolute value of gamma function of the argument.
  • If a pole error occurs, ±HUGE_VAL, ±HUGE_VALF, or ±HUGE_VALL is returned.
  • If a range error due to overflow occurs, ±HUGE_VAL, ±HUGE_VALF, or ±HUGE_VALL is returned.

Example:

The example below shows the usage of lgamma() function.

#include <iostream>
#include <cmath>
using namespace std;
 
int main (){
  cout<<"lgamma(0.1): "<<lgamma(0.1)<<"\n";
  cout<<"lgamma(1.5): "<<lgamma(1.5)<<"\n";
  cout<<"lgamma(2.5): "<<lgamma(2.5)<<"\n";
  cout<<"lgamma(-1.5): "<<lgamma(-1.5)<<"\n";
  cout<<"lgamma(-2.5): "<<lgamma(-2.5)<<"\n";
  return 0;
}

The output of the above code will be:

lgamma(0.1): 2.25271
lgamma(1.5): -0.120782
lgamma(2.5): 0.284683
lgamma(-1.5): 0.860047
lgamma(-2.5): -0.0562437

❮ C++ <cmath> Library