You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@commons.apache.org by GitBox <gi...@apache.org> on 2018/12/14 12:36:28 UTC

[GitHub] darkma773r closed pull request #20: NUMBERS-84: Checks for NaN and Infinite in Quaternion Normalization

darkma773r closed pull request #20: NUMBERS-84: Checks for NaN and Infinite in Quaternion Normalization
URL: https://github.com/apache/commons-numbers/pull/20
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/.travis.yml b/.travis.yml
index 9db916b8..5a3ba35c 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -17,7 +17,6 @@ language: java
 sudo: false
 
 jdk:
-  - openjdk7
   - oraclejdk8
 
 after_success:
diff --git a/commons-numbers-quaternion/src/main/java/org/apache/commons/numbers/quaternion/Quaternion.java b/commons-numbers-quaternion/src/main/java/org/apache/commons/numbers/quaternion/Quaternion.java
index 25badc12..ed87900e 100644
--- a/commons-numbers-quaternion/src/main/java/org/apache/commons/numbers/quaternion/Quaternion.java
+++ b/commons-numbers-quaternion/src/main/java/org/apache/commons/numbers/quaternion/Quaternion.java
@@ -42,7 +42,7 @@
     /** Serializable version identifier. */
     private static final long serialVersionUID = 20170118L;
     /** Error message. */
-    private static final String ZERO_NORM_MSG = "Norm is zero";
+    private static final String ILLEGAL_NORM_MSG = "Illegal norm: ";
 
     /** {@link #toString() String representation}. */
     private static final String FORMAT_START = "[";
@@ -274,13 +274,14 @@ public double normSq() {
      * The norm of the quaternion must not be near zero.
      *
      * @return a normalized quaternion.
-     * @throws IllegalStateException if the norm of the quaternion is near zero.
+     * @throws IllegalStateException if the norm of the quaternion is NaN, infinite,
+     *      or near zero
      */
     public Quaternion normalize() {
         final double norm = norm();
 
-        if (norm < Precision.SAFE_MIN) {
-            throw new IllegalStateException(ZERO_NORM_MSG);
+        if (!Double.isFinite(norm) || norm < Precision.SAFE_MIN) {
+            throw new IllegalStateException(ILLEGAL_NORM_MSG + norm);
         }
 
         final Quaternion unit = divide(norm);
@@ -389,12 +390,13 @@ public Quaternion negate() {
      * The norm of the quaternion must not be zero.
      *
      * @return the inverse.
-     * @throws IllegalArgumentException if the norm (squared) of the quaternion is zero.
+     * @throws IllegalStateException if the norm (squared) of the quaternion is NaN, infinite,
+     *      or near zero
      */
     public Quaternion inverse() {
         final double squareNorm = normSq();
-        if (squareNorm < Precision.SAFE_MIN) {
-            throw new IllegalStateException(ZERO_NORM_MSG);
+        if (!Double.isFinite(squareNorm) || squareNorm < Precision.SAFE_MIN) {
+            throw new IllegalStateException(ILLEGAL_NORM_MSG + Math.sqrt(squareNorm));
         }
 
         return of(w / squareNorm,
diff --git a/commons-numbers-quaternion/src/test/java/org/apache/commons/numbers/quaternion/QuaternionTest.java b/commons-numbers-quaternion/src/test/java/org/apache/commons/numbers/quaternion/QuaternionTest.java
index caa202d3..2a174681 100644
--- a/commons-numbers-quaternion/src/test/java/org/apache/commons/numbers/quaternion/QuaternionTest.java
+++ b/commons-numbers-quaternion/src/test/java/org/apache/commons/numbers/quaternion/QuaternionTest.java
@@ -332,11 +332,29 @@ public final void testNormalize() {
     }
 
     @Test(expected=IllegalStateException.class)
-    public final void testNormalizeFail() {
+    public final void testNormalizeFail_zero() {
         final Quaternion zeroQ = Quaternion.of(0, 0, 0, 0);
         zeroQ.normalize();
     }
 
+    @Test(expected=IllegalStateException.class)
+    public final void testNormalizeFail_nan() {
+        final Quaternion zeroQ = Quaternion.of(0, 0, 0, Double.NaN);
+        zeroQ.normalize();
+    }
+
+    @Test(expected=IllegalStateException.class)
+    public final void testNormalizeFail_positiveInfinity() {
+        final Quaternion zeroQ = Quaternion.of(0, 0, Double.POSITIVE_INFINITY, 0);
+        zeroQ.normalize();
+    }
+
+    @Test(expected=IllegalStateException.class)
+    public final void testNormalizeFail_negativeInfinity() {
+        final Quaternion zeroQ = Quaternion.of(0, Double.NEGATIVE_INFINITY, 0, 0);
+        zeroQ.normalize();
+    }
+
     @Test
     public final void testObjectEquals() {
         final double one = 1;
@@ -540,6 +558,30 @@ public final void testInverse() {
         }
     }
 
+    @Test(expected=IllegalStateException.class)
+    public void testInverse_zeroNorm() {
+        Quaternion q = Quaternion.of(0, 0, 0, 0);
+        q.inverse();
+    }
+
+    @Test(expected=IllegalStateException.class)
+    public void testInverse_nanNorm() {
+        Quaternion q = Quaternion.of(Double.NaN, 0, 0, 0);
+        q.inverse();
+    }
+
+    @Test(expected=IllegalStateException.class)
+    public void testInverse_positiveInfinityNorm() {
+        Quaternion q = Quaternion.of(0, Double.POSITIVE_INFINITY, 0, 0);
+        q.inverse();
+    }
+
+    @Test(expected=IllegalStateException.class)
+    public void testInverse_negativeInfinityNorm() {
+        Quaternion q = Quaternion.of(0, 0, Double.NEGATIVE_INFINITY, 0);
+        q.inverse();
+    }
+
     @Test
     public void testInverseNormalized() {
         final Quaternion invQ = Quaternion.of(-1.2, 3.4, -5.6, -7.8).normalize().inverse();
diff --git a/pom.xml b/pom.xml
index 29d8de20..0145aa80 100644
--- a/pom.xml
+++ b/pom.xml
@@ -127,8 +127,8 @@
     <commons.jira.id>NUMBERS</commons.jira.id>
     <commons.jira.pid>12320720</commons.jira.pid>
     <commons.encoding>UTF-8</commons.encoding>
-    <maven.compiler.source>1.6</maven.compiler.source>
-    <maven.compiler.target>1.6</maven.compiler.target>
+    <maven.compiler.source>1.8</maven.compiler.source>
+    <maven.compiler.target>1.8</maven.compiler.target>
     <numbers.pmd.version>3.9.0</numbers.pmd.version>
     <numbers.checkstyle.version>3.0.0</numbers.checkstyle.version>
     <numbers.mathjax.version>2.7.2</numbers.mathjax.version>


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services