gh-71385: add relative delta parameter for assertAlmostEqual and assertNotAlmostEqual#96881
gh-71385: add relative delta parameter for assertAlmostEqual and assertNotAlmostEqual#96881wehlgrundspitze wants to merge 11 commits intopython:mainfrom
Conversation
|
Most changes to Python require a NEWS entry. Please add it using the blurb_it web app or the blurb command-line tool. |
|
|
||
| 3) Comparing that the relative difference between the two | ||
| objects is more than the given rel_delta. The relative | ||
| difference is applied with respect to the first object, |
There was a problem hiding this comment.
Is there any good reason to apply the difference with respect only to first object?
math.isclose() is symmetrical and I think having different behavior here would be potentially confusing.
| safe_repr(delta), | ||
| safe_repr(diff)) | ||
| elif rel_delta is not None: | ||
| if diff < rel_delta*abs(first): |
There was a problem hiding this comment.
| if diff < rel_delta*abs(first): | |
| if math.isclose(first,second,rel_tol=rel_delta, abs_tol=0.0): |
import math is necessary at the beginning of the file if you want to inlcude this suggestion
| first_dt = datetime.timedelta(seconds=20) | ||
| second_dt = datetime.timedelta(seconds=15) | ||
|
|
||
| self.assertAlmostEqual(first_dt, second_dt, rel_delta=0.5) |
There was a problem hiding this comment.
Let's add two datetime objects together with timedelta as rel_delta
There was a problem hiding this comment.
From my point of view this seems not well defined. The clearest method to see are the units: Within the calculation, we multiply the relative delta with one of the objects: timedelta * datetime would have a unit of square seconds ...
|
@wehlgrundspitze your PR title should be: |
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
….TestCase.assertAlmostEqual
|
Thanks for your comments and feedback. I implemented them all and hope for acceptance. |
|
It seems to me that both existing and modified code don't have defined behavior when one (or both) input are either float('nan') or float('inf'). Especially assertNotAlmostEqual will fail when both of it's arguments are NaNs. This is different than math.isclose() and potentially confusing. https://docs.python.org/3/library/math.html#math.isclose Either we fix it or at least add a note in docstring. |
… to handle +-inf and NaN according to IEEE 754 standard
…ssert(Not)AlmostEqual
|
That was a great remark. I fixed the algorithm to handle +-inf and NaN according to IEEE 754 (and therefore consistent with math.isclose) and wrote an additional test about it. |
|
This PR is stale because it has been open for 30 days with no activity. |
relative delta keyword for assertAlmostEqual and assertNotAlmostEqual #71385
gh-71385: a keyword "rel_delta" is introduced to the functions assertAlmostEqual and assertNotAlmostEqual to allow comparisons based on the relative difference of two values
more detailed history in the corresponding issue #71385