Cannot compare equality of two ApproxSequencelike objects #8866
-
Question summaryI notice a slight incongruity between the behaviour of Happy to helpIf a change in the behaviour of DetailsOrdinarily, one would only need to have one member of an equality comparison be treated with >>> 1 == pytest.approx(1)
True But, in fact, you can treat both members of an equality comparison with >>> pytest.approx(1) == pytest.approx(1)
True By comparison, and rather incongruously, >>> [1] == pytest.approx([1])
True
>>> pytest.approx([1]) == pytest.approx([1])
False Why care?One wouldn't normally need to bother about this, but I stumbled on it when trying to apply >>> [np.arcsin(np.sin(1))] == [np.arccos(np.cos(1))] == [1]
False
>>> [np.arcsin(np.sin(1))] == pytest.approx([np.arccos(np.cos(1))]) == pytest.approx([1])
False A workaround is to apply >>> [np.arcsin(np.sin(1))] == pytest.approx([np.arccos(np.cos(1))]) == [1]
True
>>> pytest.approx([np.arcsin(np.sin(1))]) == [np.arccos(np.cos(1))] == pytest.approx([1])
True But that feels a bit quirky, and I can't find it documented anywhere for new users of |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
# The left operand is approximately close to 11, but the
# right operand is not approximately close to 10...
approx(10, abs=2) == approx(11, abs=0.2) If you actually try making this comparison, you'll find that the result depends on the order of the operands: >>> approx(10, abs=2) == approx(11, abs=0.2)
False
>>> approx(11, abs=0.2) == approx(10, abs=2)
True My opinion is that comparisons like this should probably be errors. Even if they aren't (yet), I would avoid them. Instead, I would write the example you gave as follows: assert [np.arcsin(np.sin(1))] == pytest.approx([1])
assert [np.arccos(np.cos(1))] == pytest.approx([1]) |
Beta Was this translation helpful? Give feedback.
approx()
is meant to be thought of as the reference value in a comparison, so I don't think it really makes sense to compare twoapprox
instances to each other. For example, it's not clear whether the following should beTrue
orFalse
:If you actually try making this comparison, you'll find that the result depends on the order of the operands:
My opinion is that comparisons like this should probably be errors. Even if they aren't (yet), I wo…