From ff2f846ceab47b8be80fc0cb0a4fe23420168b6d Mon Sep 17 00:00:00 2001 From: Timon Ringwald Date: Tue, 16 Aug 2022 12:23:03 +0200 Subject: [PATCH] min, max and limit implemented --- go.mod | 3 +++ limits.go | 19 +++++++++++++++++++ number.go | 5 +++++ 3 files changed, 27 insertions(+) create mode 100644 go.mod create mode 100644 limits.go create mode 100644 number.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..121c502 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.milar.in/milarin/gmath + +go 1.19 diff --git a/limits.go b/limits.go new file mode 100644 index 0000000..cd11839 --- /dev/null +++ b/limits.go @@ -0,0 +1,19 @@ +package gmath + +func Min[N Number](a, b N) N { + if a < b { + return a + } + return b +} + +func Max[N Number](a, b N) N { + if a > b { + return a + } + return b +} + +func Limit[N Number](v, min, max N) N { + return Min(Max(v, min), max) +} diff --git a/number.go b/number.go new file mode 100644 index 0000000..bd92510 --- /dev/null +++ b/number.go @@ -0,0 +1,5 @@ +package gmath + +type Number interface { + ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~float32 | ~float64 +}