-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmath.go
More file actions
44 lines (36 loc) · 687 Bytes
/
math.go
File metadata and controls
44 lines (36 loc) · 687 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package genetic
import (
"math"
)
func zeroValue[T any]() T {
var v T
return v
}
func max(x, y int) int {
if x > y {
return x
}
return y
}
func floatDiv(x, y int) float64 {
if y == 0 {
return math.Inf(1)
}
return float64(x) / float64(y)
}
// computeProportions calculates the proportion each number
// represents in the sum of a given set of integers.
func computeProportions(ints []int) []float64 {
sum := 0
for _, n := range ints {
if n < 0 {
panic("failed to compute proportion with negative number in set")
}
sum += n
}
proportions := make([]float64, len(ints))
for i, n := range ints {
proportions[i] = floatDiv(n, sum)
}
return proportions
}