[R] About 'choose' function

Peter Dalgaard p.dalgaard at biostat.ku.dk
Tue Nov 9 00:16:04 CET 2004


(Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> writes:

> On 08-Nov-04 Duncan Murdoch wrote:
> > Are you sure you're using the standard R choose()?  I get different
> > results from you:
> > 
> >> print(choose(60000000000,1),digits=20)
> > [1] 6e+10
...
> Well, that's good to learn! As usual, I'm out of date, but this
> time it seems to matter:
> 
>   R-1.8.0 on Linux
> 
> (Yes, it was the standard R choose() from that version).
> 
> Perhaps I'd better roll up my sleeves and finally upgrade (though
> I'm really waiting for the next release since the move to 2.0 seems
> to have needed a few things ironed out).

And just to rub it in:

> choose(60000000000,31)
[1] 1.613121e+300

in 2.0.0, whereas the straightforward calculation

> exp(lgamma(60000000000+1)-lgamma(60000000000-31+1)-lgamma(31+1))
[1] 1.613161e+300

is off in the 6th significant digit, due to cancellation between

> lgamma(60000000000+1)
[1] 1.429057e+12

and

> lgamma(60000000000-31+1)
[1] 1.429057e+12

(the true difference between the two is supposed to be 
> lchoose(60000000000,31)+lgamma(31+1)
[1] 769.3459
)

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list