From 816b5e8e7a3cb6c0cd57145b55920c620a9003a3 Mon Sep 17 00:00:00 2001 From: Kistaro Windrider Date: Sat, 13 May 2023 20:13:05 -0700 Subject: [PATCH] Mathematical mean helper. --- koboldsim/util.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 koboldsim/util.go diff --git a/koboldsim/util.go b/koboldsim/util.go new file mode 100644 index 0000000..b6cbd42 --- /dev/null +++ b/koboldsim/util.go @@ -0,0 +1,22 @@ +package koboldsim + +import "math" + +// 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)) +}