Browse Source

Fix rounding for minZTUAMForecastGlucose

Sam King 6 months ago
parent
commit
da23c31378

+ 7 - 6
Trio/Sources/APS/OpenAPSSwift/Forecasts/ForecastGenerator.swift

@@ -305,17 +305,18 @@ enum ForecastGenerator {
         // 1. Calculate minZTUAMForecastGlucose ("minZTUAMPredBG" in JS)
         var minZTUAMForecastGlucose = uamResult.minForecastGlucose
         if ztResult.minGuardGlucose < threshold {
-            minZTUAMForecastGlucose = ((uamResult.minForecastGlucose + ztResult.minGuardGlucose) / 2)
-                .rounded()
+            minZTUAMForecastGlucose = (uamResult.minForecastGlucose + ztResult.minGuardGlucose) / 2
         } else if ztResult.minGuardGlucose < targetGlucose {
             let blendPct = (ztResult.minGuardGlucose - threshold) / (targetGlucose - threshold)
             let blendedMinZTGuardGlucose = uamResult.minForecastGlucose * blendPct + ztResult.minGuardGlucose * (1 - blendPct)
-            minZTUAMForecastGlucose = ((uamResult.minForecastGlucose + blendedMinZTGuardGlucose) / 2).rounded()
+            minZTUAMForecastGlucose = (uamResult.minForecastGlucose + blendedMinZTGuardGlucose) / 2
         } else if ztResult.minGuardGlucose > uamResult.minForecastGlucose {
-            minZTUAMForecastGlucose = ((uamResult.minForecastGlucose + ztResult.minGuardGlucose) / 2)
-                .rounded()
+            minZTUAMForecastGlucose = (uamResult.minForecastGlucose + ztResult.minGuardGlucose) / 2
         }
-        minZTUAMForecastGlucose = minZTUAMForecastGlucose.jsRounded()
+        // Note: We found at least one case where decmial weren't able
+        // to handle the precision of a calculation, so we'll do the
+        // double rounding trick we do in JS sometimes
+        minZTUAMForecastGlucose = minZTUAMForecastGlucose.jsRounded(scale: 6).jsRounded()
 
         // 2. avgForecastGlucose blending (like avgPredBG)
         let avgerageForecastGlucose: Decimal

+ 1 - 1
TrioTests/OpenAPSSwiftTests/DetermineBasalJsonTests.swift

@@ -103,7 +103,7 @@ import Testing
         // this test is meant for one-off analysis so it's ok to hard code
         // a file, just make sure to _not_ check in updates to this to
         // avoid polluting our change logs
-        let algorithmComparison = try await HttpFiles.downloadFile(at: "/files/d247c5ea-b2af-448b-88ce-7ecb3fc09e68.2.json")
+        let algorithmComparison = try await HttpFiles.downloadFile(at: "/files/240ecbe9-5903-46ca-a4af-a4a0aebbf8a0.4.json")
         let determineBasalInput = algorithmComparison.determineBasalInput!
 
         let encoder = JSONCoding.encoder