A Sterling series approximation of the gamma function.

You're viewing an older version of this page (#114). View the current version.

View versions (1)

Interface

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

using namespace Maths::Special::Gamma;

Returns a simple approximation to the gamma functions. If your only interested in low levels of accuracy (10 significant figures), then this solution is evaluated quickly and is relatively stable.

This approximation of the gamma function is achieved using the exponential of the Stirling series,

\Gamma(z) = e^{\ln |\Gamma(z)|}
(1)

where

ln \Gamma(z) = \frac{1}{2} \ln(2 \pi) + (z - \frac{1}{2}) \ln z - z + \frac{1}{12z} - \frac{1}{360 z^3} + \frac{1}{1260 z^5} - ...
(2)

See also Gamma and Stirling

Example 1

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

int main()
{
  for(double x=3; x<7; x+=0.4)
  printf("\n x=%lf gamma_simple=%lf",x, Maths::Special::Gamma::gamma_simple(x));
  return 0;
}

Output:

x=3.000000 gamma_simple=2.000000
x=3.400000 gamma_simple=2.981206
x=3.800000 gamma_simple=4.694174
x=4.200000 gamma_simple=7.756690
x=4.600000 gamma_simple=13.381286
x=5.000000 gamma_simple=24.000000
x=5.400000 gamma_simple=44.598848
x=5.800000 gamma_simple=85.621738
x=6.200000 gamma_simple=169.406099
x=6.600000 gamma_simple=344.701924

Parameters

x
argument
GPL Licence — free for non commercial use. See Licence details.

Interactive Calculator

x
Result