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

View File

@ -1,35 +0,0 @@
package mathx
import (
"github.com/charlienet/go-mixed/expr"
"golang.org/x/exp/constraints"
"unsafe"
)
// Max returns the larger one of v1 and v2.
func Max[T constraints.Ordered](v1, v2 T) T {
return expr.Ternary(v1 > v2, v1, v2)
}
// Min returns the smaller one of v1 and v2.
func Min[T constraints.Ordered](v1, v2 T) T {
return expr.Ternary(v1 < v2, v1, v2)
}
func Abs1[T constraints.Signed](n T) T {
shift := 63
switch unsafe.Sizeof(n) {
case 1:
shift = 7
case 4:
shift = 31
}
y := n >> shift
return T((n ^ y) - y)
}
func Abs(n int64) int64 {
y := n >> 63
return (n ^ y) - y
}

View File

@ -1,27 +0,0 @@
package mathx_test
import (
"github.com/charlienet/go-mixed/mathx"
"github.com/stretchr/testify/assert"
"testing"
)
func TestMin(t *testing.T) {
assert.Equal(t, 1, mathx.Min(1, 3))
assert.Equal(t, 2, mathx.Min(66, 2))
}
func TestMax(t *testing.T) {
assert.Equal(t, 3, mathx.Max(1, 3))
assert.Equal(t, 66, mathx.Max(66, 2))
}
func TestAbs(t *testing.T) {
assert.Equal(t, 23, mathx.Abs1(23))
assert.Equal(t, 23, mathx.Abs1(-23))
assert.Equal(t, 0, mathx.Abs1(0))
var u int8 = -127
var exp int8 = 127
assert.Equal(t, exp, mathx.Abs1(u))
}

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
}

54
mathx/math_test.go Normal file
View File

@ -0,0 +1,54 @@
package mathx_test
import (
"testing"
"github.com/charlienet/go-mixed/mathx"
"github.com/stretchr/testify/assert"
)
func TestMin(t *testing.T) {
assert.Equal(t, 1, mathx.Min(1, 3))
assert.Equal(t, 2, mathx.Min(66, 2))
}
func TestMax(t *testing.T) {
assert.Equal(t, 3, mathx.Max(1, 3))
assert.Equal(t, 66, mathx.Max(66, 2))
}
func TestAbs(t *testing.T) {
assert.Equal(t, 23, mathx.Abs(23))
assert.Equal(t, 23, mathx.Abs(-23))
assert.Equal(t, 0, mathx.Abs(0))
var u int8 = -127
var exp int8 = 127
assert.Equal(t, exp, mathx.Abs(u))
assert.Equal(t, 1.23, mathx.Abs(-1.23))
}
func TestClamp(t *testing.T) {
tests := []struct{ value, min, max, want int }{
// Min.
{-1, 0, 2, 0},
{0, 0, 2, 0},
// Mid.
{1, 0, 2, 1},
{2, 0, 2, 2},
// Max.
{2, 0, 2, 2},
{3, 0, 2, 2},
// Empty range.
{-1, 0, 0, 0},
{0, 0, 0, 0},
{1, 0, 0, 0},
}
for _, test := range tests {
got := mathx.Clamp(test.value, test.min, test.max)
if got != test.want {
t.Errorf("Clamp(%v, %v, %v) = %v, want %v", test.value, test.min, test.max, got, test.want)
}
}
}