C++ cmath tgamma , tgammaf and tgammal function


The C++ <cmath> 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.

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

/*Passing floating point value*/
cout<< “tgamma(45)=” << tgamma(45) << endl;

/*Passing -ve value*/
cout<< “tgamma(-5)=” << tgamma(-5) << endl;

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

/*Passing very large +ve and -ve value*/
cout<< “tgamma(34e350)=” << tgamma(34e350) << endl ;
cout<< “tgamma(-34e350)=” << tgamma(-34e350) << endl ;

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

Output

tgamma(45)= 2.65827e+054
tgamma(-5)= inf
tgamma(NAN)= nan
tgamma(34e350)= inf
tgamma(-34e350)= nan
tgamma(INFINITY)= inf
tgamma(-INFINITY)= nan

In passing very large value which is an overflow case,the compiler treat the value as +∞ and also in passing a very small -ve value which is again an overflow case the compiler treat it as -∞.So we can say that passing a very large +ve and -ve value is same as passing ±∞,this is also supported by the fact that passing very large value yield the same result as passing +∞,and -∞ yield the same result as passing very small negative value;refer to the output given above.


 


tgammaf and tgammal

*Note

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

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

Code example

#include <typeinfo>

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

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

Output

*tgammaf
f
f
*tgammal
e (means ‘long double’)
e (means ‘long double’)


*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.


Related links

C programming tgamma,tgammaf and tgammal math.h