[英]for loop does not execute correctly in C
The for loop in the following function does not seem to store correctly. 以下函数中的for循环似乎无法正确存储。 The value that returns for
range_angle
is the very first value, which I know is not correct. 返回
range_angle
的值是第一个值,我知道这是不正确的。 Also, my compute_landing_distance
function (not shown here) works just fine, so it must be something in the for loop: 另外,我的
compute_landing_distance
函数(此处未显示)可以正常工作,因此它必须在for循环中:
for (theta_l_deg=22.5; theta_l_deg<=83;.01)
{
x_f = compute_landing_distance(v0,theta_l_deg);
if (x_f > range_distance)
{
range_distance = x_f;
range_angle = theta_l_deg;
}
else
{
break;
}
}
return range_angle;
I want this loop to run from theta_l_deg
= 22.5
to 83
in increments of .01
, and return the largest value for range_angle
in that range. 我希望此循环以
.01
为增量从theta_l_deg
= 22.5
运行到83
,并返回该范围内range_angle
。 Also, I am aware that C only calculates in radians, and the compute_landing_distance
function takes that into account. 另外,我知道C仅以弧度计算,而
compute_landing_distance
函数将其考虑在内。
for (theta_l_deg=22.5; theta_l_deg<=83;.01)
should be 应该
for ( theta_l_deg = 22.5 ; theta_l_deg <= 83 ; theta_l_deg += 0.01 )
In your original code, you are not incrementing the value of theta_l_deg
在原始代码中,您不会递增
theta_l_deg
的值
for (theta_l_deg=22.5; theta_l_deg<=83; theta_l_deg+=0.01)
{
x_f = compute_landing_distance(v0,theta_l_deg);
if (x_f > range_distance)
{
range_distance = x_f;
range_angle = theta_l_deg;
}
else
{
break;
}
}
return range_angle;
Wrong for loop, missing increment 错误的循环,缺少增量
for (theta_l_deg=22.5; theta_l_deg<=83;.01)
should be 应该
for (theta_l_deg=22.5; theta_l_deg<=83; theta_l_deg += .01)
Read http://floating-point-gui.de/ and notice that 0.01
is not exactly representable as IEEE754 阅读http://floating-point-gui.de/ ,注意
0.01
不能完全代表IEEE754
BTW, you should compile with all warnings and debug info: gcc -Wall -Wextra -g
if using GCC . 顺便说一句,您应该使用所有警告和调试信息进行编译:
gcc -Wall -Wextra -g
如果使用GCC) 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.