On 14/05/2014 10:37, Adrian Du?a wrote:> Dear devels,
>
> I need to create a (short) vector in C, which contains potentially very
> large numbers, exponentially to the powers of 2.
This isn't an R question, except in so far that R mandates the usual
convention of C <int> being 32-bit. However
1) You should use an unsigned integer type.
2) Most compilers have uint64_t but C99/C11 do not require it. They
require uint_fast64_t and uintmax_t (which is the widest unsigned int)
types.
3) double will hold much larger powers, and functions like pow_di (where
supported) or pow will compute them efficiently for you. And R has
R_pow_di in Rmath.h.
>
> This is my test example:
>
> lgth = 35;
> int power[lgth];
> power[lgth - 1] = 1;
> for (j = 1; j < lgth; j++) {
> power[lgth - j - 1] = 2*power[lgth - j];
> }
>
> Everything works ok until it reaches the limit of 2^32:
>
> power: 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192,
> 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304,
> 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912,
> 1073741824, -2147483648, 0, 0, 0
>
> How should I declare the "power" vector, in order to accept
integer values
> larger then 2^32?
>
> Thanks very much in advance,
> Adrian
>
>
--
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