NUMBERS-22: if block added to ensure that reciprocal of very small real numbers return an imaginary component of 0 rather than NaN
Project: http://git-wip-us.apache.org/repos/asf/commons-numbers/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-numbers/commit/f0d1b9cd Tree: http://git-wip-us.apache.org/repos/asf/commons-numbers/tree/f0d1b9cd Diff: http://git-wip-us.apache.org/repos/asf/commons-numbers/diff/f0d1b9cd Branch: refs/heads/master Commit: f0d1b9cdc279c5438a799d62b74effb7ece4958b Parents: ec3bb9e Author: Eric Barnhill <ericbarnh...@apache.org> Authored: Thu Sep 7 15:21:28 2017 +0200 Committer: Eric Barnhill <ericbarnh...@apache.org> Committed: Thu Sep 7 15:21:28 2017 +0200 ---------------------------------------------------------------------- .../org/apache/commons/numbers/complex/Complex.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/f0d1b9cd/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/Complex.java ---------------------------------------------------------------------- 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 7fcbbf4..d396619 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 @@ -355,11 +355,23 @@ in the if (Math.abs(real) < Math.abs(imaginary)) { double q = real / imaginary; double scale = 1. / (real * q + imaginary); - return new Complex(scale * q, -scale); + double scaleQ; + if (q == 0 | scale == 0) { + scaleQ = 0; + } else { + scaleQ = scale * q; + } + return new Complex(scaleQ, -scale); } else { double q = imaginary / real; double scale = 1. / (imaginary * q + real); - return new Complex(scale, -scale * q); + double scaleQ; + if (q == 0 | scale == 0) { + scaleQ = 0; + } else { + scaleQ = scale * q; + } + return new Complex(scale, -scaleQ); } }