It also depends how loop variables are incremented in python...
(i+1)-th value of loop could be either
value[i] = startValue + i * increment
or
value[i] = value[i-1] + increment
In floating point, these values will differ...
So if you want to deal with strict floating point equality, you'll have to know this kind of detail
Also, it's very hard to control when the loop will stop... The last value could well be (3.0-epsilon)
A first rule is to not use loops on floating point whatever the language
Use integers instead:
def test(numList):
yesList=[]
for num in numList:
print num, "start here"
for i in np.arange(20,30,1):
print i*0.1
if num==(i/10.0): yesList.append(num)
return yesList
Also note that i*0.1 and i/10.0 might differ since the float 0.1 is not exactly 1/10...
So, if you write if num==(i*0.1) it won't work as you expect...
For example (23*0.1) == (23/10.0) is false, and while the last happens to be strictly equal to the float 2.3, they are both different from the mathematical fraction 23/10.
A second rule tells to not use strict equality on float, that's rarely what you think
def test(numList):
yesList=[]
for num in numList:
print num, "start here"
for i in np.arange(20,30,1):
print i*0.1
if abs(num-0.1*i) < 0.05 : yesList.append(num)
return yesList
Please read "What Every Computer Scientist Should Know About Floating-Point Arithmetic." http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html