/
js_math.go
109 lines (99 loc) · 2.69 KB
/
js_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
package templatefunctions
import (
"context"
"math"
"reflect"
)
type (
// JsMath is exported as a template function
JsMath struct{}
// Math is our Javascript's Math equivalent
Math struct{}
)
// Func as implementation of debug method
func (ml JsMath) Func(ctx context.Context) interface{} {
return func() Math {
return Math{}
}
}
// Ceil rounds a value up to the next biggest integer
func (m Math) Ceil(x interface{}) int {
if reflect.TypeOf(x).Kind() == reflect.Int {
x = float64(reflect.ValueOf(x).Int())
} else if reflect.TypeOf(x).Kind() == reflect.Int64 {
x = float64(reflect.ValueOf(x).Int())
} else if reflect.TypeOf(x).Kind() == reflect.Float64 {
x = float64(reflect.ValueOf(x).Float())
}
return int(math.Ceil(x.(float64)))
}
// Min gets the minimum value
func (m Math) Min(x ...interface{}) (res float64) {
res = float64(math.MaxFloat64)
for _, v := range x {
if reflect.TypeOf(v).Kind() == reflect.Int {
v = float64(reflect.ValueOf(v).Int())
} else if reflect.TypeOf(v).Kind() == reflect.Int64 {
v = float64(reflect.ValueOf(v).Int())
} else if reflect.TypeOf(v).Kind() == reflect.Float64 {
v = float64(reflect.ValueOf(v).Float())
}
if v.(float64) < res {
res = v.(float64)
}
}
return
}
// Max gets the maximum value
func (m Math) Max(x ...interface{}) (res float64) {
res = float64(math.SmallestNonzeroFloat64)
for _, v := range x {
if reflect.TypeOf(v).Kind() == reflect.Int {
v = float64(reflect.ValueOf(v).Int())
} else if reflect.TypeOf(v).Kind() == reflect.Int64 {
v = float64(reflect.ValueOf(v).Int())
} else if reflect.TypeOf(v).Kind() == reflect.Float64 {
v = float64(reflect.ValueOf(v).Float())
}
if v.(float64) > res {
res = v.(float64)
}
}
return
}
// Trunc drops the decimals
func (m Math) Trunc(x interface{}) int {
if reflect.TypeOf(x).Kind() == reflect.Int {
x = float64(reflect.ValueOf(x).Int())
} else if reflect.TypeOf(x).Kind() == reflect.Int64 {
x = float64(reflect.ValueOf(x).Int())
} else if reflect.TypeOf(x).Kind() == reflect.Float64 {
x = float64(reflect.ValueOf(x).Float())
}
return int(math.Trunc(x.(float64)))
}
func round(n float64) float64 {
if n >= 0.5 {
return math.Trunc(n + 0.5)
}
if n <= -0.5 {
return math.Trunc(n - 0.5)
}
if math.IsNaN(n) {
return math.NaN()
}
return 0
}
// Round rounds a value to the nearest integer
func (m Math) Round(x interface{}) int {
if reflect.TypeOf(x).Kind() == reflect.Int {
x = float64(reflect.ValueOf(x).Int())
} else if reflect.TypeOf(x).Kind() == reflect.Int64 {
x = float64(reflect.ValueOf(x).Int())
} else if reflect.TypeOf(x).Kind() == reflect.Float64 {
x = float64(reflect.ValueOf(x).Float())
} else {
return 0
}
return int(round(x.(float64)))
}