EngComp icon indicating copy to clipboard operation
EngComp copied to clipboard

Review use of numpy.arange() to define t array

Open ncclementi opened this issue 8 years ago • 0 comments

Same dt in both cases, The last element is supposed not to be inclusive, Why in the first case works and in the second doesn't? Any idea?

In [16]: dt = 0.005

In [17]: t1=numpy.arange(0, 50.005, dt)

In [18]: t1[-1]
Out[18]: 50.0

In [19]: t2=numpy.arange(0, 10.005, dt)

In [20]: t2[-1]
Out[20]: 10.005000000000001

According to numpy.arange documentation:

For floating point arguments, the length of the result is ceil((stop - start)/step). Because of floating point overflow, this rule may result in the last element of out being greater than stop.

caso a) numpy.ceil((stop - start)/dt) returns 10001.0

caso b) numpy.ceil((stop - start)/dt) returns 2002.0

ncclementi avatar Dec 11 '17 20:12 ncclementi