# large integer values

 Classic List Threaded
7 messages
Reply | Threaded
Open this post in threaded view
|

## large integer values

 Dear devels, I need to create a (short) vector in C, which contains potentially very large numbers, exponentially to the powers of 2. 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 -- 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]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

## Re: large integer values

 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 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,                  [hidden email] 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 ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

## Re: large integer values

 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. Will keep reading about these... Best wishes, Adrian On Wed, May 14, 2014 at 2:45 PM, Prof Brian Ripley <[hidden email]>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 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,                  [hidden email] > 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]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

## Re: large integer values

 On May 14, 2014, at 8:41 AM, Adrian Dușa <[hidden email]> 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 <[hidden email]>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 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,                  [hidden email] >> 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]] > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

## Re: large integer values

 On Wed, May 14, 2014 at 5:35 PM, Simon Urbanek <[hidden email]>wrote: > [...] > > How do you print them? It seems like you're printing 32-bit value instead > ... (powers of 2 are simply shifts of 1). > > I am simply using Rprintf():     long long int power[lgth];     power[lgth - 1] = 1;     Rprintf("power: %d", power[lgth - 1]);     for (j = 1; j < lgth; j++) {         power[lgth - j - 1] = 2*power[lgth - j];         Rprintf(", %d", power[lgth - j - 1]);     } Basically, I need them in reversed order (hence the inverse indexing), but the values are nonetheless the same. Adrian PS: also tried long long int, same result... -- 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]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

## Re: large integer values

 On Wed, 2014-05-14 at 18:17 +0300, Adrian Dușa wrote: > On Wed, May 14, 2014 at 5:35 PM, Simon Urbanek > <[hidden email]>wrote: > > > [...] > > > > How do you print them? It seems like you're printing 32-bit value instead > > ... (powers of 2 are simply shifts of 1). > > > > > I am simply using Rprintf(): > >     long long int power[lgth]; >     power[lgth - 1] = 1; >     Rprintf("power: %d", power[lgth - 1]); >     for (j = 1; j < lgth; j++) { >         power[lgth - j - 1] = 2*power[lgth - j]; >         Rprintf(", %d", power[lgth - j - 1]); >     } > > > Basically, I need them in reversed order (hence the inverse indexing), but > the values are nonetheless the same. > Adrian > > PS: also tried long long int, same result... Your numbers are being coerced to int when you print them. Try the format ", %lld" instead. Martyn ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

## Re: large integer values

 On Wed, May 14, 2014 at 6:24 PM, Martyn Plummer <[hidden email]> wrote: > [...] > > Your numbers are being coerced to int when you print them. Try the > format ", %lld" instead. Oh my goodness, this was a printing issue...! (feeling embarrassed, but learned something new) Problem solved, thanks very much all, Adrian -- 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]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-devel