-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminmax.go
102 lines (95 loc) · 1.62 KB
/
minmax.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package finlib
import (
"math"
)
// Returns the maximum value in s.
func Max(s []float64) float64 {
if len(s) < 1 {
return math.NaN()
}
max := s[0]
for i := 0; i < len(s); i++ {
if s[i] > max {
max = s[i]
}
}
return max
}
// Returns the first index of the maximum value included in s.
func MaxIndex(s []float64) int {
var index int
if len(s) < 1 {
return index
}
max := s[0]
for i := 0; i < len(s); i++ {
if s[i] > max {
max = s[i]
index = i
}
}
return index
}
// Returns the minimum value in s.
func Min(s []float64) float64 {
if len(s) < 1 {
return math.NaN()
}
min := s[0]
for i := 0; i < len(s); i++ {
if s[i] < min {
min = s[i]
}
}
return min
}
// Returns the first index of the minimum value included in s.
func MinIndex(s []float64) int {
var index int
if len(s) < 1 {
return index
}
min := s[0]
for i := 0; i < len(s); i++ {
if s[i] > min {
min = s[i]
index = i
}
}
return index
}
// Returns the minimum and maximum values included in s.
func MinMax(s []float64) (float64, float64) {
if len(s) < 1 {
return math.NaN(), math.NaN()
}
min, max := s[0], s[0]
for i := 0; i < len(s); i++ {
if s[i] < min {
min = s[i]
}
if s[i] > max {
max = s[i]
}
}
return min, max
}
// Returns the first indices of the minimum and maximum values included in s.
func MinMaxIndex(s []float64) (int, int) {
minIndex, maxIndex := 0, 0
if len(s) < 1 {
return minIndex, maxIndex
}
min, max := s[0], s[0]
for i := 0; i < len(s); i++ {
if s[i] < min {
min = s[i]
minIndex = i
}
if s[i] > max {
max = s[i]
maxIndex = i
}
}
return minIndex, maxIndex
}