Skip to content
Back to formatted view

Raw Message

Message-ID: <Pine.LNX.4.61.0504041008310.8335@gannet.stats>
Date: 2005-04-04T09:13:34Z
From: Brian Ripley
Subject: Handling very large integers with factorial and combinat (nCm)
In-Reply-To: <Pine.LNX.4.58.0504031829120.15936@kika.intellektik.informatik.tu-darmstadt.de>

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