From 3a908d8e088763e6df7f7a5f74345ada30c9b0bb Mon Sep 17 00:00:00 2001 From: beorn7 Date: Thu, 4 Jul 2024 18:36:32 +0200 Subject: [PATCH] promql: Improve Kahan usage in avg_over_time The calculation of the mean value in avg_over_time is performed in an incremental fashion. This introduces additional numerical errors that even Kahan summation cannot compensate, but at least we can use the Kahan-corrected mean value when we use the intermediate mean value in the calculation. Signed-off-by: beorn7 --- promql/functions.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/promql/functions.go b/promql/functions.go index dcc2cd759..575f8302d 100644 --- a/promql/functions.go +++ b/promql/functions.go @@ -593,7 +593,8 @@ func funcAvgOverTime(vals []parser.Value, args parser.Expressions, enh *EvalNode continue } } - mean, c = kahanSumInc(f.F/count-mean/count, mean, c) + correctedMean := mean + c + mean, c = kahanSumInc(f.F/count-correctedMean/count, mean, c) } if math.IsInf(mean, 0) {