commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject [2/2] commons-numbers git commit: NUMBERS-41: Additional utility methods.
Date Wed, 31 May 2017 14:11:35 GMT
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 <gilles@harfang.homelinux.org>
Authored: Wed May 31 16:09:05 2017 +0200
Committer: Gilles Sadowski <gilles@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&pi; 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 -&pi; and &pi;.
+     *
+     * @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&pi;.
+     *
+     * @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);
+    }
 }


Mime
View raw message