Notice numpy.arange()
is using ceil
to count the numbers, for example,
a = np.arange(.1,.3,.1)
print(len(a)) # 2
print(a) # [0.1 0.2]
a = np.arange(.1,.4,.1)
print(len(a)) # 4
print(a) # [0.1 0.2 0.3 0.4]
The result is not as simple as (stop - start)/step
, due to ceilling, which adds up 1.
We can use numpy.linespace() if we know how many numbers we want, as below
a = np.linspace(.1,.4,3,endpoint=False) # do not include 0.4
print(len(a)) # 4
print(a)
Now let us try time4,
time4 = np.linspace(0.6, 0.8, 400,endpoint = False)
print(len(time4))
print(time4)
We can use parameter retstep to see the real step, as below,
time4 = np.linspace(0.6, 0.8, 400,endpoint = False, retstep=False)
print(time4[1]) # 0.0005000000000000001
Now we get real understanding of ceilling
, and we rewrite the assignment,
time4 = np.arange(0.6, 0.8, 0.0005000000000000001)
print(len(time4)) # 401 again, no wonder
time4 = np.arange(0.6, 0.8 - 0.0005000000000000001, 0.0005000000000000001)
print(len(time4)) # 400 now, as expected
print(time4)
What time4 = np.arange(0.4+0.2, 0.6+0.2, 0.0005)
does is the same, it puts the ceilling to 400, thus woks fine. Anyway, python is not pure math, it is just representing math :(