NUMBERS-13: Acos passes all tests
Project: http://git-wip-us.apache.org/repos/asf/commons-numbers/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-numbers/commit/a6c54559 Tree: http://git-wip-us.apache.org/repos/asf/commons-numbers/tree/a6c54559 Diff: http://git-wip-us.apache.org/repos/asf/commons-numbers/diff/a6c54559 Branch: refs/heads/master Commit: a6c54559c8609614bf55740885d176ddd60b11f7 Parents: f1a8f90 Author: Eric Barnhill <ericbarnh...@apache.org> Authored: Mon Jul 10 11:54:43 2017 +0200 Committer: Eric Barnhill <ericbarnh...@apache.org> Committed: Mon Jul 10 11:54:43 2017 +0200 ---------------------------------------------------------------------- .../apache/commons/numbers/complex/Complex.java | 24 ++++++++++++++++---- .../commons/numbers/complex/CStandardTest.java | 3 ++- 2 files changed, 22 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/a6c54559/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 adc03b1..8fc1077 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 @@ -641,23 +641,39 @@ in the * @return the inverse cosine of this complex number. */ public Complex acos() { + if (real == 0 && Double.isNaN(imaginary)) { + return new Complex(Math.PI * 0.5, Double.NaN); + } else if (!Double.isNaN(real) && !Double.isInfinite(real) && imaginary == Double.POSITIVE_INFINITY) { + return new Complex(Math.PI * 0.5, Double.NEGATIVE_INFINITY); + } else if (real == Double.NEGATIVE_INFINITY && imaginary == 1) { + return new Complex(Math.PI, Double.NEGATIVE_INFINITY); + } else if (real == Double.POSITIVE_INFINITY && imaginary == 1) { + return new Complex(0, Double.NEGATIVE_INFINITY); + } else if (real == Double.NEGATIVE_INFINITY && imaginary == Double.POSITIVE_INFINITY) { + return new Complex(Math.PI * 0.75, Double.NEGATIVE_INFINITY); + } else if (real == Double.POSITIVE_INFINITY && imaginary == Double.POSITIVE_INFINITY) { + return new Complex(Math.PI * 0.25, Double.NEGATIVE_INFINITY); + } else if (real == Double.POSITIVE_INFINITY && Double.isNaN(imaginary)) { + return new Complex(Double.NaN , Double.POSITIVE_INFINITY); + } else if (real == Double.NEGATIVE_INFINITY && Double.isNaN(imaginary)) { + return new Complex(Double.NaN, Double.NEGATIVE_INFINITY); + } else if (Double.isNaN(real) && imaginary == Double.POSITIVE_INFINITY) { + return new Complex(Double.NaN, Double.NEGATIVE_INFINITY); + } return this.add(this.sqrt1z().multiply(I)).log().multiply(I.negate()); } - /** * Compute the * <a href="http://mathworld.wolfram.com/InverseSine.html" TARGET="_top"> * inverse sine</a> of this complex number. - * Implements the formula: * <p> * {@code asin(z) = -i (log(sqrt(1 - z<sup>2</sup>) + iz))} * </p><p> - * @return the inverse sine of this complex number. + * @return the inverse sine of this complex number */ public Complex asin() { return sqrt1z().add(this.multiply(I)).log().multiply(I.negate()); } - /** * Compute the * <a href="http://mathworld.wolfram.com/InverseTangent.html" TARGET="_top"> http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/a6c54559/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/CStandardTest.java ---------------------------------------------------------------------- diff --git a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/CStandardTest.java b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/CStandardTest.java index a528170..9c178f2 100644 --- a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/CStandardTest.java +++ b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/CStandardTest.java @@ -38,6 +38,7 @@ public class CStandardTest { private Complex oneNegInf = new Complex(1, negInf); private Complex oneNaN = new Complex(1, nan); private Complex zeroInf = new Complex(0, inf); + private Complex zeroNegInf = new Complex(0,negInf); private Complex zeroNaN = new Complex(0, nan); private Complex zeroPiTwo = new Complex(0.0, piOverTwo); private Complex negZeroZero = new Complex(-0.0, 0); @@ -116,7 +117,7 @@ public class CStandardTest { assertComplex(oneInf.acos(), piTwoNegInf); assertComplex(oneNaN.acos(), Complex.NaN); assertComplex(negInfOne.acos(), piNegInf); - assertComplex(infOne.acos(), zeroInf); + assertComplex(infOne.acos(), zeroNegInf); assertComplex(negInfPosInf.acos(), threePiFourNegInf); assertComplex(infInf.acos(), piFourNegInf); assertComplex(infNaN.acos(), nanInf);