Equality tests on floats: add support for math.isclose() and numpy.isclose()
See original GitHub issueFeature request
When testing the equality of two floating point numbers, it’s good practices to rely on approximations (e.g., abs(a - b) < epsilon
) rather than on exact/bit-to-bit comparison.
In Python we can use math.isclose
or numpy.isclose
to do such tests. These functions are a bit different but both can be equally useful. Unlike the built-in math.isclose
, numpy.isclose
is not symmetric in a
and b
– it assumes b
is the reference value – so that isclose(a, b)
might be different from isclose(b, a)
.
- Floating point comparison is a common enough operation so that Numba can support it (for both
math
andnumpy
libraries). - Moreover, all arguments of each function should be supported (relative tolerance, absolute tolerance, and
equal_nan
if existing) because each use case can have specific parameters.
I could not find an issue about that when searching issues with keywords “isclose”, “float comparison”, “float equality”.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:13 (9 by maintainers)
Top Results From Across the Web
numpy.isclose — NumPy v1.24 Manual
Returns a boolean array where two arrays are element-wise equal within a tolerance. The tolerance values are positive, typically very small numbers. The ......
Read more >PEP 485 – A Function for testing approximate equality
This PEP proposes the addition of an isclose() function to the standard library math module that determines whether one value is ...
Read more >The Right Way to Compare Floats in Python - davidamos.dev
The trick is to avoid checking for equality. Never use == , >= , or <= with floats. Use the math.isclose() function instead:...
Read more >What is the best way to compare floats for almost-equality in ...
Python 3.5 adds the math.isclose and cmath.isclose functions as described in PEP 485. If you're using an earlier version of Python, ...
Read more >NumPy isclose Explained with examples in Python
The function NumPy isclose() does that for you. Also, it provides you with an option to add tolerant value, which comes very handy...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This comment appears to be spam since it is out of context and doesn’t make sense in the current conversation. My advice is: DO NOT CLICK THE LINK since it may very well redirect you to malware. I have reported the user @codeFairOfficial to the GitHub abuse program as outlined here: https://docs.github.com/en/github/building-a-strong-community/reporting-abuse-or-spam
I’d like to have this feature as well.