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()

</>
Copy
double tgamma(double x);
float tgammaf(float x);
long double tgammal(long double x);

Parameters

ParameterDescription
xValue 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

</>
Copy
#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:

  1. Initialize a variable with the value 5.0.
  2. Compute the gamma function using tgamma(), which for 5.0 yields 4! (i.e., 24.0).
  3. Print the result.

Program Output:

Gamma(5.000000) = 24.000000

Example 2: Evaluating Gamma for a Fractional Value

This example shows how to compute the gamma function for a fractional value.

Program

</>
Copy
#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:

  1. Assign a fractional value (2.5) to the variable.
  2. Calculate the gamma function of the value using tgamma().
  3. Print the computed result.

Program Output:

Gamma(2.500000) = 1.329340

Example 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

</>
Copy
#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:

  1. Set a negative integer (-3.0) which is a pole for the gamma function.
  2. Reset errno to detect any errors.
  3. Compute the gamma function using tgamma(), triggering a domain error.
  4. Check errno and display an error message if a domain error occurs.

Program Output:

Gamma(-3.000000) = nan

Example 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

</>
Copy
#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:

  1. Assign a large value (1071.0) which may lead to an overflow error.
  2. Reset errno before the function call.
  3. Invoke tgamma() and check if a range error occurs by examining errno.
  4. If an error occurs, print an error message; otherwise, print the computed gamma value.

Program Output:

Range error occurred for Gamma(1071.000000)