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)
TrueBut, in fact, you can treat both members of an equality comparison with >>> pytest.approx(1) == pytest.approx(1)
TrueBy comparison, and rather incongruously, >>> [1] == pytest.approx([1])
True
>>> pytest.approx([1]) == pytest.approx([1])
FalseWhy 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])
FalseA 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])
TrueBut 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)
TrueMy 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 twoapproxinstances to each other. For example, it's not clear whether the following should beTrueorFalse: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…