-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrange.go
59 lines (49 loc) · 1.25 KB
/
range.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package aggro
import (
"errors"
"github.com/shopspring/decimal"
)
func rangeValueForPeriod(value *decimal.Decimal, period []interface{}) (decimal.Decimal, error) {
var rangeVal decimal.Decimal
// Range each of the intervals (periods), determining which band the value
// falls into.
for i, p := range period {
var nextPeriodValue *decimal.Decimal
var v float64
switch i := p.(type) {
case float64:
v = i
case float32:
v = float64(i)
case int32:
v = float64(i)
case int64:
v = float64(i)
case int:
v = float64(i)
default:
return decimal.NewFromFloat(0.0), errors.New("Invalid range values supplied - NaN")
}
periodVal := decimal.NewFromFloat(v)
// Find the i+1 value in our range (if length of range allows it).
if len(period) < i {
nxt := decimal.NewFromFloat(period[i+1].(float64))
nextPeriodValue = &nxt
}
// Value equals one of our range bands exactly, return it.
if (periodVal).Equals(*value) {
return periodVal, nil
}
// Value is > than our current range value, but < than the next.
if (periodVal).Cmp(*value) > -1 {
if nextPeriodValue != nil {
if (nextPeriodValue).Cmp(*value) < 0 {
return periodVal, nil
}
} else {
return periodVal, nil
}
}
}
return rangeVal, nil
}