-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
479e5ca
commit a107145
Showing
9 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package mathutil | ||
|
||
// Clamp returns a value restricted between lo and hi. | ||
func Clamp[T Number](v, lo, hi T) T { | ||
if v < lo { | ||
return lo | ||
} | ||
if v > hi { | ||
return hi | ||
} | ||
return v | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package mathutil_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/aidenwallis/go-utils/mathutil" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestClamp(t *testing.T) { | ||
assert.Equal(t, 20, mathutil.Clamp(1, 20, 100)) | ||
assert.Equal(t, 100, mathutil.Clamp(101, 20, 100)) | ||
assert.Equal(t, 35, mathutil.Clamp(35, 20, 100)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package mathutil | ||
|
||
// Max finds the largest value of all given values | ||
func Max[T Number](a T, rest ...T) T { | ||
result := a | ||
|
||
for _, v := range rest { | ||
if v > result { | ||
result = v | ||
} | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package mathutil_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/aidenwallis/go-utils/mathutil" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMax(t *testing.T) { | ||
t.Parallel() | ||
assert.Equal(t, 6, mathutil.Max(1, 2, 3, 4, 5, 6)) | ||
assert.Equal(t, 4, mathutil.Max(4)) | ||
assert.Equal(t, 7.0, mathutil.Max(4.0, 3.234, 1.0, 1.01, 5.0, 7.0, 2.0, 5.0, 5.0)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package mathutil | ||
|
||
// Min finds the smallest value of all given values | ||
func Min[T Number](a T, rest ...T) T { | ||
result := a | ||
|
||
for _, v := range rest { | ||
if v < result { | ||
result = v | ||
} | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package mathutil_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/aidenwallis/go-utils/mathutil" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMin(t *testing.T) { | ||
t.Parallel() | ||
assert.Equal(t, 1, mathutil.Min(1, 2, 3, 4, 5, 6)) | ||
assert.Equal(t, 4, mathutil.Min(4)) | ||
assert.Equal(t, 1.0, mathutil.Min(4.0, 3.234, 1.0, 1.01, 5.0, 7.0, 2.0, 5.0, 5.0)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package mathutil | ||
|
||
import "golang.org/x/exp/constraints" | ||
|
||
// Number is the type that supports any form of integer or float | ||
type Number interface { | ||
float32 | float64 | constraints.Integer | ||
} |