C programming lgamma , lgammaf and lgammal math.h


In C programming the <math.h> 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’.

All the functions are the same,they compute the natural logarithm of the absolute value of gamma of x.The 1st,2nd and the 3rd function have the same name i.e.’lgamma’.The 4th and 5th function have different names.What does having a different names mean is explained in detail below.

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.

Link :C programming log,logf and logl

Code example

double d=1.234 , x1=2e200 ;

float f=1.234 ;

long double ld=1.234 ;

printf(“%lf”, lgamma( d ) ) ; //calls the 2nd version
printf(“\n%f”, lgamma( f ) ) ; //calls the 1st version
printf(“\n%Lf”, lgamma( ld ) ) ;//calls the 3rd version

printf(“\n%lf”, lgamma( x1 ) ) ;
printf(“\n%lf”, lgamma( INFINITY ) ) ;

Output in Visual Studio ,

-0.094478
-0.094478
-0.094478
92042033155873819419374369724279933720134528783717883499118901924219854452111138965978847330509552934593833866790892859743646300740012192508637116437543123181867111246564088055454933073978293432350670848.000000
inf

Since x1 is very large we also get the 4th output as very large value,for the 5th output INFINITY is passed as argument so ‘inf'(infinity) is returned.


 


lgammaf

The ‘lgammaf‘ function is same as the 1st function.This means the parameter and return type of the function is float type.The ‘f‘ character appended to ‘lgammaf‘ stands for float and it signify the parameter type and return type of the function.

Code example

float x=50.01 ;

printf(“%f”, lgamma( x ) ); //Calls the 1st version
printf(“\n%f”, lgammaf( 50.01) ); Calls the 4th version

Output in Visual Studio,

144.604752
144.604752


lgammal

The ‘lgammal‘ function is same as the 1st function.This means the parameter and return type of the function is long double type.The ‘l‘ character appended to ‘lgammal‘ stands for long double and it signify the parameter type and return type of the function.

Code example

long double x=50.01;

printf(“%Lf”, lgamma( x ) ); //Calls the 3rd version
printf(“\n%Lf”, lgammal( 50.01 ) ); Calls the 5th version

Output,

144.604765
144.604765


*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 tgamma,tgammaf and tgammal math.h