From 02c8ea2ca55bc052ec473ff57f5c773bf62669da Mon Sep 17 00:00:00 2001 From: Zhuo Zhao <193034689+chaduo@users.noreply.github.com> Date: Tue, 13 Oct 2026 20:50:41 +0800 Subject: [PATCH] Fix HalfVector equality for signed zero --- pgvector/halfvec.py | 2 +- tests/test_half_vector.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/pgvector/halfvec.py b/pgvector/halfvec.py index 1a2c8c7..fa35194 100644 --- a/pgvector/halfvec.py +++ b/pgvector/halfvec.py @@ -37,7 +37,7 @@ def __repr__(self) -> str: def __eq__(self, other: object, /) -> bool: if not isinstance(other, self.__class__): return NotImplemented - return self._value == other._value + return self.to_list() == other.to_list() def dimensions(self) -> int: return len(self._value) diff --git a/tests/test_half_vector.py b/tests/test_half_vector.py index 26c5ac7..4847501 100644 --- a/tests/test_half_vector.py +++ b/tests/test_half_vector.py @@ -56,6 +56,27 @@ def test_equality(self) -> None: assert HalfVector([1, 2, 3]) != HalfVector([1, 2, 4]) assert HalfVector([1, 2, 3]) != 1 + @pytest.mark.parametrize('positive, negative', [ + ([0.0], [-0.0]), + ([0.0, 1.5, -2.0], [-0.0, 1.5, -2.0]), + ([1e-8, 1.5], [-1e-8, 1.5]), + ]) + def test_equality_signed_zero(self, positive: list[float], negative: list[float]) -> None: + left = HalfVector(positive) + right = HalfVector(negative) + assert left == right + assert right == left + # Numeric equality must not normalize the stored sign bit. + assert left.to_binary() != right.to_binary() + + def test_equality_signed_zero_from_binary(self) -> None: + left = HalfVector.from_binary(pack('>HH2e', 2, 0, 0.0, 1.5)) + right = HalfVector.from_binary(pack('>HH2e', 2, 0, -0.0, 1.5)) + assert left == right + + def test_equality_signed_zero_from_text(self) -> None: + assert HalfVector.from_text('[0.0,1.5]') == HalfVector.from_text('[-0.0,1.5]') + def test_dimensions(self) -> None: assert HalfVector([1, 2, 3]).dimensions() == 3