Skip to content

Commit

Permalink
Backend/FiatValueEst: fix avg calc's edge case
Browse files Browse the repository at this point in the history
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] #278
  • Loading branch information
knocte committed May 19, 2024
1 parent 9b1ee11 commit 21770dd
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/GWallet.Backend/FiatValueEstimation.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 21770dd

Please sign in to comment.