You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by kw...@apache.org on 2016/06/22 14:16:35 UTC
[1/2] lucene-solr:branch_6x: LUCENE-7194: Ban Math.toRadians and
Math.toDegrees
Repository: lucene-solr
Updated Branches:
refs/heads/branch_6x 60dcef6c6 -> 388d388c9
LUCENE-7194: Ban Math.toRadians and Math.toDegrees
Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/40ca6f1d
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/40ca6f1d
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/40ca6f1d
Branch: refs/heads/branch_6x
Commit: 40ca6f1d64ab8ec2e873c2a6c6815ca449b046a4
Parents: 60dcef6
Author: Karl Wright <Da...@gmail.com>
Authored: Wed Jun 22 03:53:47 2016 -0400
Committer: Karl Wright <Da...@gmail.com>
Committed: Wed Jun 22 07:52:36 2016 -0400
----------------------------------------------------------------------
.../java/org/apache/lucene/geo/Rectangle.java | 4 +--
.../java/org/apache/lucene/util/SloppyMath.java | 20 ++++++++++++++-
.../demo/facet/DistanceFacetsExample.java | 27 ++++++++++----------
.../lucene/spatial3d/geom/GeoPathTest.java | 2 +-
.../org/apache/lucene/geo/EarthDebugger.java | 4 +--
.../java/org/apache/lucene/geo/GeoTestUtil.java | 8 +++---
lucene/tools/forbiddenApis/lucene.txt | 3 +++
7 files changed, 45 insertions(+), 23 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/40ca6f1d/lucene/core/src/java/org/apache/lucene/geo/Rectangle.java
----------------------------------------------------------------------
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 592e202..c8fddf7 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 {
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/40ca6f1d/lucene/core/src/java/org/apache/lucene/util/SloppyMath.java
----------------------------------------------------------------------
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 0bfca5e..44f2c3b 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);
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/40ca6f1d/lucene/demo/src/java/org/apache/lucene/demo/facet/DistanceFacetsExample.java
----------------------------------------------------------------------
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 96ca57c..7d029ee 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);
}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/40ca6f1d/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/geom/GeoPathTest.java
----------------------------------------------------------------------
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 96c7ea7..f5dd8b0 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;
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/40ca6f1d/lucene/test-framework/src/java/org/apache/lucene/geo/EarthDebugger.java
----------------------------------------------------------------------
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 fdef990..4f68adb 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;
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/40ca6f1d/lucene/test-framework/src/java/org/apache/lucene/geo/GeoTestUtil.java
----------------------------------------------------------------------
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 62b824f..619b682 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;
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/40ca6f1d/lucene/tools/forbiddenApis/lucene.txt
----------------------------------------------------------------------
diff --git a/lucene/tools/forbiddenApis/lucene.txt b/lucene/tools/forbiddenApis/lucene.txt
index 5b89af7..e02bd40 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)
[2/2] lucene-solr:branch_6x: LUCENE-7194: Ban Math.toRadians,
Math.toDegrees
Posted by kw...@apache.org.
LUCENE-7194: Ban Math.toRadians, Math.toDegrees
Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/388d388c
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/388d388c
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/388d388c
Branch: refs/heads/branch_6x
Commit: 388d388c990c8d9e05ec9ba9bc881fdc921e734b
Parents: 40ca6f1
Author: Karl Wright <Da...@gmail.com>
Authored: Wed Jun 22 10:16:22 2016 -0400
Committer: Karl Wright <Da...@gmail.com>
Committed: Wed Jun 22 10:16:22 2016 -0400
----------------------------------------------------------------------
.../apache/lucene/spatial/geopoint/search/GeoPointTestUtil.java | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/388d388c/lucene/spatial/src/test/org/apache/lucene/spatial/geopoint/search/GeoPointTestUtil.java
----------------------------------------------------------------------
diff --git a/lucene/spatial/src/test/org/apache/lucene/spatial/geopoint/search/GeoPointTestUtil.java b/lucene/spatial/src/test/org/apache/lucene/spatial/geopoint/search/GeoPointTestUtil.java
index a8c4271..029c464 100644
--- a/lucene/spatial/src/test/org/apache/lucene/spatial/geopoint/search/GeoPointTestUtil.java
+++ b/lucene/spatial/src/test/org/apache/lucene/spatial/geopoint/search/GeoPointTestUtil.java
@@ -22,6 +22,7 @@ import java.util.Random;
import org.apache.lucene.geo.GeoUtils;
import org.apache.lucene.geo.Polygon;
import org.apache.lucene.geo.Rectangle;
+import org.apache.lucene.util.SloppyMath;
import com.carrotsearch.randomizedtesting.RandomizedContext;
@@ -212,8 +213,8 @@ final class GeoPointTestUtil {
}
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;