[R] Handling very large integers with factorial and combinat (nCm)

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Apr 4 11:13:34 CEST 2005


On Mon, 4 Apr 2005, Marco Chiarandini wrote:

> Dear list,
>
> perhpas this question is more suitable for R-dev but since I am not
> really a developer I post it here first.
>
> Apparently the following lines do not create any problem in R:
>
> library(combinat)
> r <- 20; b <- 2;
> sum( sapply(0:r,function(x) nCm(r,x)^(2*b)) ) > 2^64
>
> while in C I obtain an overflow of data even using unsigned long long
> and with long double I incurr in precision problems.
>
> Where can I find information about how R (or the combinat package)
> handles very large integer numbers?

In this case, as doubles.  R numeric variables are doubles, and 'r' and 
'b' are numeric, not integer.  However,

> r <- as.integer(20); b <- as.integer(2)
> sum( sapply(0:r,function(x) nCm(r,x)^(2*b)) )

gives the same result (and internally nCm computes in doubles: Note that 
factorials are computed via lgamma).

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list