diff --git a/src/property_utils/properties/property.py b/src/property_utils/properties/property.py index 92d77f6..d3904e0 100644 --- a/src/property_utils/properties/property.py +++ b/src/property_utils/properties/property.py @@ -217,7 +217,7 @@ def __mul__(self, other) -> "Property": if isinstance(other, Property): _other = self._unit_preconversion(other) return Property( - self.value * _other.value, (self.unit * _other.unit).simplified() + self.value * _other.value, self._simplify_units(self.unit * _other.unit) ) raise PropertyBinaryOperationError( f"cannot multiply {self} with {other}; " @@ -268,7 +268,7 @@ def __truediv__(self, other) -> "Property": raise PropertyBinaryOperationError( f"cannot divide {self} with {other}; denominator's value is zero. " ) from None - return Property(value, (self.unit / _other.unit).simplified()) + return Property(value, self._simplify_units(self.unit / _other.unit)) raise PropertyBinaryOperationError( f"cannot divide {self} with {other}; " "denominator must be numeric or Property. " @@ -299,7 +299,7 @@ def __rtruediv__(self, other) -> "Property": raise PropertyBinaryOperationError( f"cannot divide {self} with {other}; denominator's value is zero. " ) from None - return Property(value, (other.unit / self.unit).simplified()) + return Property(value, self._simplify_units(other.unit / self.unit)) raise PropertyBinaryOperationError( f"cannot divide {self} with {other}; " "numerator must be numeric or Property. " @@ -727,3 +727,15 @@ def _simple_unit_preconversion(self, unit: MeasurementUnit) -> MeasurementUnit: multiplication or division with this property. """ return self._dimension_unit_preconversion(unit**1).unit + + def _simplify_units(self, unit: CompositeDimension) -> UnitDescriptor: + """ + Simplifies the composite dimension and returns NON_DIMENSIONAL if the simplified + composite does not have units. + """ + unit = unit.simplified() + + if unit.has_no_units(): + return NonDimensionalUnit.NON_DIMENSIONAL + + return unit diff --git a/src/property_utils/tests/properties/test_property.py b/src/property_utils/tests/properties/test_property.py index 079c1e5..93cc319 100644 --- a/src/property_utils/tests/properties/test_property.py +++ b/src/property_utils/tests/properties/test_property.py @@ -346,6 +346,10 @@ def test_with_exponentiated_property(self): def test_with_inverse_property(self): self.assert_result("10.4 ") + @args({"other": Property(2, Unit1.A ** (-1))}) + def test_produced_units_with_inverse_property(self): + self.assertEqual(self.result().unit, NonDimensionalUnit.NON_DIMENSIONAL) + @args({"other": Property(4, Unit2.B)}) def test_with_other_propperty(self): self.assert_result("20.8 A * B") @@ -499,6 +503,10 @@ def test_with_exponentiated_property(self): def test_with_same_property(self): self.assert_result("3.0 ") + @args({"other": Property(2, Unit1.A)}) + def test_produced_units_with_same_property(self): + self.assertEqual(self.result().unit, NonDimensionalUnit.NON_DIMENSIONAL) + @args({"other": Property(2, Unit2.B)}) def test_with_other_propperty(self): self.assert_result("3.0 A / B") @@ -639,6 +647,10 @@ def test_with_exponentiated_property(self): def test_with_same_property(self): self.assert_result("0.2 ") + @args({"other": Property(2, Unit1.A)}) + def test_produced_units_with_same_property(self): + self.assertEqual(self.result().unit, NonDimensionalUnit.NON_DIMENSIONAL) + @args({"other": Property(2, Unit2.B)}) def test_with_other_propperty(self): self.assert_result("0.2 B / A")