tgamma() Function
The tgamma() function computes the gamma function, a continuous extension of the factorial function to non-integer values. This function is widely used in mathematics, statistics, and engineering to evaluate integrals and complex formulas that involve factorial-like operations.
Syntax of tgamma()
double tgamma(double x);
float tgammaf(float x);
long double tgammal(long double x);Parameters
| Parameter | Description | 
|---|---|
| x | Value at which the gamma function is evaluated. | 
Note: The gamma function returns the computed gamma value. If the magnitude of the result is too large, an overflow range error may occur; if it is too small, an underflow range error may occur. Additionally, for values where the function is asymptotic, such as zero or negative integers, a domain error or a pole error might be raised depending on the system’s error handling settings in math_errhandling.
Return Value
The function returns the gamma function of the given input.
Examples for tgamma()
Example 1: Calculating Gamma for a Positive Integer Value
This example demonstrates how to calculate the gamma function for a positive integer value, which corresponds to the factorial of the previous integer.
Program
#include <stdio.h>
#include <math.h>
int main() {
    double num = 5.0;
    double result = tgamma(num);
    printf("Gamma(%f) = %f\n", num, result);
    return 0;
}Explanation:
- Initialize a variable with the value 5.0.
- Compute the gamma function using tgamma(), which for5.0yields4!(i.e., 24.0).
- Print the result.
Program Output:
Gamma(5.000000) = 24.000000Example 2: Evaluating Gamma for a Fractional Value
This example shows how to compute the gamma function for a fractional value.
Program
#include <stdio.h>
#include <math.h>
int main() {
    double num = 2.5;
    double result = tgamma(num);
    printf("Gamma(%f) = %f\n", num, result);
    return 0;
}Explanation:
- Assign a fractional value (2.5) to the variable.
- Calculate the gamma function of the value using tgamma().
- Print the computed result.
Program Output:
Gamma(2.500000) = 1.329340Example 3: Handling Domain Error with a Negative Integer
This example illustrates the behavior of tgamma() when a negative integer is passed, which is a pole of the gamma function.
Program
#include <stdio.h>
#include <math.h>
#include <errno.h>
int main() {
    double num = -3.0;
    errno = 0; // Reset errno before call
    double result = tgamma(num);
    if (errno == ERANGE) {
        printf("Domain error occurred for Gamma(%f)\n", num);
    } else {
        printf("Gamma(%f) = %f\n", num, result);
    }
    return 0;
}Explanation:
- Set a negative integer (-3.0) which is a pole for the gamma function.
- Reset errnoto detect any errors.
- Compute the gamma function using tgamma(), triggering a domain error.
- Check errnoand display an error message if a domain error occurs.
Program Output:
Gamma(-3.000000) = nanExample 4: Evaluating Gamma for a Large Value to Trigger Overflow
This example demonstrates how a large input value may lead to a range error due to overflow when using tgamma().
Program
#include <stdio.h>
#include <math.h>
#include <errno.h>
int main() {
    double num = 1071.0; // Large input likely to cause overflow
    errno = 0; // Reset errno before call
    double result = tgamma(num);
    if (errno == ERANGE) {
        printf("Range error occurred for Gamma(%f)\n", num);
    } else {
        printf("Gamma(%f) = %f\n", num, result);
    }
    return 0;
}Explanation:
- Assign a large value (1071.0) which may lead to an overflow error.
- Reset errnobefore the function call.
- Invoke tgamma()and check if a range error occurs by examiningerrno.
- If an error occurs, print an error message; otherwise, print the computed gamma value.
Program Output:
Range error occurred for Gamma(1071.000000)