因此,我试图做我的一个项目,我在MATLAB中做的C++,但我被困在路上。
这是在MATLAB中代码的一部分,我想要转换成C++。它确实在MATLAB上工作,但在C++中不工作。
RelRough = [0, 1E-6, 5E-6, 1E-5, 5E-5, 0.0001, 0.0002, 0.0004, 0.0006, 0.0008, 0.001];
ReT = [4000, 5000, 6000, 7000, 8000, 9000, 10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000, 90000, 100000, 200000, 300000, 400000, 500000];
for i = 1:length(ReT)
for j = 1:length(RelRough)
FCT_guess = 1;
tolerance = 1;
while tolerance > 1e-14
FCT_cal = 1/(-2*log10((RelRough(j)/3.7) + (2.51/(ReT(i)*sqrt(FCT_guess)))))^2;
tolerance = abs(FCT_cal-FCT_guess);
FCT_guess = FCT_cal;
FCT(i,j) = FCT_cal*1000;
end
end
end--这是我的C++版本,我一直在为变量g获得类似于“表达式必须具有整型或非限定域枚举类型”的错误。
double RelRough[] = { 0, 1E-6, 5E-6, 1E-5, 5E-5, 0.0001, 0.0002, 0.0004, 0.0006, 0.0008, 0.001 };
const int lengthRelRough = sizeof(RelRough) / sizeof(RelRough[0]);
double ReT[] = { 4000, 5000, 6000, 7000, 8000, 9000, 10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000, 90000, 100000, 200000, 300000, 400000, 500000 };
const int lengthReT = sizeof(ReT) / sizeof(ReT[0]);
double fct[lengthReT][lengthRelRough] = { 0 };
double fct_guess = 1;
double tolerance = 1;
double fct_cal = 0;
for (int ii = 0; ii < lengthReT; ++ii) {
for (int jj = 0; jj < lengthRelRough; ++jj) {
while (tolerance > 1e-14) {
double h = (RelRough[jj] / 3.7), w = (2.51 / (ReT[ii] * sqrt(fct_guess)));
double g = (-2*log10(h+w))^2;
fct_cal = 1/g;
tolerance = abs(fct_cal - fct_guess);
fct_guess = fct_cal;
fct[ii][jj] = fct_cal;
std::cout << fct[ii][jj] << "\t";
}
}
}
return 0;}
有没有人帮我看看是怎么回事。提前感谢!
发布于 2017-12-31 02:31:24
改变这一点:
double g = (-2*log10(h+w))^2;转入:
double g = pow(-2*log10(h+w),2.0);正如@Eljay在他的评论中指出的那样,运算符^在C++中执行XOR,而不是指数运算。有关详细信息,请参阅:
https://stackoverflow.com/questions/48039416
复制相似问题