/
func_num.go
152 lines (125 loc) · 2.55 KB
/
func_num.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package expr
import (
"math"
"math/rand"
"github.com/PaesslerAG/gval"
"github.com/cortezaproject/corteza/server/pkg/gvalfnc"
"github.com/pkg/errors"
"github.com/spf13/cast"
)
func NumericFunctions() []gval.Language {
return []gval.Language{
gval.Function("min", min),
gval.Function("max", max),
gval.Function("round", round),
gval.Function("floor", floor),
gval.Function("ceil", ceil),
gval.Function("abs", math.Abs),
gval.Function("log", math.Log10),
gval.Function("pow", math.Pow),
gval.Function("sqrt", math.Sqrt),
gval.Function("sum", sum),
gval.Function("average", average),
gval.Function("random", random),
gval.Function("int", toInt64),
gval.Function("float", gvalfnc.CastFloat),
}
}
func min(aa ...interface{}) (min float64) {
return findMinMax(-1, aa...)
}
func max(aa ...interface{}) (min float64) {
return findMinMax(1, aa...)
}
func findMinMax(dir int, aa ...interface{}) (mm float64) {
var (
set bool
flt float64
)
for i := range aa {
switch conv := aa[i].(type) {
case int:
flt = float64(conv)
case int64:
flt = float64(conv)
case uint:
flt = float64(conv)
case uint64:
flt = float64(conv)
case float32:
flt = float64(conv)
case float64:
flt = conv
default:
continue
}
if !set {
set = true
mm = flt
} else if dir < 0 {
mm = math.Min(mm, flt)
} else if dir > 0 {
mm = math.Max(mm, flt)
}
}
return mm
}
func round(f float64, d float64) float64 {
p := math.Pow(10, d)
return math.Round(f*p) / p
}
func floor(f float64) float64 {
return math.Floor(f)
}
func ceil(f float64) float64 {
return math.Ceil(f)
}
func sum(v ...interface{}) float64 {
var sum float64
for _, vv := range v {
c, err := CastToFloat(vv)
if err != nil {
continue
}
sum += c
}
return sum
}
func average(v ...interface{}) float64 {
var i int
var j, sum float64
for ; i < len(v); i++ {
c, err := CastToFloat(v[i])
if err != nil {
continue
}
j++
sum += c
}
return sum / j
}
func random(v ...float64) (out float64, err error) {
var (
from, to float64
totalArgs = len(v)
)
if totalArgs == 0 || totalArgs > 2 {
return 0, errors.Errorf("expecting 1 or 2 parameter, got %d", totalArgs)
}
if totalArgs > 0 {
if to = v[0]; to < 0 {
return 0, errors.New("unexpected input type of 1st parameter")
}
}
if totalArgs > 1 {
if to = v[1]; to < 0 {
return 0, errors.New("unexpected input type of 2nd parameter")
}
from = v[0]
}
out = from + rand.Float64()*(to-from)
return
}
func toInt64(aa interface{}) (i int64) {
return cast.ToInt64(aa)
}