address stylistic nits

Signed-off-by: darshanime <deathbullet@gmail.com>
pull/9588/head
darshanime 3 years ago
parent a905354da3
commit 694b872dee

@ -387,7 +387,7 @@ func funcAvgOverTime(vals []parser.Value, args parser.Expressions, enh *EvalNode
continue continue
} }
} }
mean, c = kahanSummationIter(v.V/count-mean/count, mean, c) mean, c = kahanSumInc(v.V/count-mean/count, mean, c)
} }
if math.IsInf(mean, 0) { if math.IsInf(mean, 0) {
@ -445,7 +445,7 @@ func funcSumOverTime(vals []parser.Value, args parser.Expressions, enh *EvalNode
return aggrOverTime(vals, enh, func(values []Point) float64 { return aggrOverTime(vals, enh, func(values []Point) float64 {
var sum, c float64 var sum, c float64
for _, v := range values { for _, v := range values {
sum, c = kahanSummationIter(v.V, sum, c) sum, c = kahanSumInc(v.V, sum, c)
} }
if math.IsInf(sum, 0) { if math.IsInf(sum, 0) {
return sum return sum
@ -682,25 +682,24 @@ func funcTimestamp(vals []parser.Value, args parser.Expressions, enh *EvalNodeHe
return enh.Out return enh.Out
} }
func kahanSummation(samples []float64) float64 { func kahanSum(samples []float64) float64 {
sum, c := 0.0, 0.0 var sum, c float64
for _, v := range samples { for _, v := range samples {
sum, c = kahanSummationIter(v, sum, c) sum, c = kahanSumInc(v, sum, c)
} }
return sum + c return sum + c
} }
func kahanSummationIter(v, sum, c float64) (float64, float64) { func kahanSumInc(inc, sum, c float64) (newSum, newC float64) {
t := sum + v t := sum + inc
// using Neumaier improvement, swap if next term larger than sum // Using Neumaier improvement, swap if next term larger than sum.
if math.Abs(sum) >= math.Abs(v) { if math.Abs(sum) >= math.Abs(inc) {
c += (sum - t) + v c += (sum - t) + inc
} else { } else {
c += (v - t) + sum c += (inc - t) + sum
} }
sum = t return t, c
return sum, c
} }
// linearRegression performs a least-square linear regression analysis on the // linearRegression performs a least-square linear regression analysis on the
@ -717,10 +716,10 @@ func linearRegression(samples []Point, interceptTime int64) (slope, intercept fl
for _, sample := range samples { for _, sample := range samples {
n += 1.0 n += 1.0
x := float64(sample.T-interceptTime) / 1e3 x := float64(sample.T-interceptTime) / 1e3
sumX, cX = kahanSummationIter(x, sumX, cX) sumX, cX = kahanSumInc(x, sumX, cX)
sumY, cY = kahanSummationIter(sample.V, sumY, cY) sumY, cY = kahanSumInc(sample.V, sumY, cY)
sumXY, cXY = kahanSummationIter(x*sample.V, sumXY, cXY) sumXY, cXY = kahanSumInc(x*sample.V, sumXY, cXY)
sumX2, cX2 = kahanSummationIter(x*x, sumX2, cX2) sumX2, cX2 = kahanSumInc(x*x, sumX2, cX2)
} }
sumX = sumX + cX sumX = sumX + cX

Loading…
Cancel
Save