C++ Standard Library C++ STL Library

C++ <cmath> - tgamma() Function



The C++ <cmath> tgamma() function returns gamma function of the argument. The gamma function of x is defined as:

tgamma
Gamma Function

Syntax

double tgamma (double x);
float tgamma (float x);
long double tgamma (long double x);
double tgamma (T x);               

Parameters

x Specify the value.

Return Value

  • If no errors occur, returns gamma function of the argument.
  • If a domain error occurs, an implementation-defined value (NaN where supported) is returned.
  • If a pole error occurs, ±HUGE_VAL, ±HUGE_VALF, or ±HUGE_VALL is returned.
  • If a range error due to overflow occurs, ±HUGE_VAL, ±HUGE_VALF, or ±HUGE_VALL is returned.
  • If a range error due to underflow occurs, the correct value (after rounding) is returned.

Example:

The example below shows the usage of tgamma() function.

#include <iostream>
#include <cmath>
using namespace std;
 
int main (){
  cout<<"tgamma(0.1): "<<tgamma(0.1)<<"\n";
  cout<<"tgamma(1.5): "<<tgamma(1.5)<<"\n";
  cout<<"tgamma(2.5): "<<tgamma(2.5)<<"\n";
  cout<<"tgamma(-1.5): "<<tgamma(-1.5)<<"\n";
  cout<<"tgamma(-2.5): "<<tgamma(-2.5)<<"\n";
  return 0;
}

The output of the above code will be:

tgamma(0.1): 9.51351
tgamma(1.5): 0.886227
tgamma(2.5): 1.32934
tgamma(-1.5): 2.36327
tgamma(-2.5): -0.945309

❮ C++ <cmath> Library