What is the Gamma approximation you use?  Printable Version + HP Forums (https://archived.hpcalc.org/museumforum) + Forum: HP Museum Forums (https://archived.hpcalc.org/museumforum/forum1.html) + Forum: Old HP Forum Archives (https://archived.hpcalc.org/museumforum/forum2.html) + Thread: What is the Gamma approximation you use? (/thread247662.html) 
What is the Gamma approximation you use?  Namir  08022013 If you calculate the gamma function and have to use an approximation (instead of built in functions like with Excel and Matlab), what approximation would you use? What does the WP34S use for the Gamma function?
Namir
Re: What is the Gamma approximation you use?  Walter B  08022013 Quote:Just look at it  it's open source.
d;)
Re: What is the Gamma approximation you use?  Barry Mead  08022013 If you haven't looked at the source before you can grab your svn checkout svn://svn.code.sf.net/p/wp34s/code wp34s I took a look at the code. It is in wp34s/trunk/decn.c file. It seems to be using a function that computes the Natural Log of Gamma and then raises e to that power.
The function seems similar to the Spouge Gamma Approximation using
Edited: 2 Aug 2013, 7:05 p.m.
Re: What is the Gamma approximation you use?  Namir  08022013 I made a small survey for the most popular approximations for the gamma function. The Spouge method came first. You can calculate the constants somewhat easily and dynamically, unlike many other approximations. The Lanczos approximation appearing in Numerical Recipes. This method required storing an array of constants.
Namir Edited: 2 Aug 2013, 10:01 p.m. after one or more responses were posted
Re: What is the Gamma approximation you use?  Gerson W. Barbosa  08022013 Quote:
Like Lanczos, does it work in the complex domain also? I Gerson. 
Gamma:
Edited: 2 Aug 2013, 9:15 p.m.
Re: What is the Gamma approximation you use?  Paul Dale  08022013 I'm not certain which approximation we use now, I think it is Lanczos. Marcus did some work to improve the accuracy of gamma a year or two back and I don't remember if the algorithm was changed or just the constants. The history is in subversion if anyone really wants to check. The reference I used for the first implementation was Pugh's thesis on the gamma function. I originally used the table of constants on page 126 which we later found out weren't entirely correct. the algorithm used works for real and complex arguments using the same series.
Re: What is the Gamma approximation you use?  Ángel Martin  08032013
Quote: Well that sure helps the sharing and debating nature of this forum... I used a Lanczos approximation with 6 terms, for both the SandMath and 41Z implementations. It works fine (accurate to the 9th decimal digit for real arguments and the 8th for complex at worst)  with the reduced precision range in the platform but you guys are in the stratospheric accuracy range so I'm sure have used more terms or yet a better approach.
Re: What is the Gamma approximation you use?  Kimberly Thompson  08032013 Namir The algorithim employed varies w/ the artifact employed (different routines for different machines). I find the assorted routines on Viktor T. Toth's web site http://www.rskey.org/CMS/index.php/exhibithall/95 very interesting: thoughts? ps: view the page for a model to see the attendant gamma routine for that model.
SlideRule
Gamma Approxiamtion for HP71B, HP41C, and HP67  Namir  08032013 Here are the source codes for the gamma function using Spouge's approximation for the HP71B, HP41C, and HP67:
HP71B Implementation In the case of the 67 and 41C, enter the value for x and press the [A] key to get the gamma function value.
Namir Edited: 3 Aug 2013, 10:42 a.m.
Re: Gamma Approxiamtion for HP71B, HP41C, and HP67  Kimberly Thompson  08032013 THANKS! ps: I am indebted to you for ALL your marvelous postings (here & your web page)! Many thanks, again!
SlideRule
Re: Gamma Approxiamtion for HP71B, HP41C, and HP67  Gerson W. Barbosa  08032013 Here is a nonoptimal HP48G/GX version:
%%HP: T(3)A(D)F(.);As a comparison, the HP50g GAMMA function returns (.15190400267, 1980488015619E2) Very nice! P.S.: The local variable s is not necessary. The following should be slightly faster:
%%HP: T(3)A(D)F(.);
Edited: 3 Aug 2013, 1:43 p.m.
Re: Gamma Approxiamtion for HP71B, HP41C, and HP67  Namir  08032013 You are most welcome. Sharing code here is fun. You can find more code for calculators and some programming languages on my web site. Please click here.
Re: Gamma Approxiamtion for HP71B, HP41C, and HP67  Gerson W. Barbosa  08032013 Only 8 terms appear to give more accurate results on the HP71B and HP48: 10 DESTROY ALLWhen the lines 40 and 60 are changed to 40 A=12.5 @ P2=SQR(2*PI)the output is GAMMA(N) N Re: Gamma Approxiamtion for HP71B, HP41C, and HP67  Namir  08032013 Very interesting. Spouge's methods calculates the upper limit of the summation (that needs the FOR loop) as the integer(ceiling(A))1 which gives 12 for A=12.5. Using an upper limit of 8 must be causing the accuracy of the 71B to give better results. I will keep that in mind! Thanks!
Namir
Re: Gamma Approxiamtion for HP71B, HP41C, and HP67  Les Koller  08042013 I took a look at your page also. Bookmarked it in fact. I even took a couple things. Fantastic site, thanks so much.
Re: What is the Gamma approximation you use?  Marcus von Cube, Germany  08042013 Pauli is right, it's some time ago when I had a closer look at Gamma to make it fit for double precision. I used Pugh's thesis and information from Victor T. Toth's site. The list of constants can be found in the file compile_consts.c, but here you are:
// Gamma estimate constants Re: Gamma Approxiamtion for HP71B, HP41C, and HP67  Olivier De Smet  08042013 To get better results:
 do loop the other way (8 downto 1) starting with small factors to keep accuracy
10 DESTROY ALL It gives better results most of the time (rounding can be tricky)
1.00000000002 1.00000000001 1 Olivier
Edited: 4 Aug 2013, 11:28 a.m.
Re: Gamma Approxiamtion for HP71B, HP41C, and HP67  Gerson W. Barbosa  08042013 It appears the left column results are more accurate, but perhaps the sample is too small. You might want to expand it. K=8 in line 35 gives more exact answer when compared to other even K. Regards, Gerson.
10 DESTROY ALL
Re: Gamma Approxiamtion for HP71B, HP41C, and HP67  Namir  08042013 Thanks Olivier and Gerson for your input. As Voltaire once said, "The better is the enemy of the good!" I tried to do a curve fit between 1/gamma(x) and a tenth order polynomial. I also tired a Pade approximation using fifth order polynomials in the numerator and denominator. Neither attempts yielded good results.
Namir
Re: What is the Gamma approximation you use?  Namir  08042013 The Nemes approximations that Viktor Toth mentions are good ones, but not as good as the Spouge and Lanczos approximations. The Nemes approximation DO come third in my little study!
Re: What is the Gamma approximation you use?  peacecalc  08052013 Hello Namir, 20 years ago I programmed the gammafunction in "turbo pascal 6" with assembler routines coded for the 387 coprocessor. The Stirlingformula was used (for arguments > 10), for smaller arguments the recursion (gamma(x) = gamma(x+1)/x)). For negative Arguments the equation: gamma(x) = pi/(sin(pi*x)*gamma(1x)). The function was only usefull for real numbers, and it was a luck, that I didn't had to earn my money with programming....
Greetings peacecalc
Re: What is the Gamma approximation you use?  Namir  08052013 Interesting that you mentioned Turbo Pascal. I remember implementing the gamma function in Turbo Pascal in the late eighties. I used the series expansion that employs 26 constants too implement a polynomial approximation for 1/Gamma(x) for 1<=x<=2. I used recursion for arguments that were greater than 2. I made a living then by writing books about programming in Turbo Pascal, and then switched to Visual Basic and Visual C++.
Namir
