Hi the list, I need to include some C code in R, but the behavior of the C code is strange : Here is my code : --- 8< --- Rprintf("\n XXXX mTraj=%f mClus=%f",mTraj[i+nbId*c],mClustersCenter[j+nbClusters*c]); Rprintf("\nDistA=%d Tmp=%d",dist,tmp); tmp = mTraj[i+nbId* c] - mClustersCenter [j+nbClusters* c]; Rprintf("\nDistB=%d Tmp=%d",dist,tmp); dist += (tmp * tmp); Rprintf("\nDistC=%d Tmp=%d",dist,tmp); --- 8< ---- Herer are the stranges results it gives : XXXX mTraj=1.000000 mClus=3.000000 DistA=0 Tmp=0 DistB=0 Tmp=0 DistC=0 Tmp=1074790400 I ask on a C chat, but no one can answer me. Any idea of what wrong ? Thanks Christophe
On 08/02/2009 5:37 PM, Christophe Genolini wrote:> Hi the list, > > I need to include some C code in R, but the behavior of the C code is > strange : Here is my code : > --- 8< --- > Rprintf("\n XXXX mTraj=%f > mClus=%f",mTraj[i+nbId*c],mClustersCenter[j+nbClusters*c]); > > Rprintf("\nDistA=%d Tmp=%d",dist,tmp); > tmp = mTraj[i+nbId* c] - mClustersCenter [j+nbClusters* c]; > > Rprintf("\nDistB=%d Tmp=%d",dist,tmp); > dist += (tmp * tmp); > > Rprintf("\nDistC=%d Tmp=%d",dist,tmp); > --- 8< ---- > > Herer are the stranges results it gives : > > XXXX mTraj=1.000000 mClus=3.000000 > DistA=0 Tmp=0 > DistB=0 Tmp=0 > DistC=0 Tmp=1074790400 > > I ask on a C chat, but no one can answer me. > Any idea of what wrong ?You likely have the wrong types for the variables you're printing. In C, the format has to match the type of the variable; if you use the wrong one, you get garbage. Duncan Murdoch
Hi Christophe, The problem might be that you used %d when printing. I guess that your variables dist and tmp are double so you have to use %f instead. Best, Mathieu Le dimanche 08 f?vrier 2009 ? 23:37 +0100, Christophe Genolini a ?crit :> Hi the list, > > I need to include some C code in R, but the behavior of the C code is > strange : Here is my code : > --- 8< --- > Rprintf("\n XXXX mTraj=%f > mClus=%f",mTraj[i+nbId*c],mClustersCenter[j+nbClusters*c]); > > Rprintf("\nDistA=%d Tmp=%d",dist,tmp); > tmp = mTraj[i+nbId* c] - mClustersCenter [j+nbClusters* c]; > > Rprintf("\nDistB=%d Tmp=%d",dist,tmp); > dist += (tmp * tmp); > > Rprintf("\nDistC=%d Tmp=%d",dist,tmp); > --- 8< ---- > > Herer are the stranges results it gives : > > XXXX mTraj=1.000000 mClus=3.000000 > DistA=0 Tmp=0 > DistB=0 Tmp=0 > DistC=0 Tmp=1074790400 > > I ask on a C chat, but no one can answer me. > Any idea of what wrong ? > > Thanks > Christophe > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel-- Institute of Mathematics Ecole Polytechnique F?d?rale de Lausanne STAT-IMA-FSB-EPFL, Station 8 CH-1015 Lausanne Switzerland http://stat.epfl.ch/ Tel: + 41 (0)21 693 7907