Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Equality tests on floats: add support for math.isclose() and numpy.isclose() #5977

Open
alexandrehuat opened this issue Jul 10, 2020 · 13 comments
Labels
feature_request good first issue A good issue for a first time contributor

Comments

@alexandrehuat
Copy link

alexandrehuat commented Jul 10, 2020

Feature 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 and numpy 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".

@stuartarchibald
Copy link
Contributor

Thanks for the suggestion. I've added this as a feature request. If you'd like to contribute, the high level Numba extension API @overload decorator should get you on the way to implementing this for Numba. Docs are here. A lot of example uses are in 'numba/np/arraymath.py'. Thanks again.

@esc
Copy link
Member

esc commented Feb 5, 2021

@alexandrehuat It looks like no one is answering this question 😞 . If you would still like an answer, Rafia at codeFair knows Python and can help! Fill out this form if you're interested: http://tinyurl.com/2uttspwt.

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

@codeFairOfficial
Copy link

codeFairOfficial commented Feb 5, 2021

This is not spam, but we are so sorry to intrude! We actually are company looking to find new ways to get issues resolved quickly. We currently have a team of programmers finding issues that they could help get fixed. Our appologies for coming off as spam/malware, we will take this into account next time. I have deleted the original post

@esc
Copy link
Member

esc commented Feb 5, 2021

@codeFairOfficial thank you for following up on this. We (the maintainers) always do welcome new members to our community, especially when it revolves around offering commercial services related to Numba. To avoid any false-positive classifications of your submissions, I would suggest introducing yourselves, your company and maybe your programmers on our Community forum at: https://numba.discourse.group/ -- this would also allow you to discuss with our community, your approach to finding issues to resolve in order to find consensus on a good process. For example, it will make sense to discuss with users about their needs in the issue itself rather than linking to an external service since "random hyperlinks" are always a cause of suspicion.

@codeFairOfficial
Copy link

@esc thank you for the feedback, we couldn't agree more. We will reach out to your community forum and discuss before posting any further.

@esc
Copy link
Member

esc commented Feb 5, 2021

@codeFairOfficial thank you!

@alexandrehuat
Copy link
Author

alexandrehuat commented Feb 5, 2021 via email

@jpivarski
Copy link
Contributor

Similar to #6857, I think np.isclose has been implemented:

>>> import numpy as np
>>> import numba as nb
>>> nb.__version__
'0.55.1'
>>> def g(a, b):
...     return np.isclose(a, b)
... 
>>> g(np.array([1.1, 2.2, 3.3]), np.array([1.1+1e-12, 2.0, 3.3+1e-12]))
array([ True, False,  True])

Similarly for math.isclose:

>>> import math
>>> def h(a, b):
...     return math.isclose(a, b)
... 
>>> h(1.1, 1.1+1e-12)
True
>>> h(1.1, 1.0)
False

So I think this issue can be closed.

@esc
Copy link
Member

esc commented Feb 18, 2022

@jpivarski thank you for the heads up!

@esc esc closed this as completed Feb 18, 2022
@jpivarski
Copy link
Contributor

Sorry—I have to take that back!

np.isclose and math.isclose are not implemented in Numba.

In my tests, I forgot to add the @nb.njit decorator.

@jpivarski
Copy link
Contributor

This issue should be reopened!

@esc esc reopened this Feb 19, 2022
@esc
Copy link
Member

esc commented Feb 19, 2022

This issue should be reopened!

Of course, no problem! Errare humanum est! 😘

@renatobellotti
Copy link

I'd like to have this feature as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature_request good first issue A good issue for a first time contributor
Projects
None yet
Development

No branches or pull requests

6 participants