1

I hope to get a simple decimal range by numpy.arange . But, my code outputs unexpected result and size. My snippet shown below.

len(arange(0.60,0.70,0.01))

it returns size 10 ndarray object

array([0.60, ... ,0.69])

And, the max value is changed

len(arange(0.60,0.80,0.01))

array([0.60, ... , 0.79, 0.80])

I expected it will also return size 20 one.

array([0.60, ... , 0.79])

But, the size is 21 and 0.80 is included.
Could you explain Why is this? and What is the difference between two ranges?

Python : 3.5.2
numpy : 1.10.1

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
ric
  • 129
  • 1
  • 3
  • 11

1 Answers1

1

As is writen in numpy.arange documentation,

"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."

Note that linspace includes both endpoint, so this np.linspace(0.60,0.69,10) will produce

array([ 0.6 ,  0.61,  0.62,  0.63,  0.64,  0.65,  0.66,  0.67,  0.68,  0.69])
Miriam Farber
  • 18,986
  • 14
  • 61
  • 76