2023-05-14 03:13:05 +00:00
|
|
|
package koboldsim
|
|
|
|
|
2024-09-28 04:56:13 +00:00
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
|
|
|
|
"golang.org/x/exp/constraints"
|
|
|
|
)
|
2023-05-14 03:13:05 +00:00
|
|
|
|
|
|
|
// Generic helper functions not directly attached to Card Sim Engine mechanics.
|
|
|
|
|
|
|
|
// Mean calculates the mathematical mean of its arguments (the sum, divided
|
|
|
|
// by the number of elements). If it is called with no arguments, it returns
|
|
|
|
// NaN ("not a number"). If there are contradictory infinities among the
|
|
|
|
// arguments, it also returns NaN. Overflowing or underflowing can create an
|
|
|
|
// infinity.
|
|
|
|
func Mean(vals ...float64) float64 {
|
|
|
|
if len(vals) == 0 {
|
|
|
|
return math.NaN()
|
|
|
|
}
|
|
|
|
|
|
|
|
total := 0.0
|
|
|
|
for _, x := range vals {
|
|
|
|
total += x
|
|
|
|
}
|
|
|
|
return total / float64(len(vals))
|
|
|
|
}
|
2024-09-28 04:56:13 +00:00
|
|
|
|
|
|
|
func clamp[T constraints.Ordered](val, low, high T) T {
|
|
|
|
if val < low {
|
|
|
|
return low
|
|
|
|
}
|
|
|
|
if val > high {
|
|
|
|
return high
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|