<div dir="ltr">Dear all,<div><br></div><div>I have a strange question such that I can't conclude it in the title.</div><div>I have write an function:</div><div>//[[Rcpp::export]]</div><div>double mydcov(NumericVector x, NumericVector y)</div>
<div><br></div><div>This function is computing <a href="http://en.wikipedia.org/wiki/Distance_correlation#Distance_covariance">ditance covariance</a>. This function is OK. It give the right answer.</div><div>I use this function to compute <a href="http://en.wikipedia.org/wiki/Distance_correlation">distance correltion</a>. </div>
<div><br></div><div>//[[Rcpp::export]]</div><div>double mydcor(NumericVector x, NumericVector y)</div><div>{</div><div> return mydcov(x,y)/sqrt(mydcov(x,x)*mydcov(y,y));</div><div>}</div><div><br></div><div>Then, I compile the source code using sourceCpp.</div>
<div>And then mydcor(x,y) give a wrong answer.</div><div><br></div><div>Here comes the strange part:</div><div>In R, I run code as follow</div><div><br></div><div>res1 <- mydcov(x,y)/sqrt(mydcov(x,x)*mydcov(y,y))</div>
<div><br></div><div>a <- mydcov(x,y)</div><div>b <- mydcov(x,x)</div><div>c <- mydcov(y,y)</div><div>res2 <- a/sqrt(b*c)</div><div><br></div><div>and res2 is different from res1.(res2 is the right answer.)</div>
<div><br></div><div>And I have another test, rewriting the source code for mydcor():</div><div>version1:</div><div><div>//[[Rcpp::export]]</div><div>double mydcor(NumericVector x, NumericVector y)</div><div>{</div><div> double a = mydcov(x,y)</div>
<div> double b = mydcov(x,x);</div><div> double c = mydcov(y,y);</div><div> return (a/sqrt(b*c));</div><div>}</div></div><div><br></div><div>version2:</div><div><div>//[[Rcpp::export]]</div><div>double mydcor(NumericVector x, NumericVector y)</div>
<div>{</div><div> double a = mydcov(x,y)</div><div> Rcout<<a<<"\n";</div><div> double b = mydcov(x,x);</div><div> Rcout<<b<<"\n";</div><div> double c = mydcov(y,y);</div>
<div> Rcout<<c<<"\n"</div><div> return (a/sqrt(b*c));</div><div>}</div></div><div>This time, version2 make a right answer.</div><div>It's too weird. I havn't meet such situation before.</div>
<div><br></div><div>Happy Lunar New Year (the most important festival in China), and Thanks in advance.</div><div><br></div><div>Best regards</div><div>ZhangYet</div></div>