spatial/r{2,3}: add Norm and Norm2

This commit is contained in:
Sebastien Binet
2020-04-01 11:26:45 +02:00
committed by GitHub
parent def9e57adc
commit 962425fcd9
4 changed files with 116 additions and 1 deletions

View File

@@ -4,7 +4,10 @@
package r2
import "testing"
import (
"math"
"testing"
)
func TestAdd(t *testing.T) {
for _, test := range []struct {
@@ -135,3 +138,46 @@ func TestCross(t *testing.T) {
})
}
}
func TestNorm(t *testing.T) {
for _, test := range []struct {
v Vec
want float64
}{
{Vec{0, 0}, 0},
{Vec{0, 1}, 1},
{Vec{1, 1}, math.Sqrt2},
{Vec{1, 2}, math.Sqrt(5)},
{Vec{3, -4}, 5},
{Vec{1, 1e-16}, 1},
{Vec{4.3145006366056343748277397783556100978621924913975e-196, 4.3145006366056343748277397783556100978621924913975e-196}, 6.101625315155041e-196},
} {
t.Run("", func(t *testing.T) {
if got, want := Norm(test.v), test.want; got != want {
t.Fatalf("|%v| = %v, want %v", test.v, got, want)
}
})
}
}
func TestNorm2(t *testing.T) {
for _, test := range []struct {
v Vec
want float64
}{
{Vec{0, 0}, 0},
{Vec{0, 1}, 1},
{Vec{1, 1}, 2},
{Vec{1, 2}, 5},
{Vec{3, -4}, 25},
{Vec{1, 1e-16}, 1},
// This will underflow and return zero.
{Vec{4.3145006366056343748277397783556100978621924913975e-196, 4.3145006366056343748277397783556100978621924913975e-196}, 0},
} {
t.Run("", func(t *testing.T) {
if got, want := Norm2(test.v), test.want; got != want {
t.Fatalf("|%v|^2 = %v, want %v", test.v, got, want)
}
})
}
}