Skip to content

Commit dc88872

Browse files
author
Tyler
authored
Fix for loss of precision issues. (#15)
Enables rounding in cases where a distance in radians calc is converted to the internal degrees representation. This should fix test case failures.
1 parent 1cddeb2 commit dc88872

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

src/main/java/com/javadocmd/simplelatlng/window/CircularWindow.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121
import java.util.List;
2222

2323
import com.javadocmd.simplelatlng.LatLng;
24-
import com.javadocmd.simplelatlng.LatLngTool;
2524
import com.javadocmd.simplelatlng.util.LatLngConfig;
2625
import com.javadocmd.simplelatlng.util.LengthUnit;
2726

27+
import static com.javadocmd.simplelatlng.LatLngTool.distanceInRadians;
28+
2829
/**
2930
* <p>
3031
* A circular window. Has the benefit of performing well around poles and
@@ -105,30 +106,34 @@ public <E> void filterCopySort(Collection<E> source, Collection<E> destination,
105106
}
106107
}
107108

109+
private long toDegreesInternal(Double radians) {
110+
// When converting a radian distance to the internal degrees representation
111+
// for the sake of comparison, rounding the result seems to be appropriate.
112+
return Math.round(Math.toDegrees(radians) / LatLngConfig.DEGREE_TOLERANCE);
113+
}
114+
108115
/**
109116
* A specialized implementation of the {@link #contains(LatLng)} check which
110117
* returns the distance from the center of the window if the window contains
111118
* the point and null if it does not. Used in
112119
* {@link #filterCopySort(Collection, Collection, FilterHelper)}.
113120
*/
114121
private Double containsForSort(LatLng point) {
115-
double d = Math.toDegrees(LatLngTool.distanceInRadians(center, point));
116-
if (LatLngConfig.doubleToLong(d) <= radius)
122+
double d = distanceInRadians(center, point);
123+
if (toDegreesInternal(d) <= radius)
117124
return d;
118125
else
119126
return null;
120127
}
121128

122129
@Override
123130
public boolean contains(LatLng point) {
124-
return LatLngConfig
125-
.doubleToLong(Math.toDegrees(LatLngTool.distanceInRadians(center, point))) <= radius;
131+
return toDegreesInternal(distanceInRadians(center, point)) <= radius;
126132
}
127133

128134
@Override
129135
public boolean overlaps(CircularWindow window) {
130-
long angle = LatLngConfig.doubleToLong(Math.toDegrees(LatLngTool.distanceInRadians(
131-
this.center, window.getCenter())));
136+
long angle = toDegreesInternal(distanceInRadians(this.center, window.getCenter()));
132137
return angle <= (this.radius + window.radius);
133138
}
134139

src/test/java/com/javadocmd/simplelatlng/LatLngToolTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,7 @@ public void testDistanceInRadians() {
143143
}
144144

145145
private static void distRadTest(double expected, LatLng point1, LatLng point2) {
146-
assertEquals(expected, Math.toDegrees(distanceInRadians(point1, point2)),
147-
.00001);
146+
assertEquals(expected, Math.toDegrees(distanceInRadians(point1, point2)), .000001);
148147
}
149148

150149
@Test

0 commit comments

Comments
 (0)