1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-18 08:32:40 +08:00
This commit is contained in:
2023-08-25 15:31:00 +08:00
parent 04aecd4abc
commit b0a97978d8
58 changed files with 1330 additions and 476 deletions

46
mathx/math.go Normal file
View File

@ -0,0 +1,46 @@
package mathx
import (
"github.com/charlienet/go-mixed/expr"
"golang.org/x/exp/constraints"
)
type Real interface {
constraints.Integer | constraints.Float
}
// Max returns the larger one of v1 and v2.
func Max[T Real](v1, v2 T) T {
return expr.Ternary(v1 > v2, v1, v2)
}
// Min returns the smaller one of v1 and v2.
func Min[T Real](v1, v2 T) T {
return expr.Ternary(v1 < v2, v1, v2)
}
func Abs[T Real](val T) T {
return expr.Ternary(val < 0, -val, val)
}
// Neg returns the negative of value. It does not negate value. For
// negating, simply use -value instead.
func Neg[T Real](value T) T {
return expr.Ternary(value < 0, value, -value)
}
func Clamp[T Real](value, min, max T) T {
if min > max {
return min
}
if value < min {
return min
}
if value > max {
return max
}
return value
}