1

I created a list of numbers via [i for i in np.arange(0.0,0.3,0.05)]. This returned an unusual result:

[0.0, 0.05, 0.1, 0.15000000000000002, 0.2, 0.25]

The args for np.arange() are start,stop, and step. With a step of 0.05 and a start/stop of 0.0 - 0.3, I would have expected an arange of

[0.0, 0.05, 0.1, 0.15, 0.2, 0.25]

What is causing that odd 2e-18?

Yehuda
  • 1,787
  • 2
  • 15
  • 49
  • 3
    From the [doc](https://www.bing.com/search?form=MOZLBR&pc=MOZI&q=np.arange) *When using a non-integer step, such as 0.1, the results will often not be consistent. It is better to use numpy.linspace for these cases.* – Quang Hoang Dec 23 '20 at 04:05
  • Interestingly, even `np.linspace(0.0, 0.25, 6).tolist()` produces the same first output. – Cainã Max Couto-Silva Dec 23 '20 at 04:10
  • 1
    https://stackoverflow.com/questions/588004/is-floating-point-math-broken Simply try `.1 + .05 ` in python and it gives `0.15000000000000002` . Try - `list(np.arange(0.0,0.3,0.05).round(2))` if you want to round it off. – sharathnatraj Dec 23 '20 at 04:14

0 Answers0