-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: mathutil - add new func: IntOrDefault, UnitOrDefault, FloatOr…
…Default ... and more for convert with default - support for issues #111
- Loading branch information
Showing
12 changed files
with
602 additions
and
272 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package mathutil | ||
|
||
import ( | ||
"math" | ||
|
||
"github.com/gookit/goutil/comdef" | ||
) | ||
|
||
// Min compare two value and return max value | ||
func Min[T comdef.XintOrFloat](x, y T) T { | ||
if x < y { | ||
return x | ||
} | ||
return y | ||
} | ||
|
||
// Max compare two value and return max value | ||
func Max[T comdef.XintOrFloat](x, y T) T { | ||
if x > y { | ||
return x | ||
} | ||
return y | ||
} | ||
|
||
// SwapMin compare and always return [min, max] value | ||
func SwapMin[T comdef.XintOrFloat](x, y T) (T, T) { | ||
if x < y { | ||
return x, y | ||
} | ||
return y, x | ||
} | ||
|
||
// SwapMax compare and always return [max, min] value | ||
func SwapMax[T comdef.XintOrFloat](x, y T) (T, T) { | ||
if x > y { | ||
return x, y | ||
} | ||
return y, x | ||
} | ||
|
||
// MaxInt compare and return max value | ||
func MaxInt(x, y int) int { | ||
if x > y { | ||
return x | ||
} | ||
return y | ||
} | ||
|
||
// SwapMaxInt compare and return max, min value | ||
func SwapMaxInt(x, y int) (int, int) { | ||
if x > y { | ||
return x, y | ||
} | ||
return y, x | ||
} | ||
|
||
// MaxI64 compare and return max value | ||
func MaxI64(x, y int64) int64 { | ||
if x > y { | ||
return x | ||
} | ||
return y | ||
} | ||
|
||
// SwapMaxI64 compare and return max, min value | ||
func SwapMaxI64(x, y int64) (int64, int64) { | ||
if x > y { | ||
return x, y | ||
} | ||
return y, x | ||
} | ||
|
||
// MaxFloat compare and return max value | ||
func MaxFloat(x, y float64) float64 { | ||
return math.Max(x, y) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package mathutil_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gookit/goutil/mathutil" | ||
"github.com/gookit/goutil/testutil/assert" | ||
) | ||
|
||
func TestMaxFloat(t *testing.T) { | ||
assert.Eq(t, float64(3), mathutil.MaxFloat(2, 3)) | ||
assert.Eq(t, 3.3, mathutil.MaxFloat(2.1, 3.3)) | ||
|
||
assert.Eq(t, 3.3, mathutil.Max(2.1, 3.3)) | ||
assert.Eq(t, 3.3, mathutil.Max(3.3, 2.1)) | ||
|
||
assert.Eq(t, 2.1, mathutil.Min(2.1, 3.3)) | ||
assert.Eq(t, 2.1, mathutil.Min(3.3, 2.1)) | ||
} | ||
|
||
func TestMaxI64(t *testing.T) { | ||
assert.Eq(t, 3, mathutil.MaxInt(2, 3)) | ||
assert.Eq(t, 3, mathutil.MaxInt(3, 2)) | ||
|
||
assert.Eq(t, int64(3), mathutil.MaxI64(2, 3)) | ||
assert.Eq(t, int64(3), mathutil.MaxI64(3, 2)) | ||
|
||
assert.Eq(t, 3, mathutil.Max[int](3, 2)) | ||
assert.Eq(t, int64(3), mathutil.Max[int64](3, 2)) | ||
assert.Eq(t, int64(3), mathutil.Max(int64(3), int64(2))) | ||
} | ||
|
||
func TestSwapMaxInt(t *testing.T) { | ||
x, y := mathutil.SwapMax(2, 34) | ||
assert.Eq(t, 34, x) | ||
assert.Eq(t, 2, y) | ||
|
||
x, y = mathutil.SwapMax(34, 2) | ||
assert.Eq(t, 34, x) | ||
assert.Eq(t, 2, y) | ||
|
||
x, y = mathutil.SwapMin(2, 34) | ||
assert.Eq(t, 2, x) | ||
assert.Eq(t, 34, y) | ||
|
||
x, y = mathutil.SwapMin(34, 2) | ||
assert.Eq(t, 2, x) | ||
assert.Eq(t, 34, y) | ||
|
||
x, y = mathutil.SwapMaxInt(2, 34) | ||
assert.Eq(t, 34, x) | ||
assert.Eq(t, 2, y) | ||
x, y = mathutil.SwapMaxInt(34, 2) | ||
assert.Eq(t, 34, x) | ||
assert.Eq(t, 2, y) | ||
|
||
x64, y64 := mathutil.SwapMaxI64(2, 34) | ||
assert.Eq(t, int64(34), x64) | ||
assert.Eq(t, int64(2), y64) | ||
x64, y64 = mathutil.SwapMaxI64(34, 2) | ||
assert.Eq(t, int64(34), x64) | ||
assert.Eq(t, int64(2), y64) | ||
} |
Oops, something went wrong.