gpt4 book ai didi

c++ - 奇怪的c++算术

转载 作者:太空宇宙 更新时间:2023-11-04 16:26:14 30 4
gpt4 key购买 nike

我对这个实验中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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com