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 2019/11/28 00:49:16 UTC

[commons-numbers] branch master updated: Common constants for angles in radians.

This is an automated email from the ASF dual-hosted git repository.

erans pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git


The following commit(s) were added to refs/heads/master by this push:
     new f473b68  Common constants for angles in radians.
f473b68 is described below

commit f473b68f79e34257922b6ee305e7f889b72f847d
Author: Gilles Sadowski <gi...@harfang.homelinux.org>
AuthorDate: Thu Nov 28 01:48:01 2019 +0100

    Common constants for angles in radians.
---
 .../apache/commons/numbers/angle/PlaneAngle.java   |  2 +-
 .../commons/numbers/angle/PlaneAngleRadians.java   | 15 ++++
 .../numbers/angle/PlaneAngleRadiansTest.java       | 93 ++++++++++++++++------
 3 files changed, 83 insertions(+), 27 deletions(-)

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 2c99b16..a421e43 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
@@ -27,7 +27,7 @@ public final class PlaneAngle {
     /** Conversion factor. */
     private static final double HALF_TURN = 0.5;
     /** Conversion factor. */
-    private static final double TO_RADIANS = 2 * Math.PI;
+    private static final double TO_RADIANS = PlaneAngleRadians.TWO_PI;
     /** Conversion factor. */
     private static final double FROM_RADIANS = 1d / TO_RADIANS;
     /** Conversion factor. */
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 f140b8b..d03218f 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,21 @@ package org.apache.commons.numbers.angle;
  * @see PlaneAngle
  */
 public final class PlaneAngleRadians {
+    /** Value of \( \pi \): {@value}. */
+    public static final double PI = Math.PI;
+    /** Value of \( -\pi \): {@value}. */
+    public static final double MINUS_PI = -PI;
+    /** Value of \( 2\pi \): {@value}. */
+    public static final double TWO_PI = 2 * PI;
+    /** Value of \( -2\pi \): {@value}. */
+    public static final double MINUS_TWO_PI = -TWO_PI;
+    /** Value of \( \pi/2 \): {@value}. */
+    public static final double PI_OVER_TWO = 0.5 * PI;
+    /** Value of \( -\pi/2 \): {@value}. */
+    public static final double MINUS_PI_OVER_TWO = -PI_OVER_TWO;
+    /** Value of \( 3\pi/2 \): {@value}. */
+    public static final double THREE_PI_OVER_TWO = 3 * PI_OVER_TWO;
+
     /** Utility class. */
     private PlaneAngleRadians() {}
 
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 72ffd69..73c3168 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
@@ -23,49 +23,90 @@ import org.junit.jupiter.api.Test;
  * Test cases for the {@link PlaneAngleRadians} class.
  */
 public class PlaneAngleRadiansTest {
-    private static final double TWO_PI = 2 * Math.PI;
+    @Test
+    public void testConstants() {
+        final double eps = 0;
+
+        Assertions.assertEquals(PlaneAngleRadians.PI, Math.PI, eps);
+        Assertions.assertEquals(PlaneAngleRadians.MINUS_PI, -Math.PI, eps);
+
+        Assertions.assertEquals(PlaneAngleRadians.TWO_PI, 2 * Math.PI, eps);
+        Assertions.assertEquals(PlaneAngleRadians.MINUS_TWO_PI, -2 * Math.PI, eps);
+
+        Assertions.assertEquals(PlaneAngleRadians.PI_OVER_TWO, Math.PI / 2, eps);
+        Assertions.assertEquals(PlaneAngleRadians.MINUS_PI_OVER_TWO, -Math.PI / 2, eps);
+
+        Assertions.assertEquals(PlaneAngleRadians.THREE_PI_OVER_TWO, 3 * Math.PI / 2, eps);
+    }
+
+    // Test constants using "sin" and "cos".
+    @Test
+    public void testConstants2() {
+        final double eps = Math.ulp(1d);
+
+        Assertions.assertEquals(Math.sin(PlaneAngleRadians.PI), 0d, eps);
+        Assertions.assertEquals(Math.cos(PlaneAngleRadians.PI), -1d, eps);
+
+        Assertions.assertEquals(Math.sin(PlaneAngleRadians.MINUS_PI), 0d, eps);
+        Assertions.assertEquals(Math.cos(PlaneAngleRadians.MINUS_PI), -1d, eps);
+
+        Assertions.assertEquals(Math.sin(PlaneAngleRadians.TWO_PI), 0d, 2 * eps);
+        Assertions.assertEquals(Math.cos(PlaneAngleRadians.TWO_PI), 1d, eps);
+
+        Assertions.assertEquals(Math.sin(PlaneAngleRadians.MINUS_TWO_PI), 0d, 2 * eps);
+        Assertions.assertEquals(Math.cos(PlaneAngleRadians.MINUS_TWO_PI), 1d, eps);
+
+        Assertions.assertEquals(Math.sin(PlaneAngleRadians.PI_OVER_TWO), 1d, eps);
+        Assertions.assertEquals(Math.cos(PlaneAngleRadians.PI_OVER_TWO), 0d, eps);
+
+        Assertions.assertEquals(Math.sin(PlaneAngleRadians.MINUS_PI_OVER_TWO), -1d, eps);
+        Assertions.assertEquals(Math.cos(PlaneAngleRadians.MINUS_PI_OVER_TWO), 0d, eps);
+
+        Assertions.assertEquals(Math.sin(PlaneAngleRadians.THREE_PI_OVER_TWO), -1d, eps);
+        Assertions.assertEquals(Math.cos(PlaneAngleRadians.THREE_PI_OVER_TWO), 0d, eps);
+    }
 
     @Test
     public void testNormalize() {
         for (double a = -15.0; a <= 15.0; a += 0.1) {
             for (double b = -15.0; b <= 15.0; b += 0.2) {
                 final double c = PlaneAngleRadians.normalize(a, b);
-                Assertions.assertTrue((b - Math.PI) <= c);
-                Assertions.assertTrue(c <= (b + Math.PI));
-                double twoK = Math.rint((a - c) / Math.PI);
-                Assertions.assertEquals(c, a - twoK * Math.PI, 1e-14);
+                Assertions.assertTrue((b - PlaneAngleRadians.PI) <= c);
+                Assertions.assertTrue(c <= (b + PlaneAngleRadians.PI));
+                double twoK = Math.rint((a - c) / PlaneAngleRadians.PI);
+                Assertions.assertEquals(c, a - twoK * PlaneAngleRadians.PI, 1e-14);
             }
         }
     }
 
     @Test
     public void testNormalizeBetweenMinusPiAndPi1() {
-        final double value = 1.25 * TWO_PI;
-        final double expected = 0.25 * TWO_PI;
+        final double value = 1.25 * PlaneAngleRadians.TWO_PI;
+        final double expected = PlaneAngleRadians.PI_OVER_TWO;
         final double actual = PlaneAngleRadians.normalizeBetweenMinusPiAndPi(value);
         final double tol = Math.ulp(expected);
         Assertions.assertEquals(expected, actual, tol);
     }
     @Test
     public void testNormalizeBetweenMinusPiAndPi2() {
-        final double value = 0.75 * TWO_PI;
-        final double expected = -0.25 * TWO_PI;
+        final double value = 0.75 * PlaneAngleRadians.TWO_PI;
+        final double expected = PlaneAngleRadians.MINUS_PI_OVER_TWO;
         final double actual = PlaneAngleRadians.normalizeBetweenMinusPiAndPi(value);
         final double tol = Math.ulp(expected);
         Assertions.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 value = PlaneAngleRadians.PI + 1e-10;
+        final double expected = PlaneAngleRadians.MINUS_PI + 1e-10;
         final double actual = PlaneAngleRadians.normalizeBetweenMinusPiAndPi(value);
         final double tol = Math.ulp(expected);
         Assertions.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 value = 5 * PlaneAngleRadians.PI / 4;
+        final double expected = PlaneAngleRadians.PI * (1d / 4 - 1);
         final double actual = PlaneAngleRadians.normalizeBetweenMinusPiAndPi(value);
         final double tol = Math.ulp(expected);
         Assertions.assertEquals(expected, actual, tol);
@@ -73,16 +114,16 @@ public class PlaneAngleRadiansTest {
 
     @Test
     public void testNormalizeBetweenMinusPiAndPi_lowerBound() {
-        final double value = -Math.PI;
-        final double expected = -Math.PI;
+        final double value = PlaneAngleRadians.PI;
+        final double expected = PlaneAngleRadians.MINUS_PI;
         final double actual = PlaneAngleRadians.normalizeBetweenMinusPiAndPi(value);
         final double tol = Math.ulp(expected);
         Assertions.assertEquals(expected, actual, tol);
     }
     @Test
     public void testNormalizeBetweenMinusPiAndPi_upperBound() {
-        final double value = +Math.PI;
-        final double expected = -Math.PI;
+        final double value = PlaneAngleRadians.PI;
+        final double expected = PlaneAngleRadians.MINUS_PI;
         final double actual = PlaneAngleRadians.normalizeBetweenMinusPiAndPi(value);
         final double tol = Math.ulp(expected);
         Assertions.assertEquals(expected, actual, tol);
@@ -90,32 +131,32 @@ public class PlaneAngleRadiansTest {
 
     @Test
     public void testNormalizeBetweenZeroAndTwoPi1() {
-        final double value = 1.25 * TWO_PI;
-        final double expected = 0.25 * TWO_PI;
+        final double value = 1.25 * PlaneAngleRadians.TWO_PI;
+        final double expected = PlaneAngleRadians.PI_OVER_TWO;
         final double actual = PlaneAngleRadians.normalizeBetweenZeroAndTwoPi(value);
         final double tol = Math.ulp(expected);
         Assertions.assertEquals(expected, actual, tol);
     }
     @Test
     public void testNormalizeBetweenZeroAndTwoPi2() {
-        final double value = 1.75 * TWO_PI;
-        final double expected = 0.75 * TWO_PI;
+        final double value = 1.75 * PlaneAngleRadians.TWO_PI;
+        final double expected = PlaneAngleRadians.THREE_PI_OVER_TWO;
         final double actual = PlaneAngleRadians.normalizeBetweenZeroAndTwoPi(value);
         final double tol = Math.ulp(expected);
         Assertions.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 value = PlaneAngleRadians.MINUS_PI + 1e-10;
+        final double expected = PlaneAngleRadians.PI + 1e-10;
         final double actual = PlaneAngleRadians.normalizeBetweenZeroAndTwoPi(value);
         final double tol = Math.ulp(expected);
         Assertions.assertEquals(expected, actual, tol);
     }
     @Test
     public void testNormalizeBetweenZeroAndTwoPi4() {
-        final double value = 9 * Math.PI / 4;
-        final double expected = Math.PI / 4;
+        final double value = 9 * PlaneAngleRadians.PI / 4;
+        final double expected = PlaneAngleRadians.PI / 4;
         final double actual = PlaneAngleRadians.normalizeBetweenZeroAndTwoPi(value);
         final double tol = Math.ulp(expected);
         Assertions.assertEquals(expected, actual, tol);
@@ -131,7 +172,7 @@ public class PlaneAngleRadiansTest {
     }
     @Test
     public void testNormalizeBetweenZeroAndTwoPi_upperBound() {
-        final double value = 2.0 * Math.PI;
+        final double value = PlaneAngleRadians.TWO_PI;
         final double expected = 0.0;
         final double actual = PlaneAngleRadians.normalizeBetweenZeroAndTwoPi(value);
         final double tol = Math.ulp(expected);