nightscout / Trio

MIT License
45 stars 125 forks source link

[bug] - Rounding of mmol/L manual BG values #267

Open bjornoleh opened 4 weeks ago

bjornoleh commented 4 weeks ago

Describe the bug

The manual BG values 8,0 and 8,1 are both displayed as 8,0 in the list of BG values.

Test of manual BG values (mmol/L) : 8,2 (stored as 147 mg/dL) 8,11 (stored as 146 mg/dL) 8,1 (stored as 145 mg/dL) 8 (stored as 144 mg/dL)

Displayed as follows in the app:

Below is the json of the above manual BG values:


  {
    "type" : "Manual",
    "date" : 1717050971595.7067776,
    "dateString" : "2024-05-30T06:36:11.596Z",
    "glucose" : 147,
    "_id" : "5621BF52-49D5-4C17-B86D-A9826F33FA77"
  },
  {
    "glucose" : 146,
    "type" : "Manual",
    "_id" : "99655471-7A3F-41BB-BBBB-9694A28CFB32",
    "date" : 1717050965423.120384,
    "dateString" : "2024-05-30T06:36:05.423Z"
  },
  {
    "type" : "Manual",
    "date" : 1717050955886.6382848,
    "_id" : "15FBE50B-EBFA-4F9B-BE0A-C48E2B71C158",
    "glucose" : 145,
    "dateString" : "2024-05-30T06:35:55.887Z"
  },
  {
    "glucose" : 144,
    "date" : 1717050950799.5430912,
    "_id" : "67E4B93F-F25A-42BA-BF1D-9EC4555C8D0D",
    "dateString" : "2024-05-30T06:35:50.800Z",
    "type" : "Manual"
  },

Trio Version:

Additional context

The BG values are not saved as mmol, but are converted to mgdL first.

It turns out 8,1 is stored as 145 mg/dL, although the correct conversion is 8,1 / 0,055 = 145,9459

Then the displayed value after converting back to mmol is no longer the same as the entered value of 8,1 mmol: 145 x 0,055 = 8,0475

The reason seems to be that the initial conversion to mgdL does not round the result correctly, and truncate the result to the integer value.

The relevant code is here:

https://github.com/nightscout/Trio/blob/14c7643275e3f351448d7623432f0d1778b65af4/FreeAPS/Sources/Models/BloodGlucose.swift#L52-L105

bjornoleh commented 4 weeks ago

@mountrcg noted that the conversion issue also affects BG target values:

I notice the behaviour too. If I change from mg -> mmol and back, my target settings will change from an even number to an odd number. very annoying.

Perhaps other parts of the code too (BG values in general, ISF settings?)

marionbarker commented 4 weeks ago

Please see: LoopKit PR 527: Fix issue where glucose values outside of guardrails could be selected.

Possibly adapt the code in the LoopKit/QuantityFormatter.swift for Trio:

func doubleValue(for unit: HKUnit, withRounding: Bool, usingFractionDigits fractionDigits: Int? = nil, rule: FloatingPointRoundingRule = .toNearestOrAwayFromZero)
mountrcg commented 1 week ago

I notice the behaviour too. If I change from mg -> mmol and back, my target settings will change from an even number to an odd number. very annoying.

Screenshots - note the original target = 100mg/dL mg/dL -> mmol/L change back
IMG_7347 IMG_7348 IMG_7349