Returns gamma function of the argument.

View versions (1)

Interface

#include <codecogs/maths/special/gamma/gamma.h>

using namespace Maths::Special::Gamma;

The (complete) gamma function \Gamma(z) is defined to be an extension of the factorial for complex and real numbers. It is related to factorial by \Gamma(z)=(z-1)!, and in analytic everywhere except at z=0, -1, -2, -3, ... . The residual at z=-k is

Res_{z=-k} \Gamma(z) = \frac{(-1)^k}{k!}
(1)

There is no points when \Gamma(z) = 0.

\graph x=1:10

The gamma function can be define as a definite integral for \Re[z]>0

\Gamma(z) \equiv \int_0^{\infty} t^{z-1} e^{-t} dt = 2 \int_0^{\infty} e^{-t^2} t^{2z -1} dt
(2)

or

\Gamma(z) \equiv \int_0^1 \left [ ln \left (\frac{1}{t} \right )\right ]^{z-1} dt
(3)

Arguments |x| <= 34 are reduced by recurrence and the function approximated by a rational function of degree 6/7 in the interval (2,3). Large arguments are handled by Stirling's formula, Stirling. Large negative arguments are made positive using a reflection formula.

Accuracy:

                       Relative error:
arithmetic   domain     # trials      peak         rms
  IEEE    -170,-33      20000       2.3e-15     3.3e-16
  IEEE     -33,  33     20000       9.4e-16     2.2e-16
  IEEE      33, 171.6   20000       2.3e-15     3.2e-16

Error for arguments outside the test range will be larger owing to error amplification by the exponential function.

References

  • Cephes Math Library Release 2.8: June, 2000
  • http://mathworld.wolfram.com/GammaFunction.html

Example 1

This example, also gives comparitive results from alternative solution to gamma approximation, including:

  • Gamma_Simple
  • BSD C library (standard with many C distribute).
  • An exact integer solution.
  • Gamma (solution given here).
#include <codecogs/maths/special/gamma/gamma.h>
#include <codecogs/maths/special/gamma/gamma_simple.h>
#include <stdio.h>

// For comparitive purposes, with integeral values, this is exact.
double fact(int x)
{
  double num=1;
  while(x>1) num*=x--;
  return num;
}

int main()
{
  for(double x=20; x<50; x+=5)
  printf("\n\nx=%.0lf   \ngamma_simple=%.0lf \nExact       =%.0lf  \ngamma       =%.0lf",x, Maths::Special::Gamma::gamma_simple(x), fact(int(x-1)), Maths::Special::Gamma::gamma(x));

  return 0;
}

Output: Throughout the IT++ solution is only accourate

x=20
gamma_simple=121645100410059440
Exact       =121645100408832000
gamma       =121645100408832000

x=25
gamma_simple=620448401744419210000000
Exact       =620448401733239410000000
gamma       =620448401733239410000000

x=30
gamma_simple=8841761993974087200000000000000
Exact       =8841761993739700800000000000000
gamma       =8841761993739700800000000000000

x=35
gamma_simple=295232799049930120000000000000000000000
Exact       =295232799039604080000000000000000000000
gamma       =295232799039604200000000000000000000000

x=40
gamma_simple=20397882082076071000000000000000000000000000000
Exact       =20397882081197447000000000000000000000000000000
gamma       =20397882081197442000000000000000000000000000000

x=45
gamma_simple=2658271574923091800000000000000000000000000000000000000
Exact       =2658271574788449500000000000000000000000000000000000000
gamma       =2658271574788448500000000000000000000000000000000000000

Parameters

x
argument
sign
is pointer to an external variable, into which the sign (+1 to -1) of the Gamma solution can be placed. If sign==NULL, then the sign is not returned.
GPL Licence — free for non commercial use. See Licence details.