From a18f1c51cb2580083d4a894e92f21f6c51892063 Mon Sep 17 00:00:00 2001 From: milarin Date: Fri, 12 Jan 2024 18:19:35 +0100 Subject: [PATCH] added Mod and ModPositive --- math.go | 16 +++++++++++++++- math_test.go | 10 ++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 math_test.go diff --git a/math.go b/math.go index f2860f5..6c1a1af 100644 --- a/math.go +++ b/math.go @@ -1,6 +1,8 @@ package gmath -import "math" +import ( + "math" +) func Pow[N Number](x, y N) N { return N(math.Pow(float64(x), float64(y))) @@ -9,3 +11,15 @@ func Pow[N Number](x, y N) N { func Abs[N Number](v N) N { return N(math.Abs(float64(v))) } + +func Mod[N Number](a, b N) N { + return N(math.Mod(float64(a), float64(b))) +} + +func ModPositive[N Number](a, b N) N { + m := Mod(a, b) + if m >= 0 { + return m + } + return m + b +} diff --git a/math_test.go b/math_test.go new file mode 100644 index 0000000..4731dd4 --- /dev/null +++ b/math_test.go @@ -0,0 +1,10 @@ +package gmath + +import ( + "fmt" + "testing" +) + +func TestMod(t *testing.T) { + fmt.Println(ModPositive(-1, 10)) +}