[Rcpp-devel] Getting the exact arithmetic as in R when Rcpp is used?

Peng Yu pengyu.ut at gmail.com
Mon Jul 8 13:51:56 CEST 2013


Hi,

The following shows the arithmetic in C++ is slightly different from
in R. Is there a way to get the exact arithmetic as in R?

~/dvcs_src/rexample/Rexample/cran/base/sum$ Rscript main_Rcpp.R
> library(inline)
> src='
+ Rcpp::NumericVector vec(vx);
+ double sum = 0;
+ for (int i=0; i<vec.size(); i++) {
+   sum += vec[i];
+ }
+ return Rcpp::wrap(sum);
+ '
> fun=cxxfunction(
+   signature(vx='numeric')
+   , src
+   , plugin='Rcpp'
+   )

Attaching package: ‘Rcpp’

The following object is masked from ‘package:inline’:

    registerPlugin

> vx=rep(.1, 10)
> options(digits=22)
> fun(vx)
[1] 0.9999999999999998889777
> sum(vx)
[1] 1
>

--
Regards,
Peng


More information about the Rcpp-devel mailing list