0
在這個實驗中,我對h
的值有些困惑。在CPP,奇怪的C++算術
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構建?
R也在內部使用雙精度浮點數。所以如果你想要更精確地匹配R的精度,你應該在任何你想要浮點的地方使用double。 – 2012-08-02 11:43:19
不應該是'static_cast(J)',因爲這是一個C++問題? C風格演員是一種沮喪的後果 –
Grizzly
2012-08-02 12:40:22
@Grizzly我沒有看到數值轉換的問題。 – 2012-08-02 12:44:55