It seems std::stringstream doesn't work with Rcpp. To isolate the problem, I wrote a minimal program:
#include <string>
#include <sstream>
#include <Rcpp.h>
float atof(std::string a) {
std::stringstream ss(a);
Rf_PrintValue(Rcpp::wrap(a));
float f;
Rf_PrintValue(Rcpp::wrap(f));
ss >> f;
Rf_PrintValue(Rcpp::wrap(f));
return (f);
}
RcppExport SEXP tsmall(SEXP sR) {
std::string sC = Rcpp::as<std::string>(sR);
return Rcpp::wrap(atof(sC));
}
tsmall
should just convert a string to float. The Rf_PrintValue
is for debugging. Now in R on a OSX 10.16.7, I get
> dyn.load("min.so")
> a = .Call("tsmall","0.213245")
[1] "0.213245"
[1] 0
[1] 0
> a
[1] 0
On another machine (Ubuntu), it works as expected:
> dyn.load("min.so")
> a = .Call("tsmall","0.213245")
[1] "0.213245"
[1] 1.401298e-45
[1] 0.213245
> a
[1] 0.213245
I tried a small normal C++ program on the OSX, and of course it works fine to use stringstream to convert string and floats.
The compiler used on OSX is MacPorts g++-mp-4.4.
Update: I found an issue raised earlier about stringstream and OSX at Stringstream not working with doubles when _GLIBCXX_DEBUG enabled. However, when I compile the test program in that issue with the default gcc-4.2 in /usr/bin/g++-4.2 I get the error, but compiling with /opt/local/bin/g++-mp-4.4 works fine.
However, I had compiled the Rcpp code as
$ PKG_CPPFLAGS=`Rscript -e 'Rcpp:::CxxFlags()'` \
PKG_LIBS=`Rscript -e 'Rcpp:::LdFlags()'` \
R CMD SHLIB min.cpp
which used gcc-4.4:
/opt/local/bin/g++-mp-4.4 -I/opt/local/lib/R/include -I/opt/local/lib/R/include/x86_64 -I/opt/local/lib/R/library/Rcpp/include -I/opt/local/include -fPIC -pipe -O2 -m64 -c min.cpp -o min.o
/opt/local/bin/g++-mp-4.4 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/opt/local/lib -o min.so min.o /opt/local/lib/R/library/Rcpp/lib/x86_64/libRcpp.a -L/opt/local/lib/R/lib/x86_64 -lR
so I am not sure if this is the same issue.
Update 2: Following the discussion at https://discussions.apple.com/thread/2166586?threadID=2166586&tstart=0, I added the following to the top of my code:
#ifdef GLIBCXXDEBUG
#define GLIBCXX_DEBUGDEFINED "1"
#else
#define GLIBCXX_DEBUGDEFINED "<undefined>"
#endif
and also initialized f
as float f=0;
in stof
according to @Kerrek's suggestion (although this should not change anything).
The output on the Mac is still the same.