Added MapFloatBetween util-function
This commit is contained in:
parent
31e6b1cb5e
commit
8eb850d9fe
@ -15,3 +15,11 @@ func check(err error) {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
type Numeric interface {
|
||||
float64
|
||||
}
|
||||
|
||||
func MapFloatBetween[T Numeric](value, start1, stop1, start2, stop2 T) T {
|
||||
return start2 + (value-start1)*(stop2-start2)/(stop1-start1)
|
||||
}
|
||||
|
14
pkg/utils_test.go
Normal file
14
pkg/utils_test.go
Normal file
@ -0,0 +1,14 @@
|
||||
package gomus_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
gomus "git.cesium.pw/niku/gomus/pkg"
|
||||
)
|
||||
|
||||
func TestMapBetween(t *testing.T) {
|
||||
r := gomus.MapFloatBetween(2, 1, 3, 5, 10)
|
||||
if r != 7.5 {
|
||||
t.Fatalf("Expected value to be mapped to #, got: %f", r)
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user