I’m a bit puzzled by the value of h in this experiment. In cpp,
int h,J=3,n=200,p=3,h_m=(n+p+1)/2;
float rt=(float)h_m/n;
for(int j=0,j<J,j++){
h=floor((j+1)/J*rt*(n-p-1)+p+1);
std::cout<<h<<" "<<j<<" "<<rt<<" "<<n-p-1<<" "<<h_i<<" "<<J<<std::endl;
}
gives:
4 0 0.51 196 7 3
4 1 0.51 196 7 3
103 2 0.51 196 7 3
I’m trying to get (this is in R):
n<-200
p<-3
h_m<-as.integer((n+p+1)/2)
J<-3
j<-0:(J-1)
rt<-h_m/n
floor((j+1)/J*rt*(n-p-1)+p+1)
[1] 37 70 103
what is wrong in the cpp construct?
This
is integer division, and therefore gets truncated. Expicitly cast one of the operands to float: