From c09247c09bae47a842a638c6704876dec3da9cd8 Mon Sep 17 00:00:00 2001 From: Phil Steitz Date: Tue, 27 Oct 2009 01:32:19 +0000 Subject: [PATCH] Removed dead code, improved test coverage. Dead code pointed out in JIRA: MATH-306. Thanks to Joerg Huber. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@830044 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/commons/math/complex/Complex.java | 8 +------- src/site/xdoc/changes.xml | 3 +++ .../commons/math/complex/ComplexTest.java | 19 +++++++++++++++++++ 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/apache/commons/math/complex/Complex.java b/src/main/java/org/apache/commons/math/complex/Complex.java index 746c54901..7bc354a28 100644 --- a/src/main/java/org/apache/commons/math/complex/Complex.java +++ b/src/main/java/org/apache/commons/math/complex/Complex.java @@ -177,7 +177,7 @@ public class Complex implements FieldElement, Serializable { *

      *    a + bi          ac + bd + (bc - ad)i
      *    ----------- = -------------------------
-     *    c + di               c2 + d2
+     *    c + di         c2 + d2
      * 
* but uses * @@ -221,17 +221,11 @@ public class Complex implements FieldElement, Serializable { } if (Math.abs(c) < Math.abs(d)) { - if (d == 0.0) { - return createComplex(real/c, imaginary/c); - } double q = c / d; double denominator = c * q + d; return createComplex((real * q + imaginary) / denominator, (imaginary * q - real) / denominator); } else { - if (c == 0.0) { - return createComplex(imaginary/d, -real/c); - } double q = d / c; double denominator = d * q + c; return createComplex((imaginary * q + real) / denominator, diff --git a/src/site/xdoc/changes.xml b/src/site/xdoc/changes.xml index 511037a2b..d3937e1b2 100644 --- a/src/site/xdoc/changes.xml +++ b/src/site/xdoc/changes.xml @@ -39,6 +39,9 @@ The type attribute can be add,update,fix,remove. + + Removed dead code from Complex#divide. + Fixed implementation of RandomDataImpl#nextPoisson by implementing an alternative algorithm for large means. diff --git a/src/test/java/org/apache/commons/math/complex/ComplexTest.java b/src/test/java/org/apache/commons/math/complex/ComplexTest.java index cd9b15ec9..e78ada46c 100644 --- a/src/test/java/org/apache/commons/math/complex/ComplexTest.java +++ b/src/test/java/org/apache/commons/math/complex/ComplexTest.java @@ -144,6 +144,19 @@ public class ComplexTest extends TestCase { assertEquals(39.0 / 61.0, z.getReal(), 1.0e-5); assertEquals(2.0 / 61.0, z.getImaginary(), 1.0e-5); } + + public void testDivideReal() { + Complex x = new Complex(2d, 3d); + Complex y = new Complex(2d, 0d); + assertEquals(new Complex(1d, 1.5), x.divide(y)); + + } + + public void testDivideImaginary() { + Complex x = new Complex(2d, 3d); + Complex y = new Complex(0d, 2d); + assertEquals(new Complex(1.5d, -1d), x.divide(y)); + } public void testDivideInfinite() { Complex x = new Complex(3, 4); @@ -164,6 +177,12 @@ public class ComplexTest extends TestCase { assertTrue(Double.isNaN(z.getReal())); assertTrue(Double.isNaN(z.getImaginary())); } + + public void testDivideZero() { + Complex x = new Complex(3.0, 4.0); + Complex z = x.divide(Complex.ZERO); + assertEquals(z, Complex.NaN); + } public void testDivideNaN() { Complex x = new Complex(3.0, 4.0);