tgamma() method in C/C++ with Examples
The tgamma() function is defined in header math.h header in C and cmath library in C++. This function is used to compute the gamma function of an argument passed to the function.
Syntax of tgamma()
float tgamma(float x);
double tgamma(double x);
long double tgamma(long double x);
Parameters
x: The value whose gamma function is to be computed. It can be float, double or long double.
Return Value
- This function returns the gamma function value of x.
- For x = 0: +inf/-inf
- For x = -inf: NAN
- For x = +inf: +inf
- For x = -ve: NAN
- For x = NAN: NAN
Errors in tgamma() Function
There are two types of errors that usually occur with tgamma() method:
- Range errors
- Overflow range error: This occurs when the magnitude of the parameter x is very large.
- Underflow range error: This occurs when the magnitude of the parameter x is very small.
- Domain/Pole errors
If x is zero or a negative integer for which the function is asymptotic, it may cause a domain error or a pole error (or none, depending on implementation).
Example of tgamma Function
Below example demonstrate the use of tgamma() function in C/C++:
C
#include <math.h>
#include <stdio.h>
int main()
{
float x = 0.0;
printf ( "For x = %f, tgamma(x) = %f\n" , x, tgamma(x));
x = -18.0 / 0.0;
printf ( "For x = %f, tgamma(x) = %f\n" , x, tgamma(x));
x = 10.0 / 0.0;
printf ( "For x = %f, tgamma(x) = %f\n" , x, tgamma(x));
x = 0.0 / 0.0;
printf ( "For x = %f, tgamma(x) = %f" , x, tgamma(x));
return 0;
}
|
C++
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
float x = 0.0;
cout << "For x = " << x << ", tgamma(x) = " << tgamma(x)
<< endl;
x = -18.0 / 0.0;
cout << "For x = " << x << ", tgamma(x) = " << tgamma(x)
<< endl;
x = 10.0 / 0.0;
cout << "For x = " << x << ", tgamma(x) = " << tgamma(x)
<< endl;
x = 0.0 / 0.0;
cout << "For x = " << x
<< ", tgamma(x) = " << tgamma(x);
return 0;
}
|
Output
For x = 0.000000, tgamma(x) = inf
For x = -inf, tgamma(x) = nan
For x = inf, tgamma(x) = inf
For x = -nan, tgamma(x) = -nan
Last Updated :
01 Jun, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...