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

问题描述 投票:1回答:1

我编写了一个将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 

我错过了这里的东西吗?请提出建议,并感谢您的宝贵时间。

c++ r integer 64-bit rcpp
1个回答
0
投票

我的快速猜测(证明是正确的):最大值自身可能是标记为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()
*/
© www.soinside.com 2019 - 2024. All rights reserved.