Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Python returns a False statement when it shouldn't?

Take this code for example:

print 2.0 == 2 #---> returns True

print 12 % 5 #---> returns 2
print ((12.0 / 5) - (12 / 5)) * 5 #---> returns 2.0

print ((12.0 / 5) - (12 / 5)) * 5 == 12 % 5 #---> returns False (What the hell happens here?)
like image 261
Deneb Avatar asked Jun 13 '26 15:06

Deneb


1 Answers

Because the first calculation does not return exactly 2.0:

>>> ((12.0 / 5) - (12 / 5)) * 5
1.9999999999999996

print limits floating point display to 12 significant digits only, causing the value to be rounded to 2.0.

like image 164
Martijn Pieters Avatar answered Jun 15 '26 05:06

Martijn Pieters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!