From a905354da35b371605c89a74bd7a8e8d566e5970 Mon Sep 17 00:00:00 2001 From: darshanime Date: Mon, 25 Oct 2021 18:02:25 +0530 Subject: [PATCH] use kahan for avg_over_time Signed-off-by: darshanime --- promql/functions.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/promql/functions.go b/promql/functions.go index 9982b92c7..e43cc85ab 100644 --- a/promql/functions.go +++ b/promql/functions.go @@ -367,7 +367,7 @@ func aggrOverTime(vals []parser.Value, enh *EvalNodeHelper, aggrFn func([]Point) // === avg_over_time(Matrix parser.ValueTypeMatrix) Vector === func funcAvgOverTime(vals []parser.Value, args parser.Expressions, enh *EvalNodeHelper) Vector { return aggrOverTime(vals, enh, func(values []Point) float64 { - var mean, count float64 + var mean, count, c float64 for _, v := range values { count++ if math.IsInf(mean, 0) { @@ -387,9 +387,13 @@ func funcAvgOverTime(vals []parser.Value, args parser.Expressions, enh *EvalNode continue } } - mean += v.V/count - mean/count + mean, c = kahanSummationIter(v.V/count-mean/count, mean, c) } - return mean + + if math.IsInf(mean, 0) { + return mean + } + return mean + c }) }