Hi - I ran into a problem when the argument to gamma function is large. Says, if I do the following:> gamma(17000)[1] Inf Warning message: value out of range in 'gammafn' Is there anyway to get around this or other implementation? Thank you. -rc
Richard M. Heiberger
2009-Oct-24 21:03 UTC
[R] Implementation of gamma function for large number
Use the lgamma function. > log(gamma(170)) [1] 701.4373 > lgamma(170) [1] 701.4373 > In typical uses of gamma(), they are multiplied or divided by other gamma() values, bringing the final result into the range of double precision numbers. Also, look at beta(), which does those divisions of gamma() for you.
Robert A LaBudde
2009-Oct-24 21:07 UTC
[R] Implementation of gamma function for large number
At 04:53 PM 10/24/2009, R_help Help wrote:>Hi - I ran into a problem when the argument to gamma function is >large. Says, if I do the following: > > > gamma(17000) >[1] Inf >Warning message: >value out of range in 'gammafn' > >Is there anyway to get around this or other implementation? Thank you. > >-rcTry the log of the gamma function instead: > ? gamma > lgamma(17000) [1] 148592.5 > (17000-0.5)*log(17000)-17000+0.5*log(2*pi) [1] 148592.5 Note that, for this size number, the first few terms of the usual expansions gives the answer to 7 significant figures. I will restrain myself from asking the obvious question of what possible use gamma(17000) could be to you, and why a simple "infinity" would not work just as well. I'm sure you must have good reason for your request. ===============================================================Robert A. LaBudde, PhD, PAS, Dpl. ACAFS e-mail: ral at lcfltd.com Least Cost Formulations, Ltd. URL: http://lcfltd.com/ 824 Timberlake Drive Tel: 757-467-0954 Virginia Beach, VA 23464-3239 Fax: 757-467-2947 "Vere scire est per causas scire"