From 347cc501195802939189e48577b2214264af2808 Mon Sep 17 00:00:00 2001 From: Alexandre Dufour <> Date: Mon, 17 Sep 2018 21:07:40 +0200 Subject: [PATCH] QuantityDimension: Fixed an issue where dividing by a powered or product dimension would return a non-canonical dimension --- .../indriya/quantity/QuantityDimension.java | 5 +++-- .../quantity/QuantityDimensionTest.java | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/main/java/tech/units/indriya/quantity/QuantityDimension.java b/src/main/java/tech/units/indriya/quantity/QuantityDimension.java index 37c192fe..c675a72d 100644 --- a/src/main/java/tech/units/indriya/quantity/QuantityDimension.java +++ b/src/main/java/tech/units/indriya/quantity/QuantityDimension.java @@ -35,6 +35,7 @@ import tech.units.indriya.AbstractUnit; import tech.units.indriya.unit.BaseUnit; +import tech.units.indriya.unit.ProductUnit; import tech.units.indriya.unit.Units; import java.io.Serializable; @@ -220,7 +221,7 @@ public QuantityDimension multiply(QuantityDimension that) { * @since 1.0 */ public Dimension divide(Dimension that) { - return this.multiply(that.pow(-1)); + return that instanceof QuantityDimension ? this.divide((QuantityDimension) that) : this.divide(that); } /** @@ -232,7 +233,7 @@ public Dimension divide(Dimension that) { * @since 1.0 */ public QuantityDimension divide(QuantityDimension that) { - return this.multiply(that.pow(-1)); + return new QuantityDimension(ProductUnit.ofQuotient(pseudoUnit, that.pseudoUnit)); } /** diff --git a/src/test/java/tech/units/indriya/quantity/QuantityDimensionTest.java b/src/test/java/tech/units/indriya/quantity/QuantityDimensionTest.java index 8f1eb994..8db9a0c0 100644 --- a/src/test/java/tech/units/indriya/quantity/QuantityDimensionTest.java +++ b/src/test/java/tech/units/indriya/quantity/QuantityDimensionTest.java @@ -95,6 +95,28 @@ public void divisionIsDoneCorrectlyInOverloadedQuantityDimensionMethod() { assertEquals("[L]/[M]", result.toString()); } + /** + * Verifies that the division is done correctly on powered units. Relies on the toString method to + * verify the result. + */ + @Test + public void divisionIsDoneCorrectlyOnPoweredBaseUnits() { + Dimension m2 = QuantityDimension.MASS.pow(2); + Dimension result = QuantityDimension.LENGTH.divide(m2); + assertEquals("[L]/[M]²", result.toString()); + } + + /** + * Verifies that the division is done correctly on powered units. Relies on the toString method to + * verify the result. + */ + @Test + public void divisionIsDoneCorrectlyOnPoweredProductUnits() { + Dimension ml = QuantityDimension.MASS.multiply(QuantityDimension.LENGTH); + Dimension result = QuantityDimension.LENGTH.divide(ml); + assertEquals("1/[M]", result.toString()); + } + /** * Verifies that raising to a power is done correctly by checking that multiplication with itself is the same as raising to the power of two. */