Skip to content

Commit

Permalink
fix minMaxDist floating point rounding errors
Browse files Browse the repository at this point in the history
This manifests itsef in

#29
  • Loading branch information
fumin committed Feb 16, 2024
1 parent b0f9edc commit 6b841c8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion rtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,8 @@ func (tree *Rtree) nearestNeighbor(p Point, n *node, d float64, nearest Spatial)

for _, e := range n.entries {
minDist := p.minDist(e.bb)
if minDist > minMinMaxDist {
// Add a bit of tolerance to guard against floating point rounding errors.
if minDist > minMinMaxDist+1e-6 {
continue
}

Expand Down
18 changes: 18 additions & 0 deletions rtree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,24 @@ func TestNearestNeighborsHalf(t *testing.T) {
}
}

func TestMinMaxDistFloatingPointRoundingError(t *testing.T) {
rects := []Rect{
Point{1134900, 15600}.ToRect(0),
Point{1134900, 25600}.ToRect(0),
Point{1134900, 22805}.ToRect(0),
Point{1134900, 29116}.ToRect(0),
}
things := make([]Spatial, 0, len(rects))
for i := range rects {
things = append(things, &rects[i])
}
rt := NewTree(2, 1, 2, things...)
n := rt.NearestNeighbor(Point{1134851.8, 25570.8})
if n != things[1] {
t.Fatalf("wrong neighbor, expected %v, got %v", things[1], n)
}
}

func ensureOrderedSubset(t *testing.T, actual []Spatial, expected []Spatial) {
for i := range actual {
if len(expected)-1 < i || actual[i] != expected[i] {
Expand Down

0 comments on commit 6b841c8

Please sign in to comment.