我对这个实验中h
的值有点疑惑。在 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;
}
给出:
4 0 0.51 196 7 3
4 1 0.51 196 7 3
103 2 0.51 196 7 3
我正在尝试获取(这是在 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
cpp 构造有什么问题?
最佳答案
这个
(j+1)/J
是整数除法,因此会被截断。明确地将其中一个操作数转换为 float :
(j+1)/(float)J
关于c++ - 奇怪的c++算术,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11776635/