通过 Rcpp 和 bit64 R 包将最大 int64_t 变量值从 C++ 传递到 R

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

我写了一个函数来计算 2 的给定次方。我想使用 64 位整数。在 R 中,bit64 包具有以下最大和最小限制:

来自R

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

这是 -(2^63)2^63

但是,出于某种原因,我的 Rcpp 代码只能将 2^62 传回 R。这是我的函数的代码,它将 2 提高到给定的幂(注意:我使用 bit -转变以实现这一目标):

C++代码:

// [[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;
}

但是,当我运行这个在R时,我无法获得最大的possible/limiting值!

来自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 

我是不是漏掉了什么?请指教,感谢您的宝贵时间。

我的快速猜测(事实证明是正确的):最大值 本身 可能被标记为 NA。所以计算 'one minus' 这个值并尝试一下。

我的快速猜测:最大值可能是标记为 NA 的值。所以计算 'one minus' 该值并尝试

// [[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> largeval()
C++ value: 9223372036854775806
integer64
[1] 9223372036854775806
R> 

以下完整代码以防万一。

代码

#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()
*/