Python Gotcha: Comparisons · Ponderings of an Andy
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
related reading
- Adding Floating-Point Decimals for Fun and Profitblog.vero.site
- styleguide | Style guides for Google-originated open-source projectsgoogle.github.io
- Python's Array: Working With Numeric Data Efficiently – Real Pythonrealpython.com
- GitHub - Experience-Monks/math-as-code: a cheat-sheet for mathematical notation in code formgithub.com
- Pyrefly vs. ty: Comparing Python’s Two New Rust-Based Type Checkers | Edward Li's Blogblog.edward-li.com
- GitHub - pablogsal/python-horror-show: Strange and odd python snippets explainedgithub.com
- There are Only Four Billion Floats-So Test Them All! | Random ASCII – tech blog of Bruce Dawsonrandomascii.wordpress.com
- (Quite) A Few Words About Asyncyoric.github.io
- Python representation of negative integers - Stack Overflowstackoverflow.com
- Python基本語法 - HackMDhackmd.io
- Floating point from scratch: Hard Mode · Tales on the wireessenceia.github.io
- log is non-monotonic in PHP and Luapurplesyringa.moe