flâneur — a map of the web's best reading

Python Gotcha: Comparisons · Ponderings of an Andy

andrewwegner.com · 1,090 words · saved by 1 readers

Comparing two numerical variables in Python can have surprising results if you aren't aware of some common gotchas. This post covers a couple of the common ones.

Article Contents Float Equality Comparisons Decimal isclose is vs. == nan == nan Conclusion Float Equality Comparisons ¶ Like every other programming language, Python can't accurately represent floating-point numbers. I'm sure many Computer Science students have lost many hours learning how floating representation works . I remember that class well. In any case, let's get into the problem with comparing float values in Python and how to handle it. >>> 0.1 + 0.2 == 0.3 False You, me, and anyone with a few years of elementary school under their belt can see that this should be True . What's happ

Explore this link on the map →

related reading