You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
1.3 KiB
Go
84 lines
1.3 KiB
Go
1 year ago
|
package nmath
|
||
|
|
||
|
import (
|
||
|
"git.noahlan.cn/noahlan/ntool/ndef"
|
||
|
"math"
|
||
|
)
|
||
|
|
||
|
// Min compare two value and return max value
|
||
|
func Min[T ndef.XIntOrFloat](x, y T) T {
|
||
|
if x < y {
|
||
|
return x
|
||
|
}
|
||
|
return y
|
||
|
}
|
||
|
|
||
|
// Max compare two value and return max value
|
||
|
func Max[T ndef.XIntOrFloat](x, y T) T {
|
||
|
if x > y {
|
||
|
return x
|
||
|
}
|
||
|
return y
|
||
|
}
|
||
|
|
||
|
// SwapMin compare and always return [min, max] value
|
||
|
func SwapMin[T ndef.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 ndef.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)
|
||
|
}
|
||
|
|
||
|
// OrElse return s OR nv(new-value) on s is empty
|
||
|
func OrElse[T ndef.XIntOrFloat](in, nv T) T {
|
||
|
if in != 0 {
|
||
|
return in
|
||
|
}
|
||
|
return nv
|
||
|
}
|