You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by br...@apache.org on 2005/08/17 07:12:53 UTC

svn commit: r233126 - in /jakarta/commons/proper/math/branches/MATH_1_1: src/java/org/apache/commons/math/complex/Complex.java xdocs/changes.xml

Author: brentworden
Date: Tue Aug 16 22:12:34 2005
New Revision: 233126

URL: http://svn.apache.org/viewcvs?rev=233126&view=rev
Log:
PR 36205: Added better handling of numerical overflow and division by zero in Complex calculations.

Modified:
    jakarta/commons/proper/math/branches/MATH_1_1/src/java/org/apache/commons/math/complex/Complex.java
    jakarta/commons/proper/math/branches/MATH_1_1/xdocs/changes.xml

Modified: jakarta/commons/proper/math/branches/MATH_1_1/src/java/org/apache/commons/math/complex/Complex.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/math/branches/MATH_1_1/src/java/org/apache/commons/math/complex/Complex.java?rev=233126&r1=233125&r2=233126&view=diff
==============================================================================
--- jakarta/commons/proper/math/branches/MATH_1_1/src/java/org/apache/commons/math/complex/Complex.java (original)
+++ jakarta/commons/proper/math/branches/MATH_1_1/src/java/org/apache/commons/math/complex/Complex.java Tue Aug 16 22:12:34 2005
@@ -66,7 +66,19 @@
         if (isNaN()) {
             return Double.NaN;
         }
-        return Math.sqrt(squareSum());       
+        if (Math.abs(real) < Math.abs(imaginary)) {
+            if (imaginary == 0.0) {
+                return Math.abs(real);
+            }
+            double q = real / imaginary;
+            return (Math.abs(imaginary) * Math.sqrt(1 + q*q));
+        } else {
+            if (real == 0.0) {
+                return Math.abs(imaginary);
+            }
+            double q = imaginary / real;
+            return (Math.abs(real) * Math.sqrt(1 + q*q));
+        }
     }
     
     /**
@@ -108,17 +120,29 @@
         if (isNaN() || rhs.isNaN()) {
             return NaN;
         }
-        
-        if (Math.abs(rhs.getReal()) < Math.abs(rhs.getImaginary())) {
-            double q = rhs.getReal() / rhs.getImaginary();
-            double d = (rhs.getReal() * q) + rhs.getImaginary();
-            return new Complex(((real * q) + imaginary) / d,
-                ((imaginary * q) - real) / d);
+
+        double c = rhs.getReal();
+        double d = rhs.getImaginary();
+        if (c == 0.0 && d == 0.0) {
+            throw new ArithmeticException("Error: division by zero.");
+        }
+
+        if (Math.abs(c) < Math.abs(d)) {
+            if (d == 0.0) {
+                return new Complex(real/c, imaginary/c);
+            }
+            double q = c / d;
+            double denominator = c * q + d;
+            return new Complex((real * q + imaginary) / denominator,
+                (imaginary * q - real) / denominator);
         } else {
-            double q = rhs.getImaginary() / rhs.getReal();
-            double d = (rhs.getImaginary() * q) + rhs.getReal();
-            return new Complex(((imaginary * q) + real) / d,
-                (imaginary - (real * q)) / d);
+            if (c == 0.0) {
+                return new Complex(imaginary/d, -real/c);
+            }
+            double q = d / c;
+            double denominator = d * q + c;
+            return new Complex((imaginary * q + real) / denominator,
+                (imaginary - real * q) / denominator);
         }
     }
     
@@ -213,15 +237,6 @@
         }
         
         return new Complex(-real, -imaginary);
-    }
-    
-    /**
-     * Return the sum of the squared terms.
-     *
-     * @return the square sum.
-     */
-    private double squareSum() {
-        return real * real + imaginary * imaginary;
     }
     
     /**

Modified: jakarta/commons/proper/math/branches/MATH_1_1/xdocs/changes.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/math/branches/MATH_1_1/xdocs/changes.xml?rev=233126&r1=233125&r2=233126&view=diff
==============================================================================
--- jakarta/commons/proper/math/branches/MATH_1_1/xdocs/changes.xml (original)
+++ jakarta/commons/proper/math/branches/MATH_1_1/xdocs/changes.xml Tue Aug 16 22:12:34 2005
@@ -45,6 +45,10 @@
        and numerical utilities, and a PRNG pluggability framework making it
        possible to replace the JDK-supplied random number generator in
        commons-math (and elsewhere) with alternative PRNG implementations.">
+	  <action dev="brentworden" type="fix" issue="36205" due-to="Xiaogang Zhang">
+		Added better handling of numerical overflow and division by zero in
+		Complex calculations.
+	  </action>
 	  <action dev="brentworden" type="fix" issue="36105" due-to="Mikael Weigelt">
 		Changed ContinuedFraction to better handle infinite convergents that
 		resulted in divergent continued fraction evaluations.



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org