[R] About 'choose' function
Duncan Murdoch
murdoch at stats.uwo.ca
Mon Nov 8 14:55:57 CET 2004
On Mon, 8 Nov 2004 11:58:04 +0000 (GMT), John <cyracules at yahoo.co.uk>
wrote :
>What is the difference between 'choose', 'my.choose1',
>and 'my.choose2' below? That is, what is behind
>'choose' function and what's the problem using 'prod'
>or 'gamma' function?
>
>Thanks a lot.
>
>John
>
>##########
>
>> choose(6000,20)
>[1] 1.455904e+57
>>
>> my.choose1 <- function(x,y) {
>prod(1:x)/(prod(1:y)*prod(1:(x-y))) }
>> my.choose1(6000,20)
>[1] NaN
Generally 1:x will be taken to be of mode integer, so I would have
expected prod(1:x) to overflow around x==13, but whoever programmed it
was smart, and switched the values to floating point. Floating point
evaluation of prod(1:x) overflows around x==171. The ratio of two
overflows is undefined, so you get a NaN result.
>>
>> my.choose2 <- function(x,y) {
>gamma(x+1)/(gamma(y+1)*gamma(x-y+1)) }
>> my.choose2(6000,20)
>[1] NaN
Same problem.
Duncan Murdoch
More information about the R-help
mailing list