/
mathutil.go
90 lines (71 loc) · 2.04 KB
/
mathutil.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
// Package mathutil provides some additional math methods
package mathutil
// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
import (
"math"
)
// ////////////////////////////////////////////////////////////////////////////////// //
type Numeric interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
~float32 | ~float64
}
type NumericNeg interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~float32 | ~float64
}
// ////////////////////////////////////////////////////////////////////////////////// //
// Between returns value between min and max values
func Between[N Numeric](val, min, max N) N {
switch {
case val < min:
return min
case val > max:
return max
default:
return val
}
}
// Min returns a smaller value
func Min[N Numeric](val1, val2 N) N {
if val1 < val2 {
return val1
}
return val2
}
// Max returns a greater value
func Max[N Numeric](val1, val2 N) N {
if val1 > val2 {
return val1
}
return val2
}
// Abs returns absolute value
func Abs[N NumericNeg](val N) N {
if val < 0 {
return val * -1
}
return val
}
// Perc calculates percentage
func Perc[N Numeric](val1, val2 N) float64 {
if val2 == 0 {
return 0
}
return float64(val1) / float64(val2) * 100.0
}
// Round returns rounded value
func Round(v float64, p int) float64 {
pow := math.Pow(10, float64(p))
digit := pow * v
_, div := math.Modf(digit)
if div >= 0.5 {
return math.Ceil(digit) / pow
}
return math.Floor(digit) / pow
}
// ////////////////////////////////////////////////////////////////////////////////// //