From 21770dd415c882ac2522171c86cd7153e6612f50 Mon Sep 17 00:00:00 2001 From: "Andres G. Aragoneses" Date: Sun, 19 May 2024 15:00:29 +0800 Subject: [PATCH] Backend/FiatValueEst: fix avg calc's edge case While working on the unit tests for PR 278 [1], I noticed that when I implemented this function in the stable branch to calculate the average between 3 values discarding the outlier, I missed the edge case when the distance between the highest and the lowest is the same. So, in this case, let's discard the intermediate (the intermediate will in the end be the average, but that is calculated later in the code). [1] https://github.com/nblockchain/geewallet/pull/278 --- src/GWallet.Backend/FiatValueEstimation.fs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/GWallet.Backend/FiatValueEstimation.fs b/src/GWallet.Backend/FiatValueEstimation.fs index b01ef34b1..5d1e235c8 100644 --- a/src/GWallet.Backend/FiatValueEstimation.fs +++ b/src/GWallet.Backend/FiatValueEstimation.fs @@ -1100,11 +1100,15 @@ module FiatValueEstimation = let intermediate = sorted.Item 1 let lower = Math.Min(first, last) + if (higher - intermediate) = (intermediate - lower) then + Some higher, Some lower + // choose the two that are closest - if (higher - intermediate) < (intermediate - lower) then - Some higher, Some intermediate else - Some lower, Some intermediate + if (higher - intermediate) < (intermediate - lower) then + Some higher, Some intermediate + else + Some lower, Some intermediate let result = match someUsdPrice, otherUsdPrice with