commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject svn commit: r1164756 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/complex/Complex.java test/java/org/apache/commons/math/complex/ComplexTest.java
Date Fri, 02 Sep 2011 23:05:01 GMT
Author: erans
Date: Fri Sep  2 23:05:01 2011
New Revision: 1164756

URL: http://svn.apache.org/viewvc?rev=1164756&view=rev
Log:
MATH-657
Complex division by zero:
 z / 0 = INF if z is not ZERO
 0 / 0 = NaN

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/Complex.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/complex/ComplexTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/Complex.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/Complex.java?rev=1164756&r1=1164755&r2=1164756&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/Complex.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/Complex.java Fri
Sep  2 23:05:01 2011
@@ -78,6 +78,8 @@ public class Complex implements FieldEle
     private final transient boolean isNaN;
     /** Record whether this complex number is infinite. */
     private final transient boolean isInfinite;
+    /** Record whether this complex number is zero. */
+    private final transient boolean isZero;
 
     /**
      * Create a complex number given only the real part.
@@ -101,6 +103,7 @@ public class Complex implements FieldEle
         isNaN = Double.isNaN(real) || Double.isNaN(imaginary);
         isInfinite = !isNaN &&
             (Double.isInfinite(real) || Double.isInfinite(imaginary));
+        isZero = real == 0 && imaginary == 0;
     }
 
     /**
@@ -222,7 +225,10 @@ public class Complex implements FieldEle
      *  <li>If either {@code this} or {@code divisor} has a {@code NaN} value
      *   in either part, {@link #NaN} is returned.
      *  </li>
-     *  <li>If {@code divisor} equals {@link #ZERO}, {@link #NaN} is returned.
+     *  <li>If {@code this} and {@code divisor} are both {@link #ZERO},
+     *   {@link #NaN} is returned.
+     *  </li>
+     *  <li>If {@code divisor} equals {@link #ZERO}, {@link #INF} is returned.
      *  </li>
      *  <li>If {@code this} and {@code divisor} are both infinite,
      *   {@link #NaN} is returned.
@@ -249,16 +255,17 @@ public class Complex implements FieldEle
             return NaN;
         }
 
-        final double c = divisor.getReal();
-        final double d = divisor.getImaginary();
-        if (c == 0.0 && d == 0.0) {
-            return NaN;
+        if (divisor.isZero) {
+            return isZero ? NaN : INF;
         }
 
         if (divisor.isInfinite() && !isInfinite()) {
             return ZERO;
         }
 
+        final double c = divisor.getReal();
+        final double d = divisor.getImaginary();
+
         if (FastMath.abs(c) < FastMath.abs(d)) {
             double q = c / d;
             double denominator = c * q + d;
@@ -285,7 +292,7 @@ public class Complex implements FieldEle
             return NaN;
         }
         if (divisor == 0d) {
-            return NaN;
+            return isZero ? NaN : INF;
         }
         if (Double.isInfinite(divisor)) {
             return !isInfinite() ? ZERO : NaN;

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/complex/ComplexTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/complex/ComplexTest.java?rev=1164756&r1=1164755&r2=1164756&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/complex/ComplexTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/complex/ComplexTest.java
Fri Sep  2 23:05:01 2011
@@ -229,6 +229,13 @@ public class ComplexTest {
     public void testDivideZero() {
         Complex x = new Complex(3.0, 4.0);
         Complex z = x.divide(Complex.ZERO);
+        Assert.assertEquals(z, Complex.INF);
+    }
+
+    @Test
+    public void testDivideZeroZero() {
+        Complex x = new Complex(0.0, 0.0);
+        Complex z = x.divide(Complex.ZERO);
         Assert.assertEquals(z, Complex.NaN);
     }
 
@@ -349,13 +356,13 @@ public class ComplexTest {
 
     @Test
     public void testScalarMultiplyInf() {
-        Complex x = new Complex(1,1);
+        Complex x = new Complex(1, 1);
         double yDouble = Double.POSITIVE_INFINITY;
         Complex yComplex = new Complex(yDouble);
         Assert.assertEquals(x.multiply(yComplex), x.multiply(yDouble));
         
         yDouble = Double.NEGATIVE_INFINITY;
-         yComplex = new Complex(yDouble);
+        yComplex = new Complex(yDouble);
         Assert.assertEquals(x.multiply(yComplex), x.multiply(yDouble));
     }
 
@@ -565,9 +572,16 @@ public class ComplexTest {
     }
 
     @Test
+    public void testAtanI() {
+        for (int i = -10; i < 10; i++) {
+            System.out.println(new Complex(0, 1 - 0.1 * i).atan());
+        }
+        Assert.assertTrue(Complex.I.atan().isInfinite());
+    }
+
+    @Test
     public void testAtanNaN() {
         Assert.assertTrue(Complex.NaN.atan().isNaN());
-        Assert.assertTrue(Complex.I.atan().isNaN());
     }
 
     @Test



Mime
View raw message