• https://me.yahoo.com
COST (GBP)
1.50
1.50
0

# Gamma

viewed 2940 times and licensed 185 times
Returns gamma function of the argument.
Controller: CodeCogs
Contents

C++

## Gamma

 doublegamma( double x int* sign = NULL )
The (complete) gamma function $\inline&space;\Gamma(z)$ is defined to be an extension of the factorial for complex and real numbers. It is related to factorial by $\inline&space;\Gamma(z)=(z-1)!$, and in analytic everywhere except at z=0, -1, -2, -3, ... . The residual at $\inline&space;z=-k$ is
$Res_{z=-k}&space;\Gamma(z)&space;=&space;\frac{(-1)^k}{k!}$
There is no points when $\inline&space;\Gamma(z)&space;=&space;0$.

There is an error with your graph parameters for gamma with options x=1:10

Error Message:Function gamma failed. Ensure that: Invalid C++

The gamma function can be define as a definite integral for $\inline&space;\Re[z]>0$
$\Gamma(z)&space;\equiv&space;\int_0^{\infty}&space;t^{z-1}&space;e^{-t}&space;dt&space;=&space;2&space;\int_0^{\infty}&space;e^{-t^2}&space;t^{2z&space;-1}&space;dt$
or
$\Gamma(z)&space;\equiv&space;\int_0^1&space;\left&space;[&space;ln&space;\left&space;(\frac{1}{t}&space;\right&space;)\right&space;]^{z-1}&space;dt$

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, Maths/Special/Gamma/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:
#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.

### Authors

Stephen L.Moshier. Copyright 1984, 1987, 1989, 1992, 2000
Updated by Will Bateman (August 2005)
##### Source Code

Source code is available when you agree to a GP Licence or buy a Commercial Licence.

Not a member, then Register with CodeCogs. Already a Member, then Login.