Skip to content

Commit b41882c

Browse files
author
Igor Makhlin
authored
Merge pull request #53 from imakhlin/replace-nan-with-infinity
Replace nan with infinity
2 parents 0ca6efb + bce28f4 commit b41882c

File tree

4 files changed

+25
-13
lines changed

4 files changed

+25
-13
lines changed

pkg/aggregate/aggregate.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,13 @@ func NewAggregatorList(aggrType AggrType) *AggregatorList {
148148
list = append(list, &SqrAggregator{FloatAggregator{attr: "sqr"}})
149149
}
150150
if (aggrType & aggrTypeMin) != 0 {
151-
list = append(list, &MinAggregator{FloatAggregator{attr: "min", val: math.MaxFloat64}}) // TODO: use math.Inf(1)
151+
list = append(list, &MinAggregator{FloatAggregator{attr: "min", val: math.Inf(1)}})
152152
}
153153
if (aggrType & aggrTypeMax) != 0 {
154-
list = append(list, &MaxAggregator{FloatAggregator{attr: "max", val: -math.MaxFloat64}}) // TODO: use math.Inf(-1)
154+
list = append(list, &MaxAggregator{FloatAggregator{attr: "max", val: math.Inf(-1)}})
155155
}
156156
if (aggrType & aggrTypeLast) != 0 {
157-
list = append(list, &LastAggregator{FloatAggregator{attr: "last"}, 0})
157+
list = append(list, &LastAggregator{FloatAggregator{attr: "last", val: math.Inf(-1)}, 0})
158158
}
159159
return &list
160160
}

pkg/aggregate/functions.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@ func (a *FloatAggregator) UpdateExpr(col string, bucket int) string {
7575
}
7676

7777
func (a *FloatAggregator) InitExpr(col string, buckets int) string {
78-
return fmt.Sprintf("_%s_%s=init_array(%d,'double');", col, a.attr, buckets)
78+
return fmt.Sprintf("_%s_%s=init_array(%d,'double',%f);", col, a.attr, buckets, a.val)
7979
}
8080

8181
// Sum Aggregator
8282
type SumAggregator struct{ FloatAggregator }
8383

8484
func (a *SumAggregator) Aggregate(t int64, v float64) {
85-
if !math.IsNaN(v) {
85+
if utils.IsDefined(v) {
8686
a.val += v
8787
}
8888
}
@@ -91,18 +91,18 @@ func (a *SumAggregator) Aggregate(t int64, v float64) {
9191
type SqrAggregator struct{ FloatAggregator }
9292

9393
func (a *SqrAggregator) Aggregate(t int64, v float64) {
94-
if !math.IsNaN(v) {
94+
if utils.IsDefined(v) {
9595
a.val += v * v
9696
}
9797
}
9898

9999
// Minimum Aggregator
100100
type MinAggregator struct{ FloatAggregator }
101101

102-
func (a *MinAggregator) Clear() { a.val = math.MaxFloat64 } // TODO: use math.Inf(1)
102+
func (a *MinAggregator) Clear() { a.val = math.Inf(1) }
103103

104104
func (a *MinAggregator) Aggregate(t int64, v float64) {
105-
if !math.IsNaN(v) && (math.IsNaN(a.val) || v < a.val) {
105+
if v < a.val {
106106
a.val = v
107107
}
108108
}
@@ -114,10 +114,10 @@ func (a *MinAggregator) UpdateExpr(col string, bucket int) string {
114114
// Maximum Aggregator
115115
type MaxAggregator struct{ FloatAggregator }
116116

117-
func (a *MaxAggregator) Clear() { a.val = -math.MaxFloat64 } // TODO: use math.Inf(-1)
117+
func (a *MaxAggregator) Clear() { a.val = math.Inf(-1) }
118118

119119
func (a *MaxAggregator) Aggregate(t int64, v float64) {
120-
if !math.IsNaN(v) && (math.IsNaN(a.val) || v > a.val) {
120+
if v > a.val {
121121
a.val = v
122122
}
123123
}
@@ -132,7 +132,7 @@ type LastAggregator struct {
132132
lastT int64
133133
}
134134

135-
func (a *LastAggregator) Clear() { a.val = -math.MaxFloat64 } // TODO: use math.Inf(1)
135+
func (a *LastAggregator) Clear() { a.val = math.Inf(-1) }
136136

137137
func (a *LastAggregator) Aggregate(t int64, v float64) {
138138
if t > a.lastT {
@@ -142,7 +142,7 @@ func (a *LastAggregator) Aggregate(t int64, v float64) {
142142
}
143143

144144
func (a *LastAggregator) UpdateExpr(col string, bucket int) string {
145-
if math.IsNaN(a.val) {
145+
if utils.IsUndefined(a.val) {
146146
return ""
147147
}
148148

pkg/tsdb/tsdbtest/tsdbtest.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func ValidateCountOfSamples(t testing.TB, adapter *V3ioAdapter, metricName strin
143143
}
144144

145145
if expected != actual {
146-
t.Fatalf("Check failed: actual result is not as expected (%d != %d)", expected, actual)
146+
t.Fatalf("Check failed: actual result is not as expected [%d(actual) != %d(expected)]", actual, expected)
147147
}
148148
}
149149

pkg/utils/misc.go

+12
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,21 @@ package utils
22

33
import (
44
"fmt"
5+
"math"
56
"strings"
67
)
78

9+
func IsUndefined(value float64) bool {
10+
return math.IsNaN(value) || math.IsInf(value, -1) || math.IsInf(value, 1)
11+
}
12+
13+
func IsDefined(value float64) bool {
14+
return !IsUndefined(value)
15+
}
16+
817
func FloatToNormalizedScientificStr(val float64) string {
18+
if IsUndefined(val) {
19+
return fmt.Sprintf("%f", val)
20+
}
921
return strings.Replace(fmt.Sprintf("%e", val), "+", "", 1)
1022
}

0 commit comments

Comments
 (0)