From 59cb6cf3273540fc2474c91555c273ebe8cc9b2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Robert?= Date: Sun, 2 Apr 2023 18:26:16 +0200 Subject: [PATCH 1/3] TST: add a broken test to expose bug #407 --- unyt/tests/test_units.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/unyt/tests/test_units.py b/unyt/tests/test_units.py index fd70b6d4..41322e6a 100644 --- a/unyt/tests/test_units.py +++ b/unyt/tests/test_units.py @@ -504,15 +504,25 @@ def test_base_equivalent(): assert u.get_base_equivalent(unit_system="mks") == Unit("A") -def test_temperature_offsets(): - u1 = Unit("degC") - u2 = Unit("degF") - +@pytest.mark.parametrize( + ("u1", "u2"), + [ + (Unit("degC"), Unit("degF")), + (Unit("degC"), Unit("K")), + (Unit("degF"), Unit("K")), + ], +) +def test_temperature_offsets(u1, u2): with pytest.raises(InvalidUnitOperation): operator.mul(u1, u2) with pytest.raises(InvalidUnitOperation): operator.truediv(u1, u2) + assert u1 != u2 + assert u2 != u1 + assert not u1 == u2 + assert not u2 == u1 + def test_latex_repr(): registry = UnitRegistry() From d66608affc2140d77b17ce619080625c8dc9a2b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Robert?= Date: Sun, 2 Apr 2023 18:33:45 +0200 Subject: [PATCH 2/3] BUG: fix unit comparison for K VS degC --- unyt/array.py | 6 +++++- unyt/unit_object.py | 25 +++++++++---------------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/unyt/array.py b/unyt/array.py index efc0f3da..34db5774 100644 --- a/unyt/array.py +++ b/unyt/array.py @@ -1884,7 +1884,11 @@ def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): conv, offset = u1.get_conversion_factor(u0, inp1.dtype) new_dtype = np.dtype("f" + str(inp1.dtype.itemsize)) conv = new_dtype.type(conv) - if offset is not None: + if ( + offset is not None + and u1.base_offset != 0.0 + and not repr(u0).startswith("delta_") + ): raise InvalidUnitOperation( "Quantities with units of Fahrenheit or Celsius " "cannot by multiplied, divided, subtracted or " diff --git a/unyt/unit_object.py b/unyt/unit_object.py index 2b13e46d..5b734ef2 100644 --- a/unyt/unit_object.py +++ b/unyt/unit_object.py @@ -480,25 +480,18 @@ def __pow__(self, p): def __eq__(self, u): """Test unit equality.""" - if not isinstance(u, Unit): - return False return ( - math.isclose(self.base_value, u.base_value) - and self.dimensions == u.dimensions + isinstance(u, Unit) + and math.isclose(self.base_value, u.base_value) + and math.isclose(self.base_offset, u.base_offset) + and ( + # use 'is' comparison dimensions to avoid expensive sympy operation + self.dimensions is u.dimensions + # fall back to expensive sympy comparison + or self.dimensions == u.dimensions + ) ) - def __ne__(self, u): - """Test unit inequality.""" - if not isinstance(u, Unit): - return True - if not math.isclose(self.base_value, u.base_value): - return True - # use 'is' comparison dimensions to avoid expensive sympy operation - if self.dimensions is u.dimensions: - return False - # fall back to expensive sympy comparison - return self.dimensions != u.dimensions - def copy(self, *, deep=False): expr = str(self.expr) base_value = copy.deepcopy(self.base_value) From d0a4017ba8ebdfa54a84f13445e8d48d4a4d6d11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Robert?= Date: Sat, 8 Apr 2023 19:05:19 +0200 Subject: [PATCH 3/3] DOC: fixup doctest (temperature sums and differences are now float typed) --- docs/usage.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/usage.rst b/docs/usage.rst index 35644609..cc4a097d 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -363,9 +363,9 @@ Performing arithmetic with temperature quantities can be ambiguous. To clarify i >>> t1 = 23*degC >>> t2 = 1*delta_degC >>> print(t1 + t2) - 24 °C + 24.0 °C >>> print(t2 - t1) - -22 °C + -22.0 °C >>> tempco = 10.0*V/delta_degC >>> print(tempco*2*delta_degC) 20.0 V