[英]for loop does not execute correctly in C
以下函数中的for循环似乎无法正确存储。 返回range_angle
的值是第一个值,我知道这是不正确的。 另外,我的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;
我希望此循环以.01
为增量从theta_l_deg
= 22.5
运行到83
,并返回该范围内range_angle
。 另外,我知道C仅以弧度计算,而compute_landing_distance
函数将其考虑在内。
for (theta_l_deg=22.5; theta_l_deg<=83;.01)
应该
for ( theta_l_deg = 22.5 ; theta_l_deg <= 83 ; theta_l_deg += 0.01 )
在原始代码中,您不会递增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;
错误的循环,缺少增量
for (theta_l_deg=22.5; theta_l_deg<=83;.01)
应该
for (theta_l_deg=22.5; theta_l_deg<=83; theta_l_deg += .01)
阅读http://floating-point-gui.de/ ,注意0.01
不能完全代表IEEE754
顺便说一句,您应该使用所有警告和调试信息进行编译: gcc -Wall -Wextra -g
如果使用GCC) 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.