increase precision in Rcpp floating-point output
r, rcpp
Solution
Looking at your linked answer, you're missing the call to `std::fixed`:
code2 <- '
double x=1.0;
Rcpp::Rcout.precision(10);
Rcpp::Rcout << "(1) " << std::fixed << x << std::endl;
return Rcpp::wrap(0);
'
fun2 <- rcpp(sig=c(v=0),body=code2,includes="#include <iomanip>")
fun2(1)
## (1) 1.0000000000
## [1] 0
Problem
I'm trying to print more digits in double-precision output from an `Rcpp` function, but can't figure out how ... I've looked at How do I print a double value with full precision using cout? and elsewhere for the generic C++ answer, but I can't see how to do it in `Rcpp`, except by using `printf`, which I take to be a last resort ... ``` require(inline) code <- ' double x=1.0; std::cout.precision(10); // compiles but does nothing Rcpp::Rcout.precision(10); // compiles but does nothing printf("(1) %1.10lf\\n",x); // works but bad practice Rcpp::Rcout << "(2) " << x << std::endl; Rcpp::Rcout << "(3) " << std::setprecision(10) << x << std::endl; return Rcpp::wrap(0); ' fun <- rcpp(sig=c(v=0),body=code,includes="#include <iomanip>") fun(1) ## (1) 1.0000000000 ## (2) 1 ## (3) 1 ## [1] 0 ```