0

In python, why does np.arange(-1.6,-0.49,0.01) generate a list where the last element is -0.49 while np.arange(0,0.49,0.01) generates a list where the last element is 0.48?

OSUCowboy
  • 57
  • 2
  • 6

1 Answers1

3

Floating point arithmetic doesn't use base 10, so things that look perfectly simple often don't work that way in practice. The exception to this is integers as floating point, because for reasonable numbers the errors are all to the right of the decimal point. You can restructure your range to use integers and you'll get consistent results.

np.arange(-160, -49) * 0.01
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622