From bb48aa9f23d3b74aa3e31f395deaf6fdd1618391 Mon Sep 17 00:00:00 2001 From: Will Hawkins Date: Thu, 16 Dec 2021 00:38:05 -0500 Subject: Bugfix: Incorrect AllSequentialIncreasesLessThan (nee IncreasesLessThan) Calculation In AllSequentialIncreasesLessThan (nee IncreasesLessThan), an earlier version of this function did not properly take into account the order in which values were added to the moving average when calculating the percent change. This patch fixes that error and adds an additional criteria for the function to return `true`: there must be *at least* `intervals` collected samples. --- ma/ma.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'ma') diff --git a/ma/ma.go b/ma/ma.go index b591d19..8d06a67 100644 --- a/ma/ma.go +++ b/ma/ma.go @@ -32,10 +32,18 @@ func (ma *MovingAverage) CalculateAverage() float64 { return float64(total) / float64(ma.divisor.Value()) } -func (ma *MovingAverage) IncreasesLessThan(limit float64) bool { - previous := ma.instants[0] +func (ma *MovingAverage) AllSequentialIncreasesLessThan(limit float64) bool { + + // If we have not yet accumulated a complete set of intervals, + // this is false. + if ma.divisor.Value() != ma.intervals { + return false + } + + previous := ma.instants[ma.index] for i := 1; i < ma.intervals; i++ { - current := ma.instants[i] + currentIndex := (ma.index + i) % ma.intervals + current := ma.instants[currentIndex] percentChange := utilities.SignedPercentDifference(current, previous) previous = current if percentChange > limit { -- cgit v1.2.3