Hi everybody, here's my problem: i call a C function which calculates a large number of double values and puts them into an array which is passed from R as a parameter in the function (like .C("function", other parameters, result = as.double( c ( 1 : quantity ) ). When the values come back to R in the result array, they are all truncated to their integer value (i.e. I lose the decimal parts). I think that the problem is that this calculated double numbers are actually too long, with many decimals, because i changed in code the calculated values with arbitrary double values with a few decimals and it works. Has anybody any idea of where the problem is? Thanks in advance! -- View this message in context: http://r.789695.n4.nabble.com/C-function-double-problem-tp4652537.html Sent from the R devel mailing list archive at Nabble.com.
Hello, The double in R and the double in C are the same type, with the same number of decimals (64 bits), so it's not because the numbers are too long. Try calling .C("function", other parameters, result = double(quantity)). Or maybe the error is in the C code, of which we know nothing about. Rui Barradas Em 08-12-2012 02:26, mpietro escreveu:> Hi everybody, > > here's my problem: > > i call a C function which calculates a large number of double values and > puts them into an array which is passed from R as a parameter in the > function (like .C("function", other parameters, result = as.double( c ( 1 > : quantity ) ). > When the values come back to R in the result array, they are all truncated > to their integer value (i.e. I lose the decimal parts). > I think that the problem is that this calculated double numbers are actually > too long, with many decimals, because i changed in code the calculated > values with arbitrary double values with a few decimals and it works. > > Has anybody any idea of where the problem is? > > Thanks in advance! > > > > -- > View this message in context: http://r.789695.n4.nabble.com/C-function-double-problem-tp4652537.html > Sent from the R devel mailing list archive at Nabble.com. > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel
That is great example of why folks should NOT use the .C() interface. Rcpp can help here, __even if you do not use any Rcpp language features__. Below is a showcase for the recent sourceCpp() function which takes a C++ file (shown below), converts it and optionally runs the embedded R code. Think of as something like Sweave for mixing C++ and R rather than Latex and R. Source file is shown via cat on the cmdline: edd at max:/tmp$ cat dblvec.cpp #include <Rcpp.h> #include <vector> // [[Rcpp::export]] std::vector<double> dbl(std::vector<double> x) { std::vector<double> y(x.size()); for (unsigned int i=0; i<y.size(); i++) y[i] = 2.0*x[i]; return y; } /*** R myx <- seq(1.1, 5.5, by=1.1) print(dbl(myx)) ***/ edd at max:/tmp$ edd at max:/tmp$ We then simply source it to show off how it transform a C++ vector of doubles: edd at max:/tmp$ R --quiet -e 'library(Rcpp); sourceCpp("dblvec.cpp")' R> library(Rcpp); sourceCpp("dblvec.cpp") R> myx <- seq(1.1, 5.5, by = 1.1) R> print(dbl(myx)) [1] 2.2 4.4 6.6 8.8 11.0 R> R> edd at max:/tmp$ No make, compiler invocation, linking, ... which all happen behind the scenes. We just enjoy the C++ function to double the content of a double vector. Hope this helps, Dirk -- Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
Seemingly Similar Threads
- Rcpp, dyn.load and C++ problems
- Rcpp, dyn.load and C++ problems
- Bug report: R.home() cause package Rcpp failed executing sourceCpp, similar bug are labeled "BUG 16660" since 2016 and here I could provide a solution that tested in my laptop.
- Shade area under curve
- Would like to add this to example for plotmath. Can you help?