[R] Fortran and long integers

Peter Langfelder peter.langfelder at gmail.com
Sun Feb 6 20:11:22 CET 2011


Hi all,

I'm hoping someone more knowledgeable in Fortran than I can chime in
with opinion.

I'm the maintainer of the flashClust package that implements fast
hierarchical clustering. The fortran code fails when the number of
clustered objects is larger than about 46300. My guess is that this is
because the code uses the following construct:

IOFFSET=J+(I-1)*N-(I*(I+1))/2


where N is the number of clustered objects and I, J can vary between 1
and N. The result is used as index to access an array (namely the
distance structure).

When N is more than 46341 (or 2^16/sqrt(2)), the expressions I*(I+1)
and (I-1)*N can overflow and turn negative, messing up the indexing
and crashing the code and the entire R session with a segmentation
fault.

My solution is to turn the integers into double precision's, calculate
the index and convert back as follows:

XI = DBLE(I)
IOFFSET=J+NINT( (XI-1)*N - (XI*(XI+1))/2)

I'm wondering if there's a better way, something along the lines of
unsigned and/or long integers that are available in C?

Thanks,

Peter



More information about the R-help mailing list