C++ cmath lgamma , lgammaf and lgammal function


The C++ <cmath> lgamma,lgammaf and lgammal compute the natural logarithm of the absolute value of gamma of x.The declaration of the functions are given below.

1 float lgamma(float x);
2 double lgamma(double x);
3 long double lgamma(long double x);
4 float lgammaf(float x);
5 long double lgammal(long double x);

Parameters:
x -The value whose natural algorithm is to be computed.

Return type
floating point -The natural logarithm of the absolute value of gamma of ‘x’.

Some points to note:

i) lgamma(x) is same as loge | Γ(x) |.

ii) If x is very large range error may occur.

iii) If x is negative or 0 range error may occur.

iv) If NAN is passed ‘nan’ is returned.

v) If ±∞ is passed +∞ is returned.

Code example

/*Passing floating point value */
cout<< “lgamma(23)=” << lgamma(23) << endl ;

/*Passing very large value */
cout<< “lgamma(3e400)=” << lgamma(3e400) << endl ;

/*Passing -ve value */
cout<< “lgamma(-56)=” << lgamma(-56) << endl ;

/*Passing NAN */
cout<< “lgamma(NAN)=” << lgamma(NAN) << endl ;

/*Passing INFINITY */
cout<< “lgamma(INFINITY)=” << lgamma(INFINITY) << endl ;

Output

lgamma(23)= 48.4712
lgamma(3e400)= inf
lgamma(-56)= inf
lgamma(NAN) = nan
lgamma(INFINITY)= inf


 


lgammaf and lgammal

*Note

i)The 4th version lgammaf is same as the 1st version float lgamma(float).The last character ‘f‘ in ‘lgammaf’ stands for ‘float’ which signify the argument type and return type of the function.

ii)The 5th version lgammal is same as the 3rd version long double lgamma(long double).The last character ‘l‘ in ‘lgammal’ stands for ‘long double’ which signify the argument type and return type of the function.

Code example

#include <typeinfo>

/*lgammaf */
cout<< “*lgammaf\n”;
float f=2;
cout<< typeid( lgammaf(2) ).name() << endl /*identify type of lgammaf retunred value */
<< typeid( lgamma(f) ).name() << endl; /*identify type of lgamma(float) returned value */

/*lgammal*/
cout<< “*lgammal\n”;
long double ld=2;
cout<< typeid( lgammal(2) ).name() << endl /*indentify type of lgammal returned value */
<< typeid( lgamma(ld) ).name() << endl; /*identify type of lgamma(long double) returned value*/

Output

*lgammaf
f
f
*lgammal
e (means ‘long double’)
e (means ‘long double’)


*Side Note

Some facts about lgamma function:

  i) lgamma(1) returns +0.

  ii) lgamma(2) returns +0.

  iii) lgamma(x) returns +∞ and raises the ‘divide-by-zero’ floating-point exception for x a negative integer or zero.


Related links

C programming lgamma,lgammaf and lgammal math.h