Round -> kt
This commit is contained in:
parent
946081fb0e
commit
6fa4c880fd
4 changed files with 36 additions and 47 deletions
|
@ -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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
32
core/src/main/java/info/nightscout/androidaps/utils/Round.kt
Normal file
32
core/src/main/java/info/nightscout/androidaps/utils/Round.kt
Normal file
|
@ -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
|
||||||
|
}
|
|
@ -232,8 +232,8 @@ public class DanaRv2Plugin extends AbstractDanaRPlugin {
|
||||||
int percentRate = Double.valueOf(absoluteRate / getBaseBasalRate() * 100).intValue();
|
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.
|
// 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 (absoluteRate < 0.10d) percentRate = 0;
|
||||||
if (percentRate < 100) percentRate = Round.ceilTo((double) percentRate, 10d).intValue();
|
if (percentRate < 100) percentRate = (int) Round.ceilTo((double) percentRate, 10d);
|
||||||
else percentRate = Round.floorTo((double) percentRate, 10d).intValue();
|
else percentRate = (int) Round.floorTo((double) percentRate, 10d);
|
||||||
if (percentRate > 500) // Special high temp 500/15min
|
if (percentRate > 500) // Special high temp 500/15min
|
||||||
percentRate = 500;
|
percentRate = 500;
|
||||||
aapsLogger.debug(LTag.PUMP, "setTempBasalAbsolute: Calculated percent rate: " + percentRate);
|
aapsLogger.debug(LTag.PUMP, "setTempBasalAbsolute: Calculated percent rate: " + percentRate);
|
||||||
|
|
|
@ -217,8 +217,8 @@ public class DanaRPlugin extends AbstractDanaRPlugin {
|
||||||
int percentRate = Double.valueOf(absoluteRate / getBaseBasalRate() * 100).intValue();
|
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.
|
// 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 (absoluteRate < 0.10d) percentRate = 0;
|
||||||
if (percentRate < 100) percentRate = Round.ceilTo((double) percentRate, 10d).intValue();
|
if (percentRate < 100) percentRate = (int) Round.ceilTo((double) percentRate, 10d);
|
||||||
else percentRate = Round.floorTo((double) percentRate, 10d).intValue();
|
else percentRate = (int) Round.floorTo((double) percentRate, 10d);
|
||||||
if (percentRate > getPumpDescription().getMaxTempPercent()) {
|
if (percentRate > getPumpDescription().getMaxTempPercent()) {
|
||||||
percentRate = getPumpDescription().getMaxTempPercent();
|
percentRate = getPumpDescription().getMaxTempPercent();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue