Closed pachadotdev closed 1 year ago
here's a MWE
> is_na_or_inf(c(1,NA,3,Inf,5))
[1] TRUE TRUE TRUE TRUE TRUE
> is_na_or_inf(c(1,NA,3,Inf,5))
[1] FALSE TRUE FALSE TRUE TRUE
> is_na_or_inf(c(1,NA,3,Inf,5))
[1] TRUE TRUE TRUE TRUE TRUE
and the corresponding C++ code (called with the "source" button is
#include <cpp11.hpp>
#include <cpp11/doubles.hpp>
#include <cpp11/logicals.hpp>
using namespace cpp11;
[[cpp11::register]] logicals is_na_or_inf(SEXP x)
{
int nobs = Rf_length(x);
double *px = REAL(x);
bool anyNAInf = true;
writable::logicals is_na_inf(anyNAInf ? nobs : 1);
for (int i = 0; i < nobs; ++i)
{
double x_tmp = px[i];
if (std::isnan(x_tmp))
{
is_na_inf[i] = true;
}
else if (std::isinf(x_tmp))
{
is_na_inf[i] = true;
}
}
return is_na_inf;
}
Unless I am missing something, you never initialize the output vector to false or explicitly set the value to false if it is not a NA or an Inf. cpp11 doesn't do any initialization for you, so you are seeing random memory effects due to reading uninitialized memory.
I have a function migrated from Rcpp to cpp11 that should take a vector, say
x <- c(1,8,NA,2)
, and returnc(F,F,T,F)
withT
for each value that is NA or NaN. This function is a simple for loop that usesstd::
. If I run the function two or more times, I get 2 or more different outputs that shouldn't be random. I described it here https://github.com/pachadotdev/fixest2/issues/27#issuecomment-1426900420.Btw, I am working on fixest2, which is fixest adapted to use cpp11 instead of Rcpp
The steps to obtain the error from bash/shell are
the exact error with the output of a vector function can be seen here https://github.com/pachadotdev/fixest2/issues/39#issuecomment-1427116435
also in R...
first run:
second run:
the real T/F vector should be: