Skip to content

Commit

Permalink
BUG: fix unit comparison for K VS degC
Browse files Browse the repository at this point in the history
  • Loading branch information
neutrinoceros committed Apr 8, 2023
1 parent 5707dd7 commit 861e6f0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
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_")
):
raise InvalidUnitOperation(
"Quantities with units of Fahrenheit or Celsius "
"cannot by multiplied, divided, subtracted or "
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)
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

0 comments on commit 861e6f0

Please sign in to comment.