You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2019/12/20 17:58:19 UTC
[commons-numbers] 28/30: Simplify exp() edge cases.
This is an automated email from the ASF dual-hosted git repository.
aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git
commit e2e7b8b9fba25f181d1254f41db189a4543b480b
Author: aherbert <ah...@apache.org>
AuthorDate: Fri Dec 20 16:47:45 2019 +0000
Simplify exp() edge cases.
---
.../apache/commons/numbers/complex/Complex.java | 65 +++++++++-------------
1 file changed, 27 insertions(+), 38 deletions(-)
diff --git a/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java b/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java
index 7c9d962..bc91838 100644
--- a/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java
+++ b/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java
@@ -1997,48 +1997,37 @@ public final class Complex implements Serializable {
* @see <a href="http://functions.wolfram.com/ElementaryFunctions/Exp/">Exp</a>
*/
public Complex exp() {
- if (Double.isFinite(real)) {
- if (Double.isFinite(imaginary)) {
- final double expReal = Math.exp(real);
- if (imaginary == 0) {
- // Preserve sign for conjugate equality
- return new Complex(expReal, imaginary);
- }
- return new Complex(expReal * Math.cos(imaginary),
- expReal * Math.sin(imaginary));
- }
- // Imaginary is infinite or nan
- return NAN;
- }
+ // Set the values used to compute exp(real) * cis(im)
+ double expReal;
+ double im = imaginary;
if (Double.isInfinite(real)) {
- if (Double.isFinite(imaginary)) {
- if (real == Double.POSITIVE_INFINITY) {
- if (imaginary == 0) {
- return this;
- }
- // inf * cis(y)
- final double re = Double.POSITIVE_INFINITY * Math.cos(imaginary);
- final double im = Double.POSITIVE_INFINITY * Math.sin(imaginary);
- return new Complex(re, im);
+ if (real < 0) {
+ expReal = 0;
+ if (!Double.isFinite(im)) {
+ // Preserve conjugate equality
+ im = Math.copySign(1, im);
}
- // +0 * cis(y)
- final double re = 0.0 * Math.cos(imaginary);
- final double im = 0.0 * Math.sin(imaginary);
- return new Complex(re, im);
- }
- // imaginary is infinite or NaN
- if (real == Double.POSITIVE_INFINITY) {
- return new Complex(Double.POSITIVE_INFINITY, Double.NaN);
+ } else {
+ if (im == 0 || !Double.isFinite(im)){
+ return Double.isInfinite(im) ?
+ new Complex(real, Double.NaN) :
+ this;
+ }
+ expReal = real;
}
- // Preserve sign for conjugate equality
- return new Complex(0, Math.copySign(0, imaginary));
- }
- // real is NaN
- if (imaginary == 0) {
- return new Complex(Double.NaN, Math.copySign(0, imaginary));
+ } else if (imaginary == 0) {
+ // Real-only number
+ return Double.isNaN(real) ?
+ this :
+ new Complex(Math.exp(real), imaginary);
+ } else if (Double.isNaN(real)) {
+ return NAN;
+ } else {
+ // real is finite
+ expReal = Math.exp(real);
}
- // optionally raises the ‘‘invalid’’ floating-point exception, for finite y.
- return NAN;
+ return new Complex(expReal * Math.cos(im),
+ expReal * Math.sin(im));
}
/**