From 6fa4c880fd28c85692b1e3f73e6e99edcce609ca Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 13 Dec 2021 17:42:54 +0100 Subject: [PATCH] Round -> kt --- .../nightscout/androidaps/utils/Round.java | 43 ------------------- .../info/nightscout/androidaps/utils/Round.kt | 32 ++++++++++++++ .../androidaps/danaRv2/DanaRv2Plugin.java | 4 +- .../androidaps/danar/DanaRPlugin.java | 4 +- 4 files changed, 36 insertions(+), 47 deletions(-) delete mode 100644 core/src/main/java/info/nightscout/androidaps/utils/Round.java create mode 100644 core/src/main/java/info/nightscout/androidaps/utils/Round.kt diff --git a/core/src/main/java/info/nightscout/androidaps/utils/Round.java b/core/src/main/java/info/nightscout/androidaps/utils/Round.java deleted file mode 100644 index bc3c5d8b9c..0000000000 --- a/core/src/main/java/info/nightscout/androidaps/utils/Round.java +++ /dev/null @@ -1,43 +0,0 @@ -package info.nightscout.androidaps.utils; - -import java.math.BigDecimal; - -/** - * Created by mike on 20.06.2016. - */ -public class Round { - public static Double roundTo(double x, Double step) { - if (x == 0d) { - return 0d; - } - - //Double oldCalc = Math.round(x / step) * step; - Double newCalc = BigDecimal.valueOf(Math.round(x / step)).multiply(BigDecimal.valueOf(step)).doubleValue(); - - // just for the tests, forcing failures - //newCalc = oldCalc; - - return newCalc; - } - - public static Double floorTo(Double x, Double step) { - if (x != 0d) { - return Math.floor(x / step) * step; - } - return 0d; - } - - public static Double ceilTo(Double x, Double step) { - if (x != 0d) { - return Math.ceil(x / step) * step; - } - return 0d; - } - - public static boolean isSame(Double d1, Double d2) { - double diff = d1 - d2; - - return (Math.abs(diff) <= 0.000001); - } - -} diff --git a/core/src/main/java/info/nightscout/androidaps/utils/Round.kt b/core/src/main/java/info/nightscout/androidaps/utils/Round.kt new file mode 100644 index 0000000000..f1d47b8e06 --- /dev/null +++ b/core/src/main/java/info/nightscout/androidaps/utils/Round.kt @@ -0,0 +1,32 @@ +package info.nightscout.androidaps.utils + +import java.math.BigDecimal +import kotlin.math.abs +import kotlin.math.ceil +import kotlin.math.floor +import kotlin.math.roundToLong + +/** + * Created by mike on 20.06.2016. + */ +object Round { + + @JvmStatic + fun roundTo(x: Double, step: Double): Double = + if (x == 0.0) 0.0 + else BigDecimal.valueOf((x / step).roundToLong()).multiply(BigDecimal.valueOf(step)).toDouble() + + @JvmStatic + fun floorTo(x: Double, step: Double): Double = + if (x != 0.0) floor(x / step) * step + else 0.0 + + @JvmStatic + fun ceilTo(x: Double, step: Double): Double = + if (x != 0.0) ceil(x / step) * step + else 0.0 + + @JvmStatic + fun isSame(d1: Double, d2: Double): Boolean = + abs(d1 - d2) <= 0.000001 +} \ No newline at end of file diff --git a/danar/src/main/java/info/nightscout/androidaps/danaRv2/DanaRv2Plugin.java b/danar/src/main/java/info/nightscout/androidaps/danaRv2/DanaRv2Plugin.java index 11b7c69a52..d9c8602f12 100644 --- a/danar/src/main/java/info/nightscout/androidaps/danaRv2/DanaRv2Plugin.java +++ b/danar/src/main/java/info/nightscout/androidaps/danaRv2/DanaRv2Plugin.java @@ -232,8 +232,8 @@ public class DanaRv2Plugin extends AbstractDanaRPlugin { int percentRate = Double.valueOf(absoluteRate / getBaseBasalRate() * 100).intValue(); // Any basal less than 0.10u/h will be dumped once per hour, not every 4 minutes. So if it's less than .10u/h, set a zero temp. if (absoluteRate < 0.10d) percentRate = 0; - if (percentRate < 100) percentRate = Round.ceilTo((double) percentRate, 10d).intValue(); - else percentRate = Round.floorTo((double) percentRate, 10d).intValue(); + if (percentRate < 100) percentRate = (int) Round.ceilTo((double) percentRate, 10d); + else percentRate = (int) Round.floorTo((double) percentRate, 10d); if (percentRate > 500) // Special high temp 500/15min percentRate = 500; aapsLogger.debug(LTag.PUMP, "setTempBasalAbsolute: Calculated percent rate: " + percentRate); diff --git a/danar/src/main/java/info/nightscout/androidaps/danar/DanaRPlugin.java b/danar/src/main/java/info/nightscout/androidaps/danar/DanaRPlugin.java index 5e40de5801..1a2844ba58 100644 --- a/danar/src/main/java/info/nightscout/androidaps/danar/DanaRPlugin.java +++ b/danar/src/main/java/info/nightscout/androidaps/danar/DanaRPlugin.java @@ -217,8 +217,8 @@ public class DanaRPlugin extends AbstractDanaRPlugin { int percentRate = Double.valueOf(absoluteRate / getBaseBasalRate() * 100).intValue(); // Any basal less than 0.10u/h will be dumped once per hour, not every 4 minutes. So if it's less than .10u/h, set a zero temp. if (absoluteRate < 0.10d) percentRate = 0; - if (percentRate < 100) percentRate = Round.ceilTo((double) percentRate, 10d).intValue(); - else percentRate = Round.floorTo((double) percentRate, 10d).intValue(); + if (percentRate < 100) percentRate = (int) Round.ceilTo((double) percentRate, 10d); + else percentRate = (int) Round.floorTo((double) percentRate, 10d); if (percentRate > getPumpDescription().getMaxTempPercent()) { percentRate = getPumpDescription().getMaxTempPercent(); }