asfgit closed pull request #21: NUMBERS-84: Adding NaN and infinite checks to 
Quaternion
URL: https://github.com/apache/commons-numbers/pull/21
 
 
   

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/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 7d555fb8..e5c831ba 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
@@ -44,7 +44,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 = "[";
@@ -379,7 +379,8 @@ 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() {
         switch (type) {
@@ -389,8 +390,8 @@ public Quaternion normalize() {
         case DEFAULT:
             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);
@@ -517,7 +518,8 @@ 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() {
         switch (type) {
@@ -526,8 +528,8 @@ public Quaternion inverse() {
             return new Quaternion(type, w, -x, -y, -z);
         case DEFAULT:
             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 aa89b1d0..b2cb39c9 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,9 +332,27 @@ public final void testNormalize() {
     }
 
     @Test(expected=IllegalStateException.class)
-    public final void testNormalizeFail() {
-        final Quaternion zeroQ = Quaternion.of(0, 0, 0, 0);
-        zeroQ.normalize();
+    public final void testNormalizeFail_zero() {
+        final Quaternion q = Quaternion.of(0, 0, 0, 0);
+        q.normalize();
+    }
+
+    @Test(expected=IllegalStateException.class)
+    public final void testNormalizeFail_nan() {
+        final Quaternion q = Quaternion.of(0, 0, 0, Double.NaN);
+        q.normalize();
+    }
+
+    @Test(expected=IllegalStateException.class)
+    public final void testNormalizeFail_positiveInfinity() {
+        final Quaternion q = Quaternion.of(0, 0, Double.POSITIVE_INFINITY, 0);
+        q.normalize();
+    }
+
+    @Test(expected=IllegalStateException.class)
+    public final void testNormalizeFail_negativeInfinity() {
+        final Quaternion q = Quaternion.of(0, Double.NEGATIVE_INFINITY, 0, 0);
+        q.normalize();
     }
 
     @Test
@@ -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();


 

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to