-
Notifications
You must be signed in to change notification settings - Fork 134
/
calc.go
73 lines (65 loc) · 1.24 KB
/
calc.go
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package eventbenchmark
import (
"math"
)
func statistics(data []int64) *Statistics {
return &Statistics{
Min: minInt64(data),
Max: maxInt64(data),
Average: avgInt64(data),
Variance: varianceInt64(data),
StandardDeviation: standardDeviationInt64(data),
}
}
func minInt64(input []int64) int64 {
var m int64
for i, e := range input {
if i == 0 || e < m {
m = e
}
}
return m
}
func maxInt64(input []int64) int64 {
var m int64
for i, e := range input {
if i == 0 || e > m {
m = e
}
}
return m
}
func sumInt64(input []int64) int64 {
var sum int64
for _, e := range input {
sum += e
}
return sum
}
func avgInt64(input []int64) float64 {
num := len(input)
if num == 0 {
return 0
}
sum := sumInt64(input)
avg := float64(sum) / float64(num)
return avg
}
func varianceInt64(numbers []int64) float64 {
if len(numbers) < 2 {
return 0
}
var total float64
avg := avgInt64(numbers)
for _, number := range numbers {
total += math.Pow(float64(number)-avg, 2)
}
num := len(numbers)
variance := total / float64(num-1)
return variance
}
func standardDeviationInt64(numbers []int64) float64 {
variance := varianceInt64(numbers)
stdDev := math.Sqrt(variance)
return stdDev
}