added Mod and ModPositive

This commit is contained in:
milarin 2024-01-12 18:19:35 +01:00
parent de82000934
commit a18f1c51cb
2 changed files with 25 additions and 1 deletions

16
math.go
View File

@ -1,6 +1,8 @@
package gmath package gmath
import "math" import (
"math"
)
func Pow[N Number](x, y N) N { func Pow[N Number](x, y N) N {
return N(math.Pow(float64(x), float64(y))) 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 { func Abs[N Number](v N) N {
return N(math.Abs(float64(v))) 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
}

10
math_test.go Normal file
View File

@ -0,0 +1,10 @@
package gmath
import (
"fmt"
"testing"
)
func TestMod(t *testing.T) {
fmt.Println(ModPositive(-1, 10))
}