-3
import numpy as np
a=np.arange(1e-10,2e-10,1e-11)
print(len(a))
b=np.arange(0.1,0.2,0.01)
print(len(b))

Value of a is 11 and value of b is 10. Why is that? I've known that arange func is the interval including start but excluding stop.

  • 2
    From the doc: 'When using a non-integer step, such as 0.1, the results will often not be consistent. It is better to use linspace for these cases.' – Thierry Lathuille Jan 28 '19 at 09:18
  • Value of a is 11 and value of b is 10. Why is that? Because your are printing the length of the array. And np.arange(0.1,0.2,0.01) [0.1 + 10 * 0.01] = 0.2 or [0.1 , 0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19] – Jon.. Jan 28 '19 at 09:32

1 Answers1

-1

Because you are passing an exponentinal, non-integer value to the step parameter of the arange method. It may cause a not consistent output according to the official documentation.

Try using linspace method instead. Here is the point from the official documentatin for numpy.arrange;

When using a non-integer step, such as 0.1, the results will often not be consistent. It is better to use linspace for these cases.

Ozan Yurtsever
  • 1,274
  • 8
  • 32