C programming tgamma , tgammaf and tgammal math.h


In C programming the <math.h> tgamma,tgammaf and tgammal compute the gamma function of the argument.The declaration of the functions are given below.

1 float tgamma(float x);
2 double tgamma(double x);
3 long double tgamma(long double x);
4 float tgammaf(float x);
5 long double tgammal(long double x);

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

Return type
floating point -The gamma function of x.

All the functions are the same,they compute the gamma function of x.The 1st,2nd and the 3rd function have the same name i.e.’tgamma’.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) tgamma(x) is same as Γ(x).

ii) A domain or range error may occur if x is -ve integer or 0.

iii) A range error may occur if x is very large or very small.

iv) If x is NAN, ‘nan’ is returned.

v) If +∞ is passed +∞ is returned, if -∞ is returned ‘nan’ is returned.

vi) If integer is passed the second version is called-the double version.

Code example

double d=1.234 , x1=2e200 ;

float f=1.234 ;

long double ld=1.234 ;

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

printf(“\n%lf”, tgamma( x1 ) ) ;
printf(“\n%lf”, tgamma( NAN ) ) ;

Output in Visual Studio ,

0.909847
0.909847
0.909847
inf
-nan(ind)

Since the x1 argument value is very large the returned value is ‘inf'(infinity) and on passing “NAN”(a NaN value) we get ‘-nan(ind)'(nan).






tgammaf

The ‘tgammaf‘ 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 ‘tgammaf‘ stands for float and it signify the parameter type and return type of the function.

Code example

float x=50.01 ;

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

Output in Visual Studio,

inf
inf

The returned type is float type but it could not represent the value returned by the function so the compiler will compute the returned value as infinity,hence we got ‘inf’ as the output.


tgammal

The ‘tgammal‘ 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 ‘tgammal‘ 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”, tgamma( x ) ); //Calls the 3rd version
printf(“\n%Lf”, tgammal( 50.01 ) ); Calls the 5th version

Output,

632486751055887344584972401348882917281491396750029511087620096.000000
632486751055887344584972401348882917281491396750029511087620096.000000

You can see here that the output is very large but long double can represent it so we got the value as the output and not an infinity.In the previous example where ‘tgammaf’ was called the returned type is float type but it could not represent the returned value so the compiler had to returned ‘inf’ instead.


*Side Note

Some facts about tgamma:

  i) tgamma(±0) returns ±∞ and raises the ‘divide-by-zero’ floating-point exception.

  ii) tgamma(x) returns a NAN and raises the ‘invalid’ floating-point exception for x a negative integer.

  iii) tgamma(−∞) returns a NAN and raises the ‘invalid’ floating-point exception.