Skip to content

Commit

Permalink
FIX: Fixes dot() returning NaN. (#26)
Browse files Browse the repository at this point in the history
This seems to be due to floating point inaccuracies.
  • Loading branch information
SolarLune committed Sep 26, 2022
1 parent 0b64764 commit 007106a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
22 changes: 20 additions & 2 deletions arithmetic.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,23 @@ func dot(a, b []float64) float64 {
result, dimA, dimB := 0., len(a), len(b)

if dimA == 2 && dimB == 2 {
return a[x]*b[x] + a[y]*b[y]
result = a[x]*b[x] + a[y]*b[y]
if result > 1 {
result = 1
} else if result < -1 {
result = -1
}
return result
}

if dimA == 3 && dimB == 3 {
return a[x]*b[x] + a[y]*b[y] + a[z]*b[z]
result = a[x]*b[x] + a[y]*b[y] + a[z]*b[z]
if result > 1 {
result = 1
} else if result < -1 {
result = -1
}
return result
}

if dimA > dimB {
Expand All @@ -246,6 +258,12 @@ func dot(a, b []float64) float64 {
result += a[i] * b[i]
}

if result > 1 {
result = 1
} else if result < -1 {
result = -1
}

return result
}

Expand Down
17 changes: 17 additions & 0 deletions vector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,20 @@ func TestSwizzling(t *testing.T) {
}

}

func TestDotNaN(t *testing.T) {
v1 := vec{1, 0, 0}
v2 := vec{0, 1, 0}

if v1.Dot(v2) != 0 {
t.Error("dot function did not return values expected")
}

v1 = vector.Vector{-0.9040721420170615, 0, 0.4273798802338293}
v2 = v1.Invert()

if math.IsNaN(v1.Dot(v2)) {
t.Error("dot function returned NaN")
}

}

0 comments on commit 007106a

Please sign in to comment.