Please excuse the newbie question.
I'm a high school teacher that is introducing vpython in an introductory high school physics class.
I'm also learning quite a bit along the way. I *think* this is a floating point problem.
When I run the following...
GlowScript 2.9 VPython
dt=0.01
t=0
x=0
v=2
while t<10:
x=x+v*dt
t=t+dt
print("time = ",t)
print("position = ",x)
I get...
time = 10.01
position = 20.02
I've played around with various values for dt. I get exactly time=10 and position=20 when dt=0.025 for example but not for 0.02.
One solution that I've come up with is:
GlowScript 2.9 VPython
dt=0.01
t=0
x=0
v=2
while abs(t-10)>dt/2:
x=x+v*dt
t=t+dt
print("time = ",t)
print("position = ",x)
This results in time=10 and position=20 for every dt that I've tested.
My questions:
1. Is there a simpler solution here?
2. Do you have suggestions on how I can simply explain this to my beginning coders?
Thanks in advance.