# [Rcpp-devel] A strange question while using Rcpp

Dirk Eddelbuettel edd at debian.org
Fri Jan 17 14:36:02 CET 2014

```On 17 January 2014 at 20:51, 晔张 wrote:
| Dear all,
|
| I have a strange question such that I can't conclude it in the title.
| I have write an function:
| //[[Rcpp::export]]
| double mydcov(NumericVector x, NumericVector y)

It would help us to see the function to have any guess at what may be wrong.

| This function is computing  ditance covariance. This function is OK. It give
| I use this function to compute distance correltion.
|
| //[[Rcpp::export]]
| double mydcor(NumericVector x, NumericVector y)
| {
|     return mydcov(x,y)/sqrt(mydcov(x,x)*mydcov(y,y));
| }
|
| Then, I compile the source code using sourceCpp.
| And then mydcor(x,y) give a wrong answer.
|
| Here comes the strange part:
| In R, I run code as follow
|
| res1 <- mydcov(x,y)/sqrt(mydcov(x,x)*mydcov(y,y))
|
| a <- mydcov(x,y)
| b <- mydcov(x,x)
| c <- mydcov(y,y)
| res2 <- a/sqrt(b*c)
|
| and res2 is different from res1.(res2 is the right answer.)
|
| And I have another test, rewriting the source code for mydcor():
| version1:
| //[[Rcpp::export]]
| double mydcor(NumericVector x, NumericVector y)
| {
|     double a = mydcov(x,y)
|     double b = mydcov(x,x);
|     double c = mydcov(y,y);
|     return (a/sqrt(b*c));
| }
|
| version2:
| //[[Rcpp::export]]
| double mydcor(NumericVector x, NumericVector y)
| {
|     double a = mydcov(x,y)
|     Rcout<<a<<"\n";
|     double b = mydcov(x,x);
|     Rcout<<b<<"\n";
|     double c = mydcov(y,y);
|     Rcout<<c<<"\n"
|     return (a/sqrt(b*c));
| }
| This time, version2 make a right answer.
| It's too weird. I havn't meet such situation before.

I am not sure we are in a position to help you here.

Regards, Dirk

|
| Happy Lunar New Year (the most important festival in China), and Thanks in
|
| Best regards
| ZhangYet
|
| ----------------------------------------------------------------------
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
--
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
```