Reputation: 3
I am trying to do an update every 20 steps but it skips some numbers I don't understand why
time=np.linspace(0.1,100,1000)
for t in time:
if t % 2 ==0.0:
print(t, 'ms')
Thank you
Upvotes: 0
Views: 89
Reputation: 5459
This due to the fact that linespace
does not achieve exact values like 18.0
or 20.0
. Due to floating point calculation there is a small epsilon. You can change your code like the following, then you will hit all the numbers:
time=np.linspace(0.1,100,1000)
epsilon = 0.0001
for t in time:
if t % 2 < epsilon:
print(t, 'ms')
Another way is to just print out every 20 step (the first print would be but after 19):
time=np.linspace(0.1,100,1000)
epsilon = 0.0001
for i,t in enumerate(time):
if (i -19) % 20 ==0:
print(t, 'ms')
Output:
2.0 ms
4.0 ms
6.0 ms
8.0 ms
10.0 ms
12.0 ms
14.0 ms
16.0 ms
....
Upvotes: 1