-
-
Notifications
You must be signed in to change notification settings - Fork 190
/
math.go
236 lines (211 loc) · 4.46 KB
/
math.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package funcs
import (
"fmt"
gmath "math"
"strconv"
"sync"
"github.com/hairyhenderson/gomplate/conv"
"github.com/hairyhenderson/gomplate/math"
)
var (
mathNS *MathFuncs
mathNSInit sync.Once
)
// MathNS - the math namespace
func MathNS() *MathFuncs {
mathNSInit.Do(func() { mathNS = &MathFuncs{} })
return mathNS
}
// AddMathFuncs -
func AddMathFuncs(f map[string]interface{}) {
f["math"] = MathNS
f["add"] = MathNS().Add
f["sub"] = MathNS().Sub
f["mul"] = MathNS().Mul
f["div"] = MathNS().Div
f["rem"] = MathNS().Rem
f["pow"] = MathNS().Pow
f["seq"] = MathNS().Seq
}
// MathFuncs -
type MathFuncs struct{}
// IsInt -
func (f *MathFuncs) IsInt(n interface{}) bool {
switch i := n.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return true
case string:
_, err := strconv.ParseInt(i, 0, 64)
return err == nil
}
return false
}
// IsFloat -
func (f *MathFuncs) IsFloat(n interface{}) bool {
switch i := n.(type) {
case float32, float64:
return true
case string:
_, err := strconv.ParseFloat(i, 64)
if err != nil {
return false
}
if f.IsInt(i) {
return false
}
return true
}
return false
}
func (f *MathFuncs) containsFloat(n ...interface{}) bool {
c := false
for _, v := range n {
if f.IsFloat(v) {
return true
}
}
return c
}
// IsNum -
func (f *MathFuncs) IsNum(n interface{}) bool {
return f.IsInt(n) || f.IsFloat(n)
}
// Abs -
func (f *MathFuncs) Abs(n interface{}) interface{} {
m := gmath.Abs(conv.ToFloat64(n))
if f.IsInt(n) {
return conv.ToInt64(m)
}
return m
}
// Add -
func (f *MathFuncs) Add(n ...interface{}) interface{} {
if f.containsFloat(n...) {
nums := conv.ToFloat64s(n...)
var x float64
for _, v := range nums {
x += v
}
return x
}
nums := conv.ToInt64s(n...)
var x int64
for _, v := range nums {
x += v
}
return x
}
// Mul -
func (f *MathFuncs) Mul(n ...interface{}) interface{} {
if f.containsFloat(n...) {
nums := conv.ToFloat64s(n...)
x := 1.
for _, v := range nums {
x *= v
}
return x
}
nums := conv.ToInt64s(n...)
x := int64(1)
for _, v := range nums {
x *= v
}
return x
}
// Sub -
func (f *MathFuncs) Sub(a, b interface{}) interface{} {
if f.containsFloat(a, b) {
return conv.ToFloat64(a) - conv.ToFloat64(b)
}
return conv.ToInt64(a) - conv.ToInt64(b)
}
// Div -
func (f *MathFuncs) Div(a, b interface{}) (interface{}, error) {
divisor := conv.ToFloat64(a)
dividend := conv.ToFloat64(b)
if dividend == 0 {
return 0, fmt.Errorf("Error: division by 0")
}
return divisor / dividend, nil
}
// Rem -
func (f *MathFuncs) Rem(a, b interface{}) interface{} {
return conv.ToInt64(a) % conv.ToInt64(b)
}
// Pow -
func (f *MathFuncs) Pow(a, b interface{}) interface{} {
r := gmath.Pow(conv.ToFloat64(a), conv.ToFloat64(b))
if f.IsFloat(a) {
return r
}
return conv.ToInt64(r)
}
// Seq - return a sequence from `start` to `end`, in steps of `step`
// start and step are optional, and default to 1.
func (f *MathFuncs) Seq(n ...interface{}) ([]int64, error) {
start := int64(1)
end := int64(0)
step := int64(1)
if len(n) == 0 {
return nil, fmt.Errorf("math.Seq must be given at least an 'end' value")
}
if len(n) == 1 {
end = conv.ToInt64(n[0])
}
if len(n) == 2 {
start = conv.ToInt64(n[0])
end = conv.ToInt64(n[1])
}
if len(n) == 3 {
start = conv.ToInt64(n[0])
end = conv.ToInt64(n[1])
step = conv.ToInt64(n[2])
}
return math.Seq(conv.ToInt64(start), conv.ToInt64(end), conv.ToInt64(step)), nil
}
// Max -
func (f *MathFuncs) Max(a interface{}, b ...interface{}) (interface{}, error) {
if f.IsFloat(a) || f.containsFloat(b...) {
m := conv.ToFloat64(a)
for _, n := range conv.ToFloat64s(b...) {
m = gmath.Max(m, n)
}
return m, nil
}
m := conv.ToInt64(a)
for _, n := range conv.ToInt64s(b...) {
if n > m {
m = n
}
}
return m, nil
}
// Min -
func (f *MathFuncs) Min(a interface{}, b ...interface{}) (interface{}, error) {
if f.IsFloat(a) || f.containsFloat(b...) {
m := conv.ToFloat64(a)
for _, n := range conv.ToFloat64s(b...) {
m = gmath.Min(m, n)
}
return m, nil
}
m := conv.ToInt64(a)
for _, n := range conv.ToInt64s(b...) {
if n < m {
m = n
}
}
return m, nil
}
// Ceil -
func (f *MathFuncs) Ceil(n interface{}) interface{} {
return gmath.Ceil(conv.ToFloat64(n))
}
// Floor -
func (f *MathFuncs) Floor(n interface{}) interface{} {
return gmath.Floor(conv.ToFloat64(n))
}
// Round -
func (f *MathFuncs) Round(n interface{}) interface{} {
return gmath.Round(conv.ToFloat64(n))
}