I have a rounding issue in Python 2.7 leading to unexpected output. I'm trying to get combinations of p1 and p2 that sum up to 0.6 or less in total.
from itertools import product
P = []
p1 = [0.0,0.2,0.4,0.6]
p2 = [0.0,0.2,0.4,0.6]
for p11,p22 in product(p1,p2):
if p11+p22 <= max(p1):
P.append((p11,p22))
However, when I run this, it does not include all values for which p11+p22 = 0.6:
[(0.0, 0.0),
(0.0, 0.2),
(0.0, 0.4),
(0.0, 0.6),
(0.2, 0.0),
(0.2, 0.2),
(0.4, 0.0),
(0.6, 0.0)]
It does work correctly when I set p11+p22 <= max(p1)+0.01
. For different p1
and p2
the problem may or may not occur. I find this behavior extremely strange, leading to very unreliable results.
It is probably related to floating precision issues. In my opinion this behaviour shouldn't exist in Python, since R and Matlab do not have this behaviour either. Are there any simple ways around this?