-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
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
Improve docs of math.isclose(0, x)
when x = 0
#131094
Comments
Seems the correction is to add a single word:
Would you mind making a PR? |
When making the PR also update the formatting (the parameters should be emphasized). I think the suggested change to the doc is an improvement, although with floating point arithmetic there are some corner cases:
results in
|
@eendebakpt it's true because the difference is less than import math
x = 1e-320
rel_tol = 1e-9
abs_tol = math.nextafter(1, 0)
diff = abs(0 - x);
print(diff <= abs_tol) # returns True I think this statement in the documentation holds only for |
The relevant code is: Lines 3202 to 3211 in ad90c5f
@plashchynski I agree the statement is valid if f |
@eendebakpt imho this statements sounds more like a reason to set Lines 351 to 355 in ad90c5f
|
Btw, if import math
a = math.nextafter(0, 1)
print(math.isclose(a, 0, rel_tol=0.9)) # True because: print(math.nextafter(0, 1) * 0.9 == math.nextafter(0, 1)) # True which is contrary to this statement and to pure mathematics as well. Is there a policy for such corner cases? Should they be considered and handled? |
@eendebakpt, I think you misread documentation. It looks like For nonzero So, in your example: >>> import math
...
... x = 1e-320
... rel_tol = 1e-9
... abs_tol = math.nextafter(1, 0)
...
>>> math.isclose(x, 0, rel_tol=rel_tol, abs_tol=abs_tol)
True
>>> abs(x) <= max(rel_tol * abs(x), abs_tol)
True @hoodmane fix looks good for me. Though, I'm not sure we need such pedancy in docs. |
Corrected the statement about any *x* and *rel_tol* always producing ``False`` to exclude ``x = 0``
Corrected the statement about any *x* and *rel_tol* always producing ``False`` to exclude ``x = 0``.
math.isclose(0, x)
when x = 0
(cherry picked from commit 3f50f96) Co-authored-by: Guy Jacoby <49398101+guyjacoby@users.noreply.github.com>
(cherry picked from commit 3f50f96) Co-authored-by: Guy Jacoby <49398101+guyjacoby@users.noreply.github.com>
Thank you for the report and the fix! |
Documentation
In the documentation of math.isclose it states
The statement isn't correct since x = 0 will give True
Linked PRs
math.isclose
docs (GH-131139) #131392math.isclose
docs (GH-131139) #131393The text was updated successfully, but these errors were encountered: