core: add Rescale utility function for straight linear rescaling
Signed-off-by: Ron Evans <ron@hybridgroup.com>
This commit is contained in:
parent
46dab20a2d
commit
355181843f
5
utils.go
5
utils.go
|
@ -57,6 +57,11 @@ func ToScale(input, min, max float64) float64 {
|
|||
}
|
||||
}
|
||||
|
||||
// Rescale performs a direct linear rescaling of a number from one scale to another.
|
||||
func Rescale(input, fromMin, fromMax, toMin, toMax float64) float64 {
|
||||
return (input-fromMin)*(toMax-toMin)/(fromMax-fromMin) + toMin
|
||||
}
|
||||
|
||||
// DefaultName returns a sensible random default name
|
||||
// for a robot, adaptor or driver
|
||||
func DefaultName(name string) string {
|
||||
|
|
|
@ -73,6 +73,11 @@ func TestToScale(t *testing.T) {
|
|||
gobottest.Assert(t, ToScale(0.5, 0, 10), 5.0)
|
||||
}
|
||||
|
||||
func TestRescale(t *testing.T) {
|
||||
gobottest.Assert(t, Rescale(500, 0, 1000, 0, 10), 5.0)
|
||||
gobottest.Assert(t, Rescale(-1.0, -1, 0, 490, 350), 490.0)
|
||||
}
|
||||
|
||||
func TestRand(t *testing.T) {
|
||||
a := Rand(10000)
|
||||
b := Rand(10000)
|
||||
|
|
Loading…
Reference in New Issue