@@ -30,13 +30,119 @@ func RoundP(x float64, p int) float64 {
30
30
return math .Floor (x * k + 0.5 ) / k
31
31
}
32
32
33
- // MinMax returns the min, max values in this order.
34
- func MinMax (x , y float64 ) (min float64 , max float64 ) {
35
- if x < y {
36
- return x , y
37
- } else {
38
- return y , x
33
+ // Min returns the smaller of x...
34
+ //
35
+ // Special cases are:
36
+ // Min(x, -Inf) = Min(-Inf, x) = -Inf
37
+ // Min(x, NaN) = Min(NaN, x) = NaN
38
+ // Min(-0, ±0) = Min(±0, -0) = -0
39
+ // Min(x) = x
40
+ // Min() = -Inf
41
+ func Min (x ... float64 ) float64 {
42
+ if len (x ) <= 0 {
43
+ return math .Inf (- 1 )
44
+ }
45
+ result := math .Inf (+ 1 )
46
+ for _ , a := range x {
47
+ result = math .Min (a , result )
48
+ }
49
+ return result
50
+ }
51
+
52
+ // Max returns the larger of x...
53
+ //
54
+ // Special cases are:
55
+ // Max(x, +Inf) = Max(+Inf, x) = +Inf
56
+ // Max(x, NaN) = Max(NaN, x) = NaN
57
+ // Max(+0, ±0) = Max(±0, +0) = +0
58
+ // Max(-0, -0) = -0
59
+ // Max(x) = x
60
+ // Max() = +Inf
61
+ func Max (x ... float64 ) float64 {
62
+ if len (x ) <= 0 {
63
+ return math .Inf (+ 1 )
64
+ }
65
+ result := math .Inf (- 1 )
66
+ for _ , a := range x {
67
+ result = math .Max (a , result )
68
+ }
69
+ return result
70
+ }
71
+
72
+ // MinMax returns the min, max values in this order, similar with Min and Max functions.
73
+ //
74
+ // Special cases are:
75
+ // MinMax(x) = x, x
76
+ // MinMax() = -Inf, +Inf
77
+ func MinMax (x ... float64 ) (min float64 , max float64 ) {
78
+ if len (x ) <= 0 {
79
+ return math .Inf (- 1 ), math .Inf (+ 1 )
80
+ }
81
+ min = math .Inf (+ 1 )
82
+ max = math .Inf (- 1 )
83
+ for _ , a := range x {
84
+ min = math .Min (a , min )
85
+ max = math .Max (a , max )
86
+ }
87
+ return
88
+ }
89
+
90
+ // MinInt returns the smaller integer of x...
91
+ //
92
+ // Special cases are:
93
+ // MinInt(x) = x
94
+ // MinInt() = math.MinInt64
95
+ func MinInt (x ... int64 ) int64 {
96
+ if len (x ) <= 0 {
97
+ return int64 (math .MinInt64 )
98
+ }
99
+ result := int64 (math .MaxInt64 )
100
+ for _ , a := range x {
101
+ if a < result {
102
+ result = a
103
+ }
104
+ }
105
+ return result
106
+ }
107
+
108
+ // MaxInt returns the larger integer of x...
109
+ //
110
+ // Special cases are:
111
+ // MaxInt(x) = x
112
+ // MaxInt() = math.MaxInt64
113
+ func MaxInt (x ... int64 ) int64 {
114
+ if len (x ) <= 0 {
115
+ return int64 (math .MaxInt64 )
39
116
}
117
+ result := int64 (math .MinInt64 )
118
+ for _ , a := range x {
119
+ if a > result {
120
+ result = a
121
+ }
122
+ }
123
+ return result
124
+ }
125
+
126
+ // MinMaxInt returns the min, max integers in this order, similar with MinInt and MaxInt functions.
127
+ //
128
+ // Special cases are:
129
+ // MinMaxInt(x) = x, x
130
+ // MinMaxInt() = math.MinInt64, math.MaxInt64
131
+ func MinMaxInt (x ... int64 ) (min int64 , max int64 ) {
132
+ if len (x ) <= 0 {
133
+ return int64 (math .MinInt64 ), int64 (math .MaxInt64 )
134
+ }
135
+ min = int64 (math .MaxInt64 )
136
+ max = int64 (math .MinInt64 )
137
+ for _ , a := range x {
138
+ if a < min {
139
+ min = a
140
+ }
141
+ if a > max {
142
+ max = a
143
+ }
144
+ }
145
+ return
40
146
}
41
147
42
148
// Between checks x is between a and b
@@ -74,6 +180,16 @@ func SafeDiv(x, y float64, allowNaN bool) float64 {
74
180
return x / y
75
181
}
76
182
183
+ // CryptoRand returns a random decimal number in [0, 1).
184
+ // It returns -1 when error occurs.
185
+ func CryptoRand () float64 {
186
+ r := CryptoRandInt (math .MaxInt64 )
187
+ if r < 0 {
188
+ return - 1
189
+ }
190
+ return float64 (r ) / math .MaxInt64
191
+ }
192
+
77
193
// CryptoRandInt returns a random integer in [0, max).
78
194
// It returns -1 when error occurs.
79
195
func CryptoRandInt (max int64 ) int64 {
@@ -87,14 +203,9 @@ func CryptoRandInt(max int64) int64 {
87
203
return num .Int64 ()
88
204
}
89
205
90
- // CryptoRandFloat returns a random decimal number in [0, 1).
91
- // It returns -1 when error occurs.
206
+ // CryptoRandFloat is synonym with CryptoRand.
92
207
func CryptoRandFloat () float64 {
93
- r := CryptoRandInt (math .MaxInt64 )
94
- if r < 0 {
95
- return - 1
96
- }
97
- return float64 (r ) / math .MaxInt64
208
+ return CryptoRand ()
98
209
}
99
210
100
211
// CryptoRandCode generates random code in [10^(n-1), 10^n).
0 commit comments