[英]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.