Reputation: 11738
Reprex:
// [[Rcpp::export]]
int dbl2int(double x) { return x; }
/*** R
dbl2int(NA_real_)
*/
Rcpp::sourceCpp("test-dbl2int.cpp")
On Mac, it outputs 0, while it outputs NA_INTEGER on Windows and Linux (cf. https://github.com/privefl/testbigstatsr/actions/runs/10737751205/job/29779881355#step:6:165). Note: this issue appeared only recently on Mac (it has given NA_INTEGER for years..).
Any idea what's going on, and how to do this conversion so that it works for all platforms and is still efficient?
Edit: It seems that Inf
is now converted to 2147483647
instead of NA_INTEGER; but -Inf
is still converted to NA..
Upvotes: 2
Views: 71
Reputation: 1265
This may well be risky code. You are relying an a very non-standard NA conversion: only R knows about NA for integer, so you are betting on the appropriate R conversion being found. I am not sure we can rely on that.
Two things come to mind. First, maybe see if you can distill this into a pure C API for R function and then report to r-devel. (Also, if and when that works and this does not, file an Rcpp bug showing that.)
Second, use a safer converter. Something like
Rcpp::cppFunction("int dbl2int(double x) {
if (ISNAN(x)) return NA_INTEGER; else return static_cast<int>(x); }")
You can also test via R_IsNA()
, there is a bit of overlap between test predicates.
Upvotes: 5