From 6adefa81f7dd9dcdeb6476e276b1ce4fd20066ca Mon Sep 17 00:00:00 2001 From: bloeys Date: Tue, 14 May 2024 06:06:00 +0400 Subject: [PATCH] Add Clamp --- README.md | 2 ++ gglm/scalar.go | 24 ++++++++++++++++- gglm/scalar_test.go | 64 +++++++++++++++++++++++++++++++++++++++++++++ go.mod | 4 ++- go.sum | 2 ++ 5 files changed, 94 insertions(+), 2 deletions(-) create mode 100755 gglm/scalar_test.go diff --git a/README.md b/README.md index 140aa45..8eba60c 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,8 @@ gglm currently has the following: ## Installation +Note: gglm requires Go 1.18 or higher. + `go get github.com/bloeys/gglm` ## Usage diff --git a/gglm/scalar.go b/gglm/scalar.go index 3c88dbe..6ed297a 100755 --- a/gglm/scalar.go +++ b/gglm/scalar.go @@ -1,6 +1,10 @@ package gglm -import "math" +import ( + "math" + + "golang.org/x/exp/constraints" +) // EqF32 true if abs(f1-f2) <= F32Epsilon func EqF32(f1, f2 float32) bool { @@ -52,3 +56,21 @@ func Abs32(x float32) float32 { func Sqrt32(x float32) float32 { return float32(math.Sqrt(float64(x))) } + +// Clamp returns: +// +// min if xmax +// x if x>=min && x<=max +func Clamp[T constraints.Ordered](x, min, max T) T { + + if x < min { + return min + } + + if x > max { + return max + } + + return x +} diff --git a/gglm/scalar_test.go b/gglm/scalar_test.go new file mode 100755 index 0000000..7606e83 --- /dev/null +++ b/gglm/scalar_test.go @@ -0,0 +1,64 @@ +package gglm_test + +import ( + "testing" + + "github.com/bloeys/gglm/gglm" +) + +func TestClamp(t *testing.T) { + + x := 5 + ans := 5 + if gglm.Clamp(x, 0, 10) != ans { + t.Errorf("Got: %v; Expected: %v", x, ans) + } + + x = 10 + ans = 10 + if gglm.Clamp(x, 0, 10) != ans { + t.Errorf("Got: %v; Expected: %v", x, ans) + } + + x = 20 + ans = 10 + if gglm.Clamp(x, 0, 10) != ans { + t.Errorf("Got: %v; Expected: %v", x, ans) + } + + x = -10 + ans = 0 + if gglm.Clamp(x, 0, 10) != ans { + t.Errorf("Got: %v; Expected: %v", x, ans) + } + + xf := 1.5 + ansf := 1.5 + if gglm.Clamp(xf, 0, 10) != ansf { + t.Errorf("Got: %v; Expected: %v", xf, ansf) + } + + xf = 15 + ansf = 10 + if gglm.Clamp(xf, 0, 10) != ansf { + t.Errorf("Got: %v; Expected: %v", xf, ansf) + } + + xf = -1.5 + ansf = 0 + if gglm.Clamp(xf, 0, 10) != ansf { + t.Errorf("Got: %v; Expected: %v", xf, ansf) + } + + xf = 2 + ansf = 1.5 + if gglm.Clamp(xf, 0.5, 1.5) != ansf { + t.Errorf("Got: %v; Expected: %v", xf, ansf) + } + + xf = 1.2 + ansf = 1.2 + if gglm.Clamp(xf, 0.5, 1.5) != ansf { + t.Errorf("Got: %v; Expected: %v", xf, ansf) + } +} diff --git a/go.mod b/go.mod index f1c57fc..4462ff6 100755 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/bloeys/gglm -go 1.17 +go 1.18 + +require golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 diff --git a/go.sum b/go.sum index e69de29..d023c2f 100755 --- a/go.sum +++ b/go.sum @@ -0,0 +1,2 @@ +golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 h1:vr/HnozRka3pE4EsMEg1lgkXJkTFJCVUX+S/ZT6wYzM= +golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc=