diff --git a/CHANGELOG.rst b/CHANGELOG.rst index c56afdb..2b7f702 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -9,6 +9,9 @@ UNRELEASED * Fix division ``1.0 / a`` where ``a`` is a ``Scalar`` or ``Array`` and also add support for floor division, i.e., operations like ``a // b`` where ``a`` and ``b`` are ``Scalar`` or ``Array`` (and combinations with ``float`` or ``int``). +* Modified comparison behavior of ``Scalar``. The previous behavior assumes that ``Scalar(1, "m") != Scalar(100, "cm")`` + and not it has been changed to ``Scalar(1, "m") == Scalar(100, "cm")``. This may affect users that rely on the previous + behavior. 1.7.1 (2019-10-03) ------------------ diff --git a/src/barril/units/_quantity.py b/src/barril/units/_quantity.py index c754ea0..1d24f98 100644 --- a/src/barril/units/_quantity.py +++ b/src/barril/units/_quantity.py @@ -677,9 +677,17 @@ def Convert(self, value, to_unit): :returns: An object with values to the passed unit. """ - return self._unit_database.Convert( - self._composing_categories, self._composing_units, to_unit, value - ) + try: + return self._unit_database.Convert( + self._composing_categories, self._composing_units, to_unit, value + ) + except: + return self._unit_database.Convert( + self._category, + self._CreateUnitsWithJoinedExponentsString(), + to_unit, + value, + ) @classmethod def _GetComparison(cls, operator, use_literals=False): diff --git a/src/barril/units/_scalar.py b/src/barril/units/_scalar.py index 81fe445..0096023 100644 --- a/src/barril/units/_scalar.py +++ b/src/barril/units/_scalar.py @@ -242,19 +242,20 @@ def GetFormatted(self, unit=None, value_format=None): def __eq__(self, other): return ( type(self) is type(other) - and self._value == other.value - and self._quantity == other._quantity + and self._quantity._category == other._quantity._category + and self._value == other.GetValue(self.unit) ) def AlmostEqual(self, other, precision): return ( type(self) is type(other) - and round(self._value - other.value, precision) == 0 - and self._quantity == other._quantity + and self._quantity._category == other._quantity._category + and round(self._value - other.GetValue(self.unit), precision) == 0 ) def __hash__(self, *args, **kwargs): - return hash((self._value, self._quantity)) + default_unit = self._unit_database.GetDefaultUnit(self._quantity._category) + return hash((self.GetValue(default_unit), self._quantity._category)) def __lt__(self, other): if self.quantity_type != other.quantity_type: diff --git a/src/barril/units/_tests/test_scalar.py b/src/barril/units/_tests/test_scalar.py index 07f675e..0b6aefa 100644 --- a/src/barril/units/_tests/test_scalar.py +++ b/src/barril/units/_tests/test_scalar.py @@ -630,3 +630,24 @@ class Fluid: assert fluid.density.GetValue("lbm/galUS") == 10 assert fluid.concentration.GetValue("%") == 1.0 + + +def testComparison(): + a = Scalar(1, "m") + b = Scalar(100, "cm") + c = Scalar(99, "cm") + + assert a == b + assert a <= b + assert b >= a + assert c <= b + assert c <= a + + # Test set creation with scalars + assert {b, a, c} == {a, b, c} == {c, a, b} + + # Check Scalars with different categories + assert Scalar(99, "psi") != Scalar(100, "cm") + + assert a.AlmostEqual(b, precision=10) + assert a.AlmostEqual(c, precision=1)