Mathematical mean helper.
This commit is contained in:
parent
e61d7571f0
commit
816b5e8e7a
22
koboldsim/util.go
Normal file
22
koboldsim/util.go
Normal file
@ -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))
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user