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
- There are Only Four Billion Floats-So Test Them All! | Random ASCII – tech blog of Bruce Dawsonrandomascii.wordpress.com
- 練習 - 探索浮點類型 - Training | Microsoft Learnlearn.microsoft.com
- GitHub - Experience-Monks/math-as-code: a cheat-sheet for mathematical notation in code formgithub.com
- Floating point from scratch: Hard Mode · Tales on the wireessenceia.github.io
- GitHub - pablogsal/python-horror-show: Strange and odd python snippets explainedgithub.com
- styleguide | Style guides for Google-originated open-source projectsgoogle.github.io
- log is non-monotonic in PHP and Luapurplesyringa.moe
- Ken Shirriff's blogrighto.com
- Comparison Operators in Google Sheets and Equivalent Functionsinfoinspired.com
- python's pre-declared constants are kinda weirdsebsite.pw
- Range: comparePoint() method - Web APIs | MDNdeveloper.mozilla.org