基于物理的小型程序中的无限循环
这是一个模拟网球从 50 米高的建筑物一侧抛出的程序。 程序应输出每个时间步的 x、y 和速度值。 但是,我似乎陷入了无限循环。
#include<stdio.h>
#include<math.h>
int main() {
//Intial values
float ax = 0; //acceleration in the horizontal direction
float ay = -9.8; //acceleration in the downward direction
float x = 0; //top of building at position 0
float y = 50; //building is height 50 m
float vx = 10*cos(30); //velocity in the horizontal direction = 10 m/s * cos(30);
float vy = 10*sin(30); //velocity in the vertical direction = 10 m/s * sin(30);
int time = 0; //time starts at 0 seconds
float deltaTime = 0.001; //increment time by .001 each iteration
//while ball is greater than 0, or above the ground which is at position 0
while(y > 0) {
time = time + deltaTime;
vx = vx + ax*deltaTime;
vy = vy + ay*deltaTime;
x = x + vx*deltaTime + (1/2*ax*deltaTime*deltaTime);
y = y + vy*deltaTime + (1/2*ay*deltaTime*deltaTime);
printf("x = %f, y = %f, vx = %f, vy = %f, time = %d, ", x,y,vx,vy,time);
}
system ("PAUSE");
return 0;
}
我的猜测是 y 永远不会变得小于 0,但由于我有限的物理知识,我不知道如何解决它。
Here is a program that models a tennis ball being thrown off the side of a 50 meter building.
The program should output the x, y, and velocity values at each time step.
However, I seem to be getting an infinite loop.
#include<stdio.h>
#include<math.h>
int main() {
//Intial values
float ax = 0; //acceleration in the horizontal direction
float ay = -9.8; //acceleration in the downward direction
float x = 0; //top of building at position 0
float y = 50; //building is height 50 m
float vx = 10*cos(30); //velocity in the horizontal direction = 10 m/s * cos(30);
float vy = 10*sin(30); //velocity in the vertical direction = 10 m/s * sin(30);
int time = 0; //time starts at 0 seconds
float deltaTime = 0.001; //increment time by .001 each iteration
//while ball is greater than 0, or above the ground which is at position 0
while(y > 0) {
time = time + deltaTime;
vx = vx + ax*deltaTime;
vy = vy + ay*deltaTime;
x = x + vx*deltaTime + (1/2*ax*deltaTime*deltaTime);
y = y + vy*deltaTime + (1/2*ay*deltaTime*deltaTime);
printf("x = %f, y = %f, vx = %f, vy = %f, time = %d, ", x,y,vx,vy,time);
}
system ("PAUSE");
return 0;
}
My guess is that y will never become smaller than 0, but because of my limited physics knowledge, I don't know how I could fix it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
1/2 == 0 而不是 0.5
由于 1 和 2 都是整数,因此使用整数除法截断为最接近的整数。使用
0.5f
获取float
或仅使用0.5
获取双精度值。1/2 == 0 not 0.5
Since 1 and 2 are both integers this uses integer division which truncates to the closest integral number. Use
0.5f
to get afloat
or just0.5
to get a double.time
是一个 int,而不是 float;那么它不会永远保持为零吗?time
is an int, not a float; so won't that stay zero forever?将时间声明为
float
,而不是int
:因此,它在当前代码中根本不会改变。
中的三角函数接受弧度,而不是度数。Declare time as
float
, notint
: it's not changing at all in your current code because of this.Trigonometric functions in
<math.h>
accept radians, not degrees.看一下这个词
Look at the term
如果您有兴趣了解您的代码如何在 C# 中进行很少的修改而无需杀手循环即可完成:
我所做的唯一修改是对 Cos 和 Sin 的强制转换以及更改浮动时间。
并在一些初始值(如转换)后添加“f”。
这可能不是 C 的真正答案,但它可能是一条线索?
If you are interested in finding out how your code, with little modification finishes without a killer loop in C#:
The only modification I did are the casts on Cos and Sin and changing time to float.
And added the 'f' after some initial values (like casting).
It may not be a real answer for C, but it could be a clue?