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

标签 c++ r integer 64-bit rcpp

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

来自 R:

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

这是 -(2^63)2^63

但是,由于某些原因,我的 Rcpp 代码只能将 2^62 传递回 R。这是我的函数的代码,该函数将 2 提高到给定的幂(注意:我使用位移位来实现此目的):

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 中运行它时,我无法获得最大可能/限制值!

来自 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。因此,计算该值的“一减”并尝试一下。

我的快速猜测:最大值可能是标记为 NA 的值。所以计算该值的“一减”并尝试一下

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

关于c++ - 通过 Rcpp 和 bit64 R 包将最大的 int64_t 变量值从 C++ 传递到 R,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62201852/

相关文章:

c++ - 在 C++ 中编写自定义 for 循环

java - 如何应对以下代码中的 java.lang.NumberFormatException?

jpa - 标准查询 - 使用 "like"搜索整数

c++ - fstream 库,试图创建一个具有变量名的文件 (c++)

c++ - 尝试使用 CImg 保存图像时出错

c++ - C++ 中 this* 的类型

rowMean 如果行通过测试

r - 将 R markdown 中的三向列联表打印到 docx

r - 为什么 mapply 多次重复同一个列表?

mysql - 在 MySQL 中以增量方式插入数百万行的最快方法