Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: fix unit comparison for K VS degC #408

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion unyt/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure this last conditional will always be True because delta_degF and delta_degC have base_offset=0.0.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the second condition in on u1 while this one is on u0. I don't like it very much, I think I reached the point where I was more or less typing random conditions until I got one that passed tests, but I can guarantee that it's needed :)

):
raise InvalidUnitOperation(
"Quantities with units of Fahrenheit or Celsius "
"cannot by multiplied, divided, subtracted or "
Expand Down
18 changes: 14 additions & 4 deletions unyt/tests/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
25 changes: 9 additions & 16 deletions unyt/unit_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
ngoldbaum marked this conversation as resolved.
Show resolved Hide resolved
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)
Expand Down