jeudi 24 septembre 2015

If statement is false but seems true [duplicate]

This question already has an answer here:

I have python code with multiple nested for loops.

For example:

for a in range(10):
    variable_A = a*0.1
    for b in range(10):
        variable_B = b*0.1
        for c in range(10):
            variable_C = c*0.1

During testing I had the following:

print variable_C
if variable_C == 0.4:
    print "variable_C is 0.4 here"

Variable_C would print on each loop, e.g.:

0.0
0.1
0.2
0.3
0.4
0.5
.
.

but it doesn't go into the IF statement, even though the code tells me that variable_C is 0.4 right before, in the print command.

If I change the value in the IF statement from 0.4 to 0.3 (or 0.2 or 0.1 or 0.0), it works okay. But it has a problem at 0.4 and after.

Is it possible that it is printing "0.4" but variable_C is really 0.400000001 or something due to my nested loops?

Or am I doing something dumb?

I can post the code if necessary, but it's a lot more messy than this. The essence is the same though, I'm pretty sure.

Aucun commentaire:

Enregistrer un commentaire