Message-ID: <v9uuo0dn06oka37i25ti4ndu0qdh0ckhsh@4ax.com>
Date: 2004-11-08T13:55:57Z
From: Duncan Murdoch
Subject: About 'choose' function
In-Reply-To: <20041108115804.49535.qmail@web26304.mail.ukl.yahoo.com>
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