Skip to content

Commit 9a88175

Browse files
nes1983dsymonds
authored andcommitted
s2: Add LatLng.ApproxEqual.
Add s1.Angle.ApproxEqual too. Signed-off-by: David Symonds <dsymonds@golang.org>
1 parent 335b722 commit 9a88175

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

s1/angle.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import (
4747
//
4848
// (60*Degree).Degrees() != 60
4949
//
50-
// When testing for equality, you should allow for numerical errors (floatApproxEq)
50+
// When testing for equality, you should allow for numerical errors (ApproxEqual)
5151
// or convert to discrete E5/E6/E7 values first.
5252
type Angle float64
5353

@@ -111,5 +111,10 @@ func (a Angle) String() string {
111111
return strconv.FormatFloat(a.Degrees(), 'f', 7, 64) // like "%.7f"
112112
}
113113

114+
// ApproxEqual reports whether the two angles are the same up to a small tolerance.
115+
func (a Angle) ApproxEqual(other Angle) bool {
116+
return math.Abs(float64(a)-float64(other)) <= epsilon
117+
}
118+
114119
// BUG(dsymonds): The major differences from the C++ version are:
115120
// - no unsigned E5/E6/E7 methods

s2/latlng.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,9 @@ func PointFromLatLng(ll LatLng) Point {
9393
func LatLngFromPoint(p Point) LatLng {
9494
return LatLng{latitude(p), longitude(p)}
9595
}
96+
97+
// ApproxEqual reports whether the latitude and longitude of the two LatLngs
98+
// are the same up to a small tolerance.
99+
func (ll LatLng) ApproxEqual(other LatLng) bool {
100+
return ll.Lat.ApproxEqual(other.Lat) && ll.Lng.ApproxEqual(other.Lng)
101+
}

s2/latlng_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,21 @@ func TestLatLngDistance(t *testing.T) {
150150
}
151151
}
152152
}
153+
154+
func TestLatLngApproxEqual(t *testing.T) {
155+
const ε = epsilon / 10
156+
tests := []struct {
157+
a, b LatLng
158+
want bool
159+
}{
160+
{LatLngFromDegrees(30, 50), LatLngFromDegrees(30, 50+ε), true},
161+
{LatLngFromDegrees(30, 50), LatLngFromDegrees(30-ε, 50), true},
162+
{LatLngFromDegrees(1, 5), LatLngFromDegrees(2, 3), false},
163+
}
164+
165+
for _, test := range tests {
166+
if got := test.a.ApproxEqual(test.b); got != test.want {
167+
t.Errorf("%v.ApproxEqual(%v) = %t, want %t", test.a, test.b, got, test.want)
168+
}
169+
}
170+
}

s2/rect_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,7 @@ func TestDirectedHausdorffDistanceRectToRectDegenerateCases(t *testing.T) {
11081108
rectFromDegrees(-20, 95, 20, 105), rectFromDegrees(-30, 5, 30, 15))
11091109
}
11101110

1111-
func TestApproxEqual(t *testing.T) {
1111+
func TestRectApproxEqual(t *testing.T) {
11121112
// s1.Interval and r1.Interval have additional testing.
11131113

11141114
const ε = epsilon / 10

0 commit comments

Comments
 (0)