numpy arange unexpected results
numpy, python
Solution
Perhaps it has to do with limitations on floating point numbers. Due to machine precision, it is not possible to store every conceivable value perfectly as a floating point. For example:
>>> 8.4
8.4000000000000004
>>> 8.35
8.3499999999999996
So, 8.4 as a floating point is slightly greater than the actual value of 8.4, while 8.35 as a floating point is a tiny bit less.
Problem
I am using the arange function to define my for loop iterations and getting unexpected results. ``` i = arange(7.8, 8.4, 0.05) print i ``` yeilds the following: ``` [ 7.8 7.85 7.9 7.95 8. 8.05 8.1 8.15 8.2 8.25 8.3 8.35 8.4 ] ``` yet using the stop value of 8.35 as follows ``` i = arange(7.8, 8.35, 0.05) ``` yields the following ``` [ 7.8 7.85 7.9 7.95 8. 8.05 8.1 8.15 8.2 8.25 8.3 ] ``` But I want my range to end at 8.35! I know I can use the stop value of > 8.35 and < 8.4 to achieve my result, but why is it different and in my mind, inconsistent? Edit: I am using Python 2.7