From simon.urbanek at r-project.org  Wed May 14 16:35:24 2014
From: simon.urbanek at r-project.org (Simon Urbanek)
Date: Wed, 14 May 2014 10:35:24 -0400
Subject: [Rd] large integer values
In-Reply-To: <CAJ=0CtA2yt28cT-j5mP5M0a3_s_XZ8f3iJ4kQPm6NzmZS7a6iw@mail.gmail.com>
References: <CAJ=0CtBbsqpx54p---oGxziiSLum4_k449z+hjtSjuQDdSk0Mw@mail.gmail.com>
	<5373575E.4030906@stats.ox.ac.uk>
	<CAJ=0CtA2yt28cT-j5mP5M0a3_s_XZ8f3iJ4kQPm6NzmZS7a6iw@mail.gmail.com>
Message-ID: <A4FBBBFD-7E66-444A-BEF0-7F2B40613D5F@r-project.org>

On May 14, 2014, at 8:41 AM, Adrian Du?a <dusa.adrian at unibuc.ro> wrote:

> Dear Prof. Ripley,
> 
> Once again, thank you for your replies.
> I must confess not being a genuine C programmer, having learned how to use
> C only in connection to R (and the macros provided are almost a separate
> language to learn).
> 
> I'll try to read more about the types you've indicated, and will keep
> trying. So far, most certainly I am not doing it right, because all of them
> have the same result. Tried declaring:
> 
> uint64_t power[lgth];
> and
> uint_fast64_t power[lgth];
> and
> uintmax_t power[lgth];
> 
> but still the top threshold appears at the limit of 32-bit in all cases.
> 

How do you print them? It seems like you're printing 32-bit value instead ... (powers of 2 are simply shifts of 1). 

Cheers,
S


> Will keep reading about these...
> Best wishes,
> Adrian
> 
> 
> 
> On Wed, May 14, 2014 at 2:45 PM, Prof Brian Ripley <ripley at stats.ox.ac.uk>wrote:
> 
>> 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
>> 
> 
> 
> 
> -- 
> Adrian Dusa
> University of Bucharest
> Romanian Social Data Archive
> 1, Schitu Magureanu Bd.
> 050025 Bucharest sector 5
> Romania
> Tel.:+40 21 3126618 \
>        +40 21 3120210 / int.101
> Fax: +40 21 3158391
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



[Rd] large integer values
Simon Urbanek