lorsque je tente de vérifier si la variable flottante contient la valeur entière exacte i obtenir le comportement étrange folowing. Mon code:comportement float et int Python
x = 1.7 print x, (x == int(x))
x += 0.1 print x, (x == int(x))
x += 0.1 print x, (x == int(x))
x += 0.1 print x, (x == int(x))
print "----------------------"
x = **2.7** print x, (x == int(x))
x += 0.1 print x, (x == int(x))
x += 0.1 print x, (x == int(x))
x += 0.1 print x, (x == int(x))
je reçois la sortie étrange folowing (dernière ligne est le problème):
1.7 False
1.8 False
1.9 False
2.0 True
----------------------
2.7 False
2.8 False
2.9 False
3.0 False
Toute idée pourquoi 2.0
est true
et 3.0
est false
?
[Ceci est la lecture pertinente des documents.] (https://docs.python.org/2/tutorial/ floatingpoint.html) – SuperBiasedMan
[Le calcul à virgule flottante est-il rompu?] (http://stackoverflow.com/q/588004/995714). http://floating-point-gui.de/, [Ce que tout informaticien doit savoir sur Arithmétique à virgule flottante] (http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) –