You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by bu...@apache.org on 2005/08/16 19:21:20 UTC
DO NOT REPLY [Bug 36205] New: -
[math][patch] Update Complex.java
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG�
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=36205>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND�
INSERTED IN THE BUG DATABASE.
http://issues.apache.org/bugzilla/show_bug.cgi?id=36205
Summary: [math][patch] Update Complex.java
Product: Commons
Version: unspecified
Platform: All
OS/Version: All
Status: NEW
Severity: normal
Priority: P1
Component: Math
AssignedTo: commons-dev@jakarta.apache.org
ReportedBy: zxg_32@yahoo.com
Index: Complex.java
===================================================================
--- Complex.java (revision 233018)
+++ Complex.java (working copy)
@@ -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);
}
}
--
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org