Skip to content

Commit d80219c

Browse files
Merge pull request #10 from goinsane/develop
v1.3.0
2 parents 7427fc2 + 01ac3b0 commit d80219c

File tree

2 files changed

+129
-16
lines changed

2 files changed

+129
-16
lines changed

examples/example1/main.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ import (
1111

1212
func main() {
1313
fmt.Printf("Pi is %f\n", math.Pi)
14+
fmt.Printf("Square root of 2 (Sqrt2) is %f\n", math.Sqrt2)
1415
fmt.Printf("Pi floor with precision 4: %f\n", xmath.FloorP(math.Pi, 4))
1516
fmt.Printf("Pi ceil with precision 4: %f\n", xmath.CeilP(math.Pi, 4))
1617
fmt.Printf("Pi round with precision 4: %f\n", xmath.RoundP(math.Pi, 4))
1718
fmt.Printf("Pi round with precision 2: %f\n", xmath.RoundP(math.Pi, 2))
1819
fmt.Printf("Pi round: %f\n", xmath.Round(math.Pi))
19-
fmt.Printf("Square root of 2 is %f\n", math.Sqrt2)
20-
mn, mx := xmath.MinMax(math.Pi, math.Sqrt2)
21-
fmt.Printf("Pi and Sqrt2: Min=%f, Max=%f\n", mn, mx)
20+
fmt.Printf("Min of Pi and Sqrt2 is %f\n", xmath.Min(math.Pi, math.Sqrt2))
21+
fmt.Printf("Max of Pi and Sqrt2 is %f\n", xmath.Max(math.Pi, math.Sqrt2))
22+
min, max := xmath.MinMax(math.Pi, math.Sqrt2)
23+
fmt.Printf("Pi and Sqrt2: Min=%f, Max=%f\n", min, max)
2224
fmt.Printf("Is 2.4 between 3 and 2.4: %v\n", xmath.Between(2.4, 3, 2.4))
2325
fmt.Printf("Is 2.4 between in 3 and 2.4: %v\n", xmath.BetweenIn(2.4, 3, 2.4))
2426
}

xmath.go

Lines changed: 124 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,119 @@ func RoundP(x float64, p int) float64 {
3030
return math.Floor(x*k+0.5) / k
3131
}
3232

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)
39116
}
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
40146
}
41147

42148
// Between checks x is between a and b
@@ -74,6 +180,16 @@ func SafeDiv(x, y float64, allowNaN bool) float64 {
74180
return x / y
75181
}
76182

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+
77193
// CryptoRandInt returns a random integer in [0, max).
78194
// It returns -1 when error occurs.
79195
func CryptoRandInt(max int64) int64 {
@@ -87,14 +203,9 @@ func CryptoRandInt(max int64) int64 {
87203
return num.Int64()
88204
}
89205

90-
// CryptoRandFloat returns a random decimal number in [0, 1).
91-
// It returns -1 when error occurs.
206+
// CryptoRandFloat is synonym with CryptoRand.
92207
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()
98209
}
99210

100211
// CryptoRandCode generates random code in [10^(n-1), 10^n).

0 commit comments

Comments
 (0)