I need some help understanding the difference between two boolean comparisons in the following code:
x=10.0
for i in range(10):
x+=0.1
#print x
print type(x)
print x
print type(11.0)
print x == 11.0
for i in range(10):
x-=0.1
#print x
print type(x)
print x
print type(10.0)
print x == 10.0
The code yields:
<type 'float'>
11.0
<type 'float'>
False
<type 'float'>
10.0
<type 'float'>
True
As you can see all variables are of type float, but when comparing x == 11.0 the boolean comparison returns False. In the next loop, the value of x returns to its original value of 10.0 and when compared returns True.
I'm not sure exactly why one is False and the other is True when I believe both items being compared are of the same value and type in both comparisons. I suspect the issue lies with casting and converting types, but I could be wrong.