[R] Strange integer result on Debian/amd64

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Mar 21 10:29:28 CET 2007


On Wed, 21 Mar 2007, Dave Ewart wrote:

> On Wednesday, 21.03.2007 at 14:07 +0800, Berwin A Turlach wrote:
>
>>> All help/suggestions/appreciated!
>>
>> The calculations are done in floating point arithmetic, not integer
>> arithmetic. From the help page on `choose' one might already guess
>> so much, but reading "R-2.4.1/src/nmath/choose.c" definitely confirms
>> this fact.
>>
>> Thus, your problem is covered by FAQ 7.31:
>>
>> http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f
>
> Thanks Berwin.
>
> OK, I understand the problem now.  I was wondering if it something along
> those lines, but initially dismissed that as a possibility since I
> expected 'choose' to be an integer calculation.

It is not, because it would be far too easy to get integer overflow.

> However, the underlying problem that gave rise to the difficulty was as
> follows.  A colleague wishes to create a matrix, where one of the
> dimensions of the matrix is the result of the 'choose' function, i.e.
>
>  mycols<-choose(11,6)
>  a_matrix<-matrix(0,nrow=11,ncol=mycols)
>
> Clearly, 'ncol' casts mycols as as integer.  In this case, a_matrix has
> only 461 columns, not 462.
>
> What's the best way to make this work as required?

Use round(mycols).  I think we ought to do this internally: we do when 
using computations via beta/gamma functions.

-- 
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