goext/mathext/math.go

50 lines
734 B
Go
Raw Permalink Normal View History

2022-10-27 16:00:57 +02:00
package mathext
2022-10-27 17:03:30 +02:00
import "gogs.mikescher.com/BlackForestBytes/goext/langext"
2022-10-27 16:00:57 +02:00
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
}
2022-10-27 17:03:30 +02:00
func Max[T langext.OrderedConstraint](v1 T, v2 T) T {
if v1 > v2 {
return v1
} else {
return v2
}
}
func Min[T langext.OrderedConstraint](v1 T, v2 T) T {
if v1 < v2 {
return v1
} else {
return v2
}
}
2022-11-19 13:34:21 +01:00
func Abs[T langext.NumberConstraint](v T) T {
if v < 0 {
return -v
} else {
return v
}
}
func Clamp[T langext.NumberConstraint](v T, min T, max T) T {
if v < min {
return min
} else if v > max {
return max
} else {
return v
}
}