Skip to content

Apache Lucene open-source search software

LUCENE-7194: Ban Math.toRadians and Math.toDegrees

Karl Wrightb11e48c7553d
diff --git a/lucene/core/src/java/org/apache/lucene/geo/Rectangle.java b/lucene/core/src/java/org/apache/lucene/geo/Rectangle.java
index 592e202fa87..c8fddf728c7 100644
--- a/lucene/core/src/java/org/apache/lucene/geo/Rectangle.java
+++ b/lucene/core/src/java/org/apache/lucene/geo/Rectangle.java
@@ -19,8 +19,6 @@ package org.apache.lucene.geo;
import static java.lang.Math.PI;
import static java.lang.Math.max;
import static java.lang.Math.min;
-import static java.lang.Math.toDegrees;
-import static java.lang.Math.toRadians;
import static org.apache.lucene.geo.GeoUtils.checkLatitude;
import static org.apache.lucene.geo.GeoUtils.checkLongitude;
import static org.apache.lucene.geo.GeoUtils.MAX_LAT_INCL;
@@ -34,6 +32,8 @@ import static org.apache.lucene.geo.GeoUtils.sloppySin;
import static org.apache.lucene.util.SloppyMath.TO_DEGREES;
import static org.apache.lucene.util.SloppyMath.asin;
import static org.apache.lucene.util.SloppyMath.cos;
+import static org.apache.lucene.util.SloppyMath.toDegrees;
+import static org.apache.lucene.util.SloppyMath.toRadians;
/** Represents a lat/lon rectangle. */
public class Rectangle {
diff --git a/lucene/core/src/java/org/apache/lucene/util/SloppyMath.java b/lucene/core/src/java/org/apache/lucene/util/SloppyMath.java
index 0bfca5e6f5e..44f2c3bfd84 100644
--- a/lucene/core/src/java/org/apache/lucene/util/SloppyMath.java
+++ b/lucene/core/src/java/org/apache/lucene/util/SloppyMath.java
@@ -176,6 +176,24 @@ public class SloppyMath {
}
}
+ /**
+ * Convert to degrees.
+ * @param radians radians to convert to degrees
+ * @return degrees
+ */
+ public static double toDegrees(final double radians) {
+ return radians * TO_DEGREES;
+ }
+
+ /**
+ * Convert to radians.
+ * @param degrees degrees to convert to radians
+ * @return radians
+ */
+ public static double toRadians(final double degrees) {
+ return degrees * TO_RADIANS;
+ }
+
// haversin
// TODO: remove these for java 9, they fixed Math.toDegrees()/toRadians() to work just like this.
public static final double TO_RADIANS = Math.PI / 180D;
@@ -209,7 +227,7 @@ public class SloppyMath {
// Supposed to be >= sin(77.2deg), as fdlibm code is supposed to work with values > 0.975,
// but seems to work well enough as long as value >= sin(25deg).
- private static final double ASIN_MAX_VALUE_FOR_TABS = StrictMath.sin(Math.toRadians(73.0));
+ private static final double ASIN_MAX_VALUE_FOR_TABS = StrictMath.sin(toRadians(73.0));
private static final int ASIN_TABS_SIZE = (1<<13) + 1;
private static final double ASIN_DELTA = ASIN_MAX_VALUE_FOR_TABS/(ASIN_TABS_SIZE - 1);
diff --git a/lucene/demo/src/java/org/apache/lucene/demo/facet/DistanceFacetsExample.java b/lucene/demo/src/java/org/apache/lucene/demo/facet/DistanceFacetsExample.java
index 96ca57c683d..7d029ee62ed 100644
--- a/lucene/demo/src/java/org/apache/lucene/demo/facet/DistanceFacetsExample.java
+++ b/lucene/demo/src/java/org/apache/lucene/demo/facet/DistanceFacetsExample.java
@@ -46,6 +46,7 @@ import org.apache.lucene.search.SortField;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
+import org.apache.lucene.util.SloppyMath;
import java.io.Closeable;
import java.io.IOException;
@@ -148,8 +149,8 @@ public class DistanceFacetsExample implements Closeable {
// since it's a 2D trie...
// Degrees -> Radians:
- double originLatRadians = Math.toRadians(originLat);
- double originLngRadians = Math.toRadians(originLng);
+ double originLatRadians = SloppyMath.toRadians(originLat);
+ double originLngRadians = SloppyMath.toRadians(originLng);
double angle = maxDistanceKM / EARTH_RADIUS_KM;
@@ -158,28 +159,28 @@ public class DistanceFacetsExample implements Closeable {
double minLng;
double maxLng;
- if (minLat > Math.toRadians(-90) && maxLat < Math.toRadians(90)) {
+ if (minLat > SloppyMath.toRadians(-90) && maxLat < SloppyMath.toRadians(90)) {
double delta = Math.asin(Math.sin(angle)/Math.cos(originLatRadians));
minLng = originLngRadians - delta;
- if (minLng < Math.toRadians(-180)) {
+ if (minLng < SloppyMath.toRadians(-180)) {
minLng += 2 * Math.PI;
}
maxLng = originLngRadians + delta;
- if (maxLng > Math.toRadians(180)) {
+ if (maxLng > SloppyMath.toRadians(180)) {
maxLng -= 2 * Math.PI;
}
} else {
// The query includes a pole!
- minLat = Math.max(minLat, Math.toRadians(-90));
- maxLat = Math.min(maxLat, Math.toRadians(90));
- minLng = Math.toRadians(-180);
- maxLng = Math.toRadians(180);
+ minLat = Math.max(minLat, SloppyMath.toRadians(-90));
+ maxLat = Math.min(maxLat, SloppyMath.toRadians(90));
+ minLng = SloppyMath.toRadians(-180);
+ maxLng = SloppyMath.toRadians(180);
}
BooleanQuery.Builder f = new BooleanQuery.Builder();
// Add latitude range filter:
- f.add(DoublePoint.newRangeQuery("latitude", Math.toDegrees(minLat), Math.toDegrees(maxLat)),
+ f.add(DoublePoint.newRangeQuery("latitude", SloppyMath.toDegrees(minLat), SloppyMath.toDegrees(maxLat)),
BooleanClause.Occur.FILTER);
// Add longitude range filter:
@@ -187,13 +188,13 @@ public class DistanceFacetsExample implements Closeable {
// The bounding box crosses the international date
// line:
BooleanQuery.Builder lonF = new BooleanQuery.Builder();
- lonF.add(DoublePoint.newRangeQuery("longitude", Math.toDegrees(minLng), Double.POSITIVE_INFINITY),
+ lonF.add(DoublePoint.newRangeQuery("longitude", SloppyMath.toDegrees(minLng), Double.POSITIVE_INFINITY),
BooleanClause.Occur.SHOULD);
- lonF.add(DoublePoint.newRangeQuery("longitude", Double.NEGATIVE_INFINITY, Math.toDegrees(maxLng)),
+ lonF.add(DoublePoint.newRangeQuery("longitude", Double.NEGATIVE_INFINITY, SloppyMath.toDegrees(maxLng)),
BooleanClause.Occur.SHOULD);
f.add(lonF.build(), BooleanClause.Occur.MUST);
} else {
- f.add(DoublePoint.newRangeQuery("longitude", Math.toDegrees(minLng), Math.toDegrees(maxLng)),
+ f.add(DoublePoint.newRangeQuery("longitude", SloppyMath.toDegrees(minLng), SloppyMath.toDegrees(maxLng)),
BooleanClause.Occur.FILTER);
}
diff --git a/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/geom/GeoPathTest.java b/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/geom/GeoPathTest.java
index 96c7ea7f2d2..f5dd8b0723b 100755
--- a/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/geom/GeoPathTest.java
+++ b/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/geom/GeoPathTest.java
@@ -18,7 +18,7 @@ package org.apache.lucene.spatial3d.geom;
import org.junit.Test;
-import static java.lang.Math.toRadians;
+import static org.apache.lucene.util.SloppyMath.toRadians;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
diff --git a/lucene/test-framework/src/java/org/apache/lucene/geo/EarthDebugger.java b/lucene/test-framework/src/java/org/apache/lucene/geo/EarthDebugger.java
index fdef990724a..4f68adb0067 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/geo/EarthDebugger.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/geo/EarthDebugger.java
@@ -207,8 +207,8 @@ public class EarthDebugger {
newAngle:
while (angle < 360) {
- double x = Math.cos(Math.toRadians(angle));
- double y = Math.sin(Math.toRadians(angle));
+ double x = Math.cos(SloppyMath.toRadians(angle));
+ double y = Math.sin(SloppyMath.toRadians(angle));
double factor = 2.0;
double step = 1.0;
int last = 0;
diff --git a/lucene/test-framework/src/java/org/apache/lucene/geo/GeoTestUtil.java b/lucene/test-framework/src/java/org/apache/lucene/geo/GeoTestUtil.java
index 62b824fd31e..619b6822a44 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/geo/GeoTestUtil.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/geo/GeoTestUtil.java
@@ -322,8 +322,8 @@ public class GeoTestUtil {
for(int i=0;i<gons;i++) {
double angle = 360.0-i*(360.0/gons);
//System.out.println(" angle " + angle);
- double x = Math.cos(Math.toRadians(angle));
- double y = Math.sin(Math.toRadians(angle));
+ double x = Math.cos(SloppyMath.toRadians(angle));
+ double y = Math.sin(SloppyMath.toRadians(angle));
double factor = 2.0;
double step = 1.0;
int last = 0;
@@ -476,8 +476,8 @@ public class GeoTestUtil {
}
double len = radius * (1.0 - radiusDelta + radiusDelta * random().nextDouble());
//System.out.println(" len=" + len);
- double lat = centerLat + len * Math.cos(Math.toRadians(angle));
- double lon = centerLon + len * Math.sin(Math.toRadians(angle));
+ double lat = centerLat + len * Math.cos(SloppyMath.toRadians(angle));
+ double lon = centerLon + len * Math.sin(SloppyMath.toRadians(angle));
if (lon <= GeoUtils.MIN_LON_INCL || lon >= GeoUtils.MAX_LON_INCL) {
// cannot cross dateline: try again!
continue newPoly;
diff --git a/lucene/tools/forbiddenApis/lucene.txt b/lucene/tools/forbiddenApis/lucene.txt
index 5b89af7e93f..e02bd40ad01 100644
--- a/lucene/tools/forbiddenApis/lucene.txt
+++ b/lucene/tools/forbiddenApis/lucene.txt
@@ -24,3 +24,6 @@ java.io.RandomAccessFile
java.nio.file.Path#toFile()
java.util.jar.JarFile
java.util.zip.ZipFile
+@defaultMessage Use home-grown methods instead
+java.lang.Math#toRadians(double)
+java.lang.Math#toDegrees(double)