From 96c6c168317796a95788f421730e89b32aaae755 Mon Sep 17 00:00:00 2001 From: "willem.romijn" Date: Wed, 9 Apr 2025 11:53:03 +0200 Subject: [PATCH 1/3] fix and tests for lost precision in unit conversion Signed-off-by: willem.romijn --- .../main/kotlin/com/alliander/open/measure/Measure.kt | 2 +- .../kotlin/com/alliander/open/measure/MeasureTest.kt | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/measure/src/main/kotlin/com/alliander/open/measure/Measure.kt b/measure/src/main/kotlin/com/alliander/open/measure/Measure.kt index 3147128..13da166 100644 --- a/measure/src/main/kotlin/com/alliander/open/measure/Measure.kt +++ b/measure/src/main/kotlin/com/alliander/open/measure/Measure.kt @@ -47,7 +47,7 @@ data class Measure(val amount: BigDecimal, val units: U) : Comparable infix fun `in`(other: A): BigDecimal = if (units == other) amount - else (amount * units.ratio).divide(other.ratio, amount.scale() + units.ratio.precision(), RoundingMode.UP) + else (amount * units.ratio).divide(other.ratio, amount.scale() + other.ratio.precision(), RoundingMode.UP) operator fun plus(other: Measure): Measure = baseUnits( units, diff --git a/measure/src/test/kotlin/com/alliander/open/measure/MeasureTest.kt b/measure/src/test/kotlin/com/alliander/open/measure/MeasureTest.kt index 28866db..457e467 100644 --- a/measure/src/test/kotlin/com/alliander/open/measure/MeasureTest.kt +++ b/measure/src/test/kotlin/com/alliander/open/measure/MeasureTest.kt @@ -218,4 +218,14 @@ class MeasureTest : StringSpec({ result shouldBe expectedResult } + + "Unit conversion uses the correct scale" { + val energyInJoule = 13500000 * joule + val valueInKwh = energyInJoule `in` kiloWattHour + valueInKwh.stripTrailingZeros() shouldBe BigDecimal.valueOf(3.75) + + val powerInW = 1.99E7 * watt + val valueInMw = powerInW `as` megaWatt + valueInMw.amount.stripTrailingZeros() shouldBe BigDecimal.valueOf(19.9) + } }) From 8fafdab3bbbd6d18b61f1e9223bf29eb466e4f48 Mon Sep 17 00:00:00 2001 From: "willem.romijn" Date: Thu, 8 May 2025 13:50:20 +0200 Subject: [PATCH 2/3] added test case --- .../src/test/kotlin/com/alliander/open/measure/MeasureTest.kt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/measure/src/test/kotlin/com/alliander/open/measure/MeasureTest.kt b/measure/src/test/kotlin/com/alliander/open/measure/MeasureTest.kt index 457e467..996feb0 100644 --- a/measure/src/test/kotlin/com/alliander/open/measure/MeasureTest.kt +++ b/measure/src/test/kotlin/com/alliander/open/measure/MeasureTest.kt @@ -224,6 +224,10 @@ class MeasureTest : StringSpec({ val valueInKwh = energyInJoule `in` kiloWattHour valueInKwh.stripTrailingZeros() shouldBe BigDecimal.valueOf(3.75) + val energyInJoule2 = 3.75 * kiloWattHour + val valueInKwh2 = energyInJoule2 `in` joule + valueInKwh2.stripTrailingZeros() shouldBe BigDecimal.valueOf(1.35E+7) + val powerInW = 1.99E7 * watt val valueInMw = powerInW `as` megaWatt valueInMw.amount.stripTrailingZeros() shouldBe BigDecimal.valueOf(19.9) From 23620c69086b2aeedff09b63698fd03a5d8be957 Mon Sep 17 00:00:00 2001 From: Coen van der List Date: Thu, 18 Dec 2025 10:01:30 +0100 Subject: [PATCH 3/3] Trigger Build