it's a graph
parent
e6bac6ae0f
commit
457fc2af1c
@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
func coalesce(errs ...error) error {
|
||||
for _, err := range errs {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// provided a number and a bounding range for that number, normalizes that
|
||||
// number within that bounds (linearly). that is, it returns a float between 0
|
||||
// and 1 representing i's position within the (min,max) range. Min and max are
|
||||
// both inclusive.
|
||||
func norm(i, min, max int) (n float64) {
|
||||
if i < min {
|
||||
return 0
|
||||
}
|
||||
if i > max {
|
||||
return 1
|
||||
}
|
||||
|
||||
span := max - min
|
||||
reach := i - min
|
||||
|
||||
return float64(reach) / float64(span)
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNorm(t *testing.T) {
|
||||
type test struct {
|
||||
i int
|
||||
min int
|
||||
max int
|
||||
n float64
|
||||
}
|
||||
tests := []test{
|
||||
{1, 2, 3, 0},
|
||||
{3, 2, 1, 1},
|
||||
{1, 0, 10, 0.1},
|
||||
{5, 0, 10, 0.5},
|
||||
{125, 100, 200, 0.25},
|
||||
{-125, -200, -100, 0.75},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
n := norm(test.i, test.min, test.max)
|
||||
if n == test.n {
|
||||
t.Logf("norm(%d, %d, %d) == %f", test.i, test.min, test.max, n)
|
||||
} else {
|
||||
t.Errorf("norm(%d, %d, %d) is %f, expected %f", test.i, test.min, test.max, n, test.n)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue