繁体   English   中英

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

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

我写了一个 function 来将 2 提高到给定的功率。 我想使用 64 位整数。 在 R 中,bit64 package 的最大和最小限制如下:

R

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

这是-(2^63)2^63

但是,由于某种原因,我的Rcpp代码只能将2^62传递回 R。 这是我的 function 的代码,它将 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()
*/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM