Search code examples
c++rinteger64-bitrcpp

Passing largest int64_t variable values from C++ to R via Rcpp and the bit64 R package


I have written a function to raise 2 to a given power. I want to use 64 bit integers. In R, the bit64 package have the following for the maximum and minimum limits:

From R:

> bit64::lim.integer64()
integer64
[1] -9223372036854775807 9223372036854775807 

This is -(2^63) and 2^63.

However, for some reason, my Rcpp code can only pass 2^62 back to R. Here is the code for my function that raises 2 to a given power (NOTE: I use bit-shifting to achieve this):

C++ code:

// [[Rcpp::export]]
Rcpp::NumericVector i2_to_the_power_j ( int64_t j )
{

  int64_t base = 1;
  int64_t value = base << j;

  // cout << "C++ value: " << value << "\n";

  // Create a vector of length 1 with `value` as the sole contents
  const   std::vector<int64_t> v(1, value);
  const size_t len = v.size();

  Rcpp::NumericVector nn(len);         // storage vehicle we return them in

  // transfers values 'keeping bits' but changing type
  // using reinterpret_cast would get us a warning
  std::memcpy(&(nn[0]), &(v[0]), len * sizeof(double));

  nn.attr("class") = "integer64";
  return nn;

  return value;
}

However, when I run this in R, I cannot obtain the largest possible/limiting value!

From R:

>library(Rcpp)
>library(bit64)

> sourceCpp("./hilbert_curve_copy.cpp")

> # I can get 2^62
> i2_to_the_power_j(62)
integer64
[1] 4611686018427387904

> # ...but I cannot get 2^63
> i2_to_the_power_j(63)
integer64
[1] <NA>

> # I cannot get 2^63, despite bit64 package claiming it can
> # handle integers of this size
> bit64::lim.integer64()
integer64
[1] -9223372036854775807 9223372036854775807 

Have I missed something here? Please advise, and thank you for your time.


Solution

  • Quick guess of mine (that was proven right): the max value itself may be the one flagged for NA. So compute the 'one minus' that value and try it.

    Quick guess of mine: the max value may be the one flagged for NA. So compute the 'one minus' that value and try it

    // [[Rcpp::export]]
    Rcpp::NumericVector largeval ( ) {
      int64_t val = 9223372036854775807LL - 1;
      Rcpp::Rcout << "C++ value: " << val << "\n";
      Rcpp::NumericVector dbl(1);
      std::memcpy(&(dbl[0]), &val, sizeof(double));
      dbl.attr("class") = "integer64";
      return dbl;
    }
    

    I added that to your code and running it yields:

    R> largeval()
    C++ value: 9223372036854775806
    integer64
    [1] 9223372036854775806
    R> 
    

    Full code below just in case.

    Code

    #include <Rcpp.h>
    
    // [[Rcpp::export]]
    Rcpp::NumericVector i2_to_the_power_j ( int64_t j )
    {
    
      int64_t base = 1;
      int64_t value = base << j;
    
      // cout << "C++ value: " << value << "\n";
    
      // Create a vector of length 1 with `value` as the sole contents
      const   std::vector<int64_t> v(1, value);
      const size_t len = v.size();
    
      Rcpp::NumericVector nn(len);         // storage vehicle we return them in
    
      // transfers values 'keeping bits' but changing type
      // using reinterpret_cast would get us a warning
      std::memcpy(&(nn[0]), &(v[0]), len * sizeof(double));
    
      nn.attr("class") = "integer64";
      return nn;
    
      return value;
    }
    
    // [[Rcpp::export]]
    Rcpp::NumericVector largeval ( ) {
      int64_t val = 9223372036854775807LL - 1;
      Rcpp::Rcout << "C++ value: " << val << "\n";
      Rcpp::NumericVector dbl(1);
      std::memcpy(&(dbl[0]), &val, sizeof(double));
      dbl.attr("class") = "integer64";
      return dbl;
    }
    
    
    /*** R
    library(bit64)
    # I can get 2^62
    i2_to_the_power_j(62)
    
    # ...but I cannot get 2^63
    i2_to_the_power_j(63)
    
    # I cannot get 2^63, despite bit64 package claiming it can
    # handle integers of this size
    bit64::lim.integer64()
    
    largeval()
    */