include "NSLog.incl" #define FB_M_E 2.7182818284590452 double local fn StirlingGamma( x as double ) return fn sqrt(2 * M_PI / x ) * fn pow(x / FB_M_E, x ) end fn = 0.0 double local fn LanczosGamma( x as double ) int g = 7 static double p(8) = {0.99999999999980993, 676.5203681218851, -1259.1392167224028,¬ 771.32342877765313, -176.61502916214059, 12.507343278686905, -0.13857109526572012,¬ 9.9843695780195716e-6, 1.5056327351493116e-7} if ( x < 0.5 ) then return M_PI / ( sin( M_PI * x ) * fn LanczosGamma( 1 - x ) ) x -= 1.0 double a = p(0) for int i = 1 to 8 a += p(i) / ( x + i ) next double t = x + g + 0.5 return fn sqrt(2 * M_PI ) * fn pow(t, x + 0.5 ) * exp(-t ) * a end fn = 0.0 void local fn RunGammaFunctions NSLog( @"Gamma\tStirling\t\t\t Lanczos" ) for double i = 1.0 to 20 double value = i / 10.0 NSLog( @"%1.2f\t%17.15f\t %1.15f", value, fn StirlingGamma( value ), fn LanczosGamma( value ) ) next end fn fn RunGammaFunctions HandleEvents