FUNCTION
Gamma
Returns gamma function of the argument.
You're viewing an older version of this page (#115). View the current version.
Interface
#include <codecogs/maths/special/gamma/gamma.h>
using namespace Maths::Special::Gamma;
The (complete) gamma function is defined to be an extension of the factorial for complex and real numbers. It is related to factorial by
, and in analytic everywhere except at z=0, -1, -2, -3, ... . The residual at
is
There is no points when .
\graph x=1:10
The gamma function can be define as a definite integral for
or
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