remove ginext/mongoext (no-dep lib)

This commit is contained in:
2022-10-27 16:07:42 +02:00
parent 568d7bd5e3
commit 47f123b86f
31 changed files with 6 additions and 123 deletions

41
mathext/clamp.go Normal file
View File

@@ -0,0 +1,41 @@
package mathext
func ClampInt(v int, lo int, hi int) int {
if v < lo {
return lo
} else if v > hi {
return hi
} else {
return v
}
}
func ClampInt32(v int32, lo int32, hi int32) int32 {
if v < lo {
return lo
} else if v > hi {
return hi
} else {
return v
}
}
func ClampFloat32(v float32, lo float32, hi float32) float32 {
if v < lo {
return lo
} else if v > hi {
return hi
} else {
return v
}
}
func ClampFloat64(v float64, lo float64, hi float64) float64 {
if v < lo {
return lo
} else if v > hi {
return hi
} else {
return v
}
}

7
mathext/float.go Normal file
View File

@@ -0,0 +1,7 @@
package mathext
import "math"
func Float64EpsilonEq(v1 float64, v2 float64, eps float64) bool {
return math.Abs(v1-v2) <= eps
}

13
mathext/math.go Normal file
View File

@@ -0,0 +1,13 @@
package mathext
func AvgFloat64(arr []float64) float64 {
return SumFloat64(arr) / float64(len(arr))
}
func SumFloat64(arr []float64) float64 {
sum := 0.0
for _, v := range arr {
sum += v
}
return sum
}

41
mathext/statistics.go Normal file
View File

@@ -0,0 +1,41 @@
package mathext
func Sum(v []float64) float64 {
total := float64(0)
for _, v := range v {
total += v
}
return total
}
func Mean(v []float64) float64 {
return Sum(v) / float64(len(v))
}
func Median(v []float64) float64 {
if len(v)%2 == 1 {
return v[len(v)/2]
} else {
return (v[len(v)/2-1] + v[len(v)/2]) / float64(2)
}
}
func Min(v []float64) float64 {
r := v[0]
for _, val := range v {
if val < r {
r = val
}
}
return r
}
func Max(v []float64) float64 {
r := v[0]
for _, val := range v {
if val > r {
r = val
}
}
return r
}