50 lines
734 B
Go
50 lines
734 B
Go
package mathext
|
|
|
|
import "gogs.mikescher.com/BlackForestBytes/goext/langext"
|
|
|
|
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
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|