You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2017/05/31 14:11:34 UTC
[1/2] commons-numbers git commit: Removed potentially confusing
method.
Repository: commons-numbers
Updated Branches:
refs/heads/master 04e92c6eb -> c8e0a34f7
Removed potentially confusing method.
Also changed order of constants (CheckStyle).
Project: http://git-wip-us.apache.org/repos/asf/commons-numbers/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-numbers/commit/dba917c9
Tree: http://git-wip-us.apache.org/repos/asf/commons-numbers/tree/dba917c9
Diff: http://git-wip-us.apache.org/repos/asf/commons-numbers/diff/dba917c9
Branch: refs/heads/master
Commit: dba917c96871c67e3b546df95bc6addd63cd19da
Parents: 04e92c6
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
Authored: Wed May 31 16:05:53 2017 +0200
Committer: Gilles Sadowski <gi...@harfang.homelinux.org>
Committed: Wed May 31 16:05:53 2017 +0200
----------------------------------------------------------------------
.../apache/commons/numbers/angle/PlaneAngle.java | 18 ++++--------------
.../commons/numbers/angle/PlaneAngleTest.java | 16 ++++++++--------
2 files changed, 12 insertions(+), 22 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/dba917c9/commons-numbers-angle/src/main/java/org/apache/commons/numbers/angle/PlaneAngle.java
----------------------------------------------------------------------
diff --git a/commons-numbers-angle/src/main/java/org/apache/commons/numbers/angle/PlaneAngle.java b/commons-numbers-angle/src/main/java/org/apache/commons/numbers/angle/PlaneAngle.java
index 649be45..cb8a637 100644
--- a/commons-numbers-angle/src/main/java/org/apache/commons/numbers/angle/PlaneAngle.java
+++ b/commons-numbers-angle/src/main/java/org/apache/commons/numbers/angle/PlaneAngle.java
@@ -20,6 +20,10 @@ package org.apache.commons.numbers.angle;
* Represents the <a href="https://en.wikipedia.org/wiki/Angle">angle</a> concept.
*/
public class PlaneAngle {
+ /** Zero. */
+ public static final PlaneAngle ZERO = new PlaneAngle(0);
+ /** Half-turn (aka π radians). */
+ public static final PlaneAngle PI = new PlaneAngle(0.5);
/** Conversion factor. */
private static final double HALF_TURN = 0.5;
/** Conversion factor. */
@@ -32,10 +36,6 @@ public class PlaneAngle {
private static final double FROM_DEGREES = 1d / TO_DEGREES;
/** Value (in turns). */
private final double value;
- /** Zero. */
- public static final PlaneAngle ZERO = new PlaneAngle(0);
- /** π radians. */
- public static final PlaneAngle PI = new PlaneAngle(HALF_TURN);
/**
* @param value Value in turns.
@@ -98,16 +98,6 @@ public class PlaneAngle {
}
/**
- * Normalize within the interval centered at 0.
- *
- * @return {@code a - 2 * k} with integer {@code k} such that
- * {@code -0.5 <= a - 2 * k <= 0.5} (in turns).
- */
- public PlaneAngle normalize() {
- return normalize(ZERO);
- }
-
- /**
* Test for equality with another object.
* Objects are considered to be equal if the two values are exactly the
* same, or both are {@code Double.NaN}.
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/dba917c9/commons-numbers-angle/src/test/java/org/apache/commons/numbers/angle/PlaneAngleTest.java
----------------------------------------------------------------------
diff --git a/commons-numbers-angle/src/test/java/org/apache/commons/numbers/angle/PlaneAngleTest.java b/commons-numbers-angle/src/test/java/org/apache/commons/numbers/angle/PlaneAngleTest.java
index 42fa94a..ca23e48 100644
--- a/commons-numbers-angle/src/test/java/org/apache/commons/numbers/angle/PlaneAngleTest.java
+++ b/commons-numbers-angle/src/test/java/org/apache/commons/numbers/angle/PlaneAngleTest.java
@@ -72,34 +72,34 @@ public class PlaneAngleTest {
}
@Test
- public void testNormalizeSimple1() {
+ public void testNormalizeAroundZero1() {
final double value = 1.25;
final double expected = 0.25;
- final double actual = PlaneAngle.ofTurns(value).normalize().toTurns();
+ final double actual = PlaneAngle.ofTurns(value).normalize(PlaneAngle.ZERO).toTurns();
final double tol = Math.ulp(expected);
Assert.assertEquals(expected, actual, tol);
}
@Test
- public void testNormalizeSimple2() {
+ public void testNormalizeAroundZero2() {
final double value = 0.75;
final double expected = -0.25;
- final double actual = PlaneAngle.ofTurns(value).normalize().toTurns();
+ final double actual = PlaneAngle.ofTurns(value).normalize(PlaneAngle.ZERO).toTurns();
final double tol = Math.ulp(expected);
Assert.assertEquals(expected, actual, tol);
}
@Test
- public void testNormalizeSimple3() {
+ public void testNormalizeAroundZero3() {
final double value = 0.5 + 1e-10;
final double expected = -0.5 + 1e-10;
- final double actual = PlaneAngle.ofTurns(value).normalize().toTurns();
+ final double actual = PlaneAngle.ofTurns(value).normalize(PlaneAngle.ZERO).toTurns();
final double tol = Math.ulp(expected);
Assert.assertEquals(expected, actual, tol);
}
@Test
- public void testNormalizeSimple4() {
+ public void testNormalizeAroundZero4() {
final double value = 5 * Math.PI / 4;
final double expected = Math.PI * (1d / 4 - 1);
- final double actual = PlaneAngle.ofRadians(value).normalize().toRadians();
+ final double actual = PlaneAngle.ofRadians(value).normalize(PlaneAngle.ZERO).toRadians();
final double tol = Math.ulp(expected);
Assert.assertEquals(expected, actual, tol);
}
[2/2] commons-numbers git commit: NUMBERS-41: Additional utility
methods.
Posted by er...@apache.org.
NUMBERS-41: Additional utility methods.
Project: http://git-wip-us.apache.org/repos/asf/commons-numbers/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-numbers/commit/c8e0a34f
Tree: http://git-wip-us.apache.org/repos/asf/commons-numbers/tree/c8e0a34f
Diff: http://git-wip-us.apache.org/repos/asf/commons-numbers/diff/c8e0a34f
Branch: refs/heads/master
Commit: c8e0a34f7be9fcef0c1c23ff58a548c12e800db0
Parents: dba917c
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
Authored: Wed May 31 16:09:05 2017 +0200
Committer: Gilles Sadowski <gi...@harfang.homelinux.org>
Committed: Wed May 31 16:09:05 2017 +0200
----------------------------------------------------------------------
.../numbers/angle/PlaneAngleRadians.java | 25 +++++++
.../numbers/angle/PlaneAngleRadiansTest.java | 68 ++++++++++++++++++++
2 files changed, 93 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/c8e0a34f/commons-numbers-angle/src/main/java/org/apache/commons/numbers/angle/PlaneAngleRadians.java
----------------------------------------------------------------------
diff --git a/commons-numbers-angle/src/main/java/org/apache/commons/numbers/angle/PlaneAngleRadians.java b/commons-numbers-angle/src/main/java/org/apache/commons/numbers/angle/PlaneAngleRadians.java
index 0dd6b0a..c5312f2 100644
--- a/commons-numbers-angle/src/main/java/org/apache/commons/numbers/angle/PlaneAngleRadians.java
+++ b/commons-numbers-angle/src/main/java/org/apache/commons/numbers/angle/PlaneAngleRadians.java
@@ -23,6 +23,9 @@ package org.apache.commons.numbers.angle;
* @see PlaneAngle
*/
public class PlaneAngleRadians {
+ /** Utility class. */
+ private PlaneAngleRadians() {}
+
/**
* Normalize an angle in an interval of size 2π around a
* center value.
@@ -38,4 +41,26 @@ public class PlaneAngleRadians {
final PlaneAngle c = PlaneAngle.ofRadians(center);
return a.normalize(c).toRadians();
}
+
+ /**
+ * Normalize an angle between -π and π.
+ *
+ * @param angle Value to be normalized.
+ * @return {@code a - 2 * k} with integer {@code k} such that
+ * {@code -pi <= a - 2 * k * pi <= pi}.
+ */
+ public static double normalizeBetweenMinusPiAndPi(double angle) {
+ return PlaneAngle.ofRadians(angle).normalize(PlaneAngle.ZERO).toRadians();
+ }
+
+ /**
+ * Normalize an angle between 0 and 2π.
+ *
+ * @param angle Value to be normalized.
+ * @return {@code a - 2 * k} with integer {@code k} such that
+ * {@code 0 <= a - 2 * k * pi <= 2 * pi}.
+ */
+ public static double normalizeBetweenZeroAndTwoPi(double angle) {
+ return PlaneAngle.ofRadians(angle).normalize(PlaneAngle.PI).toRadians();
+ }
}
http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/c8e0a34f/commons-numbers-angle/src/test/java/org/apache/commons/numbers/angle/PlaneAngleRadiansTest.java
----------------------------------------------------------------------
diff --git a/commons-numbers-angle/src/test/java/org/apache/commons/numbers/angle/PlaneAngleRadiansTest.java b/commons-numbers-angle/src/test/java/org/apache/commons/numbers/angle/PlaneAngleRadiansTest.java
index 1244915..65f4f6d 100644
--- a/commons-numbers-angle/src/test/java/org/apache/commons/numbers/angle/PlaneAngleRadiansTest.java
+++ b/commons-numbers-angle/src/test/java/org/apache/commons/numbers/angle/PlaneAngleRadiansTest.java
@@ -20,6 +20,8 @@ import org.junit.Test;
* Test cases for the {@link PlaneAngleRadians} class.
*/
public class PlaneAngleRadiansTest {
+ private static final double TWO_PI = 2 * Math.PI;
+
@Test
public void testNormalize() {
for (double a = -15.0; a <= 15.0; a += 0.1) {
@@ -32,4 +34,70 @@ public class PlaneAngleRadiansTest {
}
}
}
+
+ @Test
+ public void testNormalizeBetweenMinusPiAndPi1() {
+ final double value = 1.25 * TWO_PI;
+ final double expected = 0.25 * TWO_PI;
+ final double actual = PlaneAngleRadians.normalizeBetweenMinusPiAndPi(value);
+ final double tol = Math.ulp(expected);
+ Assert.assertEquals(expected, actual, tol);
+ }
+ @Test
+ public void testNormalizeBetweenMinusPiAndPi2() {
+ final double value = 0.75 * TWO_PI;
+ final double expected = -0.25 * TWO_PI;
+ final double actual = PlaneAngleRadians.normalizeBetweenMinusPiAndPi(value);
+ final double tol = Math.ulp(expected);
+ Assert.assertEquals(expected, actual, tol);
+ }
+ @Test
+ public void testNormalizeBetweenMinusPiAndPi3() {
+ final double value = 0.5 * TWO_PI + 1e-10;
+ final double expected = -0.5 * TWO_PI + 1e-10;
+ final double actual = PlaneAngleRadians.normalizeBetweenMinusPiAndPi(value);
+ final double tol = Math.ulp(expected);
+ Assert.assertEquals(expected, actual, tol);
+ }
+ @Test
+ public void testNormalizeBetweenMinusPiAndPi4() {
+ final double value = 5 * Math.PI / 4;
+ final double expected = Math.PI * (1d / 4 - 1);
+ final double actual = PlaneAngleRadians.normalizeBetweenMinusPiAndPi(value);
+ final double tol = Math.ulp(expected);
+ Assert.assertEquals(expected, actual, tol);
+ }
+
+ @Test
+ public void testNormalizeBetweenZeroAndTwoPi1() {
+ final double value = 1.25 * TWO_PI;
+ final double expected = 0.25 * TWO_PI;
+ final double actual = PlaneAngleRadians.normalizeBetweenZeroAndTwoPi(value);
+ final double tol = Math.ulp(expected);
+ Assert.assertEquals(expected, actual, tol);
+ }
+ @Test
+ public void testNormalizeBetweenZeroAndTwoPi2() {
+ final double value = 1.75 * TWO_PI;
+ final double expected = 0.75 * TWO_PI;
+ final double actual = PlaneAngleRadians.normalizeBetweenZeroAndTwoPi(value);
+ final double tol = Math.ulp(expected);
+ Assert.assertEquals(expected, actual, tol);
+ }
+ @Test
+ public void testNormalizeBetweenZeroAndTwoPi3() {
+ final double value = -0.5 * TWO_PI + 1e-10;
+ final double expected = 0.5 * TWO_PI + 1e-10;
+ final double actual = PlaneAngleRadians.normalizeBetweenZeroAndTwoPi(value);
+ final double tol = Math.ulp(expected);
+ Assert.assertEquals(expected, actual, tol);
+ }
+ @Test
+ public void testNormalizeBetweenZeroAndTwoPi4() {
+ final double value = 9 * Math.PI / 4;
+ final double expected = Math.PI / 4;
+ final double actual = PlaneAngleRadians.normalizeBetweenZeroAndTwoPi(value);
+ final double tol = Math.ulp(expected);
+ Assert.assertEquals(expected, actual, tol);
+ }
}