Repository: commons-numbers Updated Branches: refs/heads/remove-nan-returns [created] 2f1c0b5dc
NUMBERS-60: NaN descriptions in javadoc altered to reflect C++ standards. NaN changed to 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/2f1c0b5d Tree: http://git-wip-us.apache.org/repos/asf/commons-numbers/tree/2f1c0b5d Diff: http://git-wip-us.apache.org/repos/asf/commons-numbers/diff/2f1c0b5d Branch: refs/heads/remove-nan-returns Commit: 2f1c0b5dc250b34c2e7d4a7844c76d67e5ce2b2e Parents: b077fd5 Author: Eric Barnhill <ericbarnh...@apache.org> Authored: Mon Feb 12 13:15:12 2018 +0100 Committer: Eric Barnhill <ericbarnh...@apache.org> Committed: Mon Feb 12 13:15:12 2018 +0100 ---------------------------------------------------------------------- .../apache/commons/numbers/complex/Complex.java | 43 +++++--------- .../commons/numbers/complex/ComplexUtils.java | 2 +- .../commons/numbers/complex/CStandardTest.java | 60 ++++++++++---------- .../commons/numbers/complex/ComplexTest.java | 22 +++---- .../numbers/complex/ComplexUtilsTest.java | 14 ++--- 5 files changed, 63 insertions(+), 78 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/2f1c0b5d/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 f3295c3..d7c9d59 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 @@ -49,7 +49,7 @@ public class Complex implements Serializable { public static final Complex I = new Complex(0, 1); // CHECKSTYLE: stop ConstantName /** A complex number representing "NaN + NaNi" */ - public static final Complex NaN = new Complex(Double.NaN, Double.NaN); + public static final Complex NAN = new Complex(Double.NaN, Double.NaN); // CHECKSTYLE: resume ConstantName /** A complex number representing "+INF + INFi" */ public static final Complex INF = new Complex(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY); @@ -88,10 +88,7 @@ public class Complex implements Serializable { /** * Creates a Complex from its polar representation. - * <p> - * If either {@code r} or {@code theta} is NaN, or {@code theta} is - * infinite, {@link Complex#NaN} is returned. - * <p> + * * If {@code r} is infinite and {@code theta} is finite, infinite or NaN * values may be returned in parts of the result, following the rules for * double arithmetic. @@ -177,10 +174,9 @@ public class Complex implements Serializable { /** * Return the absolute value of this complex number. - * Returns {@code NaN} if either real or imaginary part is {@code NaN} - * and {@code Double.POSITIVE_INFINITY} if neither part is {@code NaN}, - * but at least one part is infinite. - * This code follows the <a href="http://www.iso-9899.info/wiki/The_Standard">ISO C Standard</a>, Annex G, in calculating the returned value (i.e. the hypot(x,y) method) + * This code follows the <a href="http://www.iso-9899.info/wiki/The_Standard">ISO C Standard</a>, Annex G, + * in calculating the returned value (i.e. the hypot(x,y) method) + * and in handling of NaNs. * * @return the absolute value. */ @@ -276,10 +272,10 @@ public class Complex implements Serializable { * {@code Infinite} and {@code NaN} values are handled according to the * following rules, applied in the order presented: * <ul> - * <li>If {@code divisor} equals {@link #ZERO}, {@link #NaN} is returned. + * <li>If {@code divisor} equals {@link #ZERO}, {@link #NAN} is returned. * </li> * <li>If {@code this} and {@code divisor} are both infinite, - * {@link #NaN} is returned. + * {@link #NAN} is returned. * </li> * <li>If {@code this} is finite (i.e., has no {@code Infinite} or * {@code NaN} parts) and {@code divisor} is infinite (one or both parts @@ -301,7 +297,7 @@ public class Complex implements Serializable { final double d = divisor.imaginary; if (c == 0 && d == 0) { - return NaN; + return NAN; } if ((Double.isInfinite(c) || @@ -334,11 +330,11 @@ public class Complex implements Serializable { */ public Complex divide(double divisor) { if (divisor == 0d) { - return NaN; + return NAN; } if (Double.isInfinite(divisor)) { return !(Double.isInfinite(real) || - Double.isInfinite(imaginary)) ? ZERO : NaN; + Double.isInfinite(imaginary)) ? ZERO : NAN; } return new Complex(real / divisor, imaginary / divisor); @@ -549,19 +545,10 @@ public class Complex implements Serializable { /** * Returns a {@code Complex} whose value is {@code this * factor}. - * Implements preliminary checks for {@code NaN} and infinity followed by - * the definitional formula: + * Implements the definitional formula: * * {@code (a + bi)(c + di) = (ac - bd) + (ad + bc)i} * - * Returns {@link #NaN} if either {@code this} or {@code factor} has one or - * more {@code NaN} parts. - * - * Returns {@link #INF} if neither {@code this} nor {@code factor} has one - * or more {@code NaN} parts and if either {@code this} or {@code factor} - * has one or more infinite parts (same result is returned regardless of - * the sign of the components). - * * Returns finite values in components of the result per the definitional * formula in all remaining cases. * @@ -599,8 +586,6 @@ public class Complex implements Serializable { /** * Returns a {@code Complex} whose value is {@code (-this)}. - * Returns {@code NaN} if either real or imaginary - * part of this complex number is {@code Double.NaN}. * * @return {@code -this}. */ @@ -965,7 +950,7 @@ public class Complex implements Serializable { return ZERO; } else { // 0 raised to anything else is NaN - return NaN; + return NAN; } } return log().multiply(x).exp(); @@ -986,7 +971,7 @@ public class Complex implements Serializable { return ZERO; } else { // 0 raised to anything else is NaN - return NaN; + return NAN; } } return log().multiply(x).exp(); @@ -1226,7 +1211,7 @@ public class Complex implements Serializable { * {@link #getArgument() argument} of this complex number. * <p> * If one or both parts of this complex number is NaN, a list with just - * one element, {@link #NaN} is returned. + * one element, {@link #NAN} is returned. * if neither part is NaN, but at least one part is infinite, the result * is a one-element list containing {@link #INF}. * http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/2f1c0b5d/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/ComplexUtils.java ---------------------------------------------------------------------- diff --git a/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/ComplexUtils.java b/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/ComplexUtils.java index 3fdd10f..b9f758a 100644 --- a/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/ComplexUtils.java +++ b/commons-numbers-complex/src/main/java/org/apache/commons/numbers/complex/ComplexUtils.java @@ -31,7 +31,7 @@ public class ComplexUtils { * Creates a complex number from the given polar representation. * <p> * If either {@code r} or {@code theta} is NaN, or {@code theta} is - * infinite, {@link Complex#NaN} is returned. + * infinite, {@link Complex#NAN} is returned. * <p> * If {@code r} is infinite and {@code theta} is finite, infinite or NaN * values may be returned in parts of the result, following the rules for http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/2f1c0b5d/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 ed0bf4f..e79ce9f 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 @@ -115,16 +115,16 @@ public class CStandardTest { assertComplex(negZeroZero.acos(), piTwoNegZero); assertComplex(zeroNaN.acos(), piTwoNaN); assertComplex(oneInf.acos(), piTwoNegInf); - assertComplex(oneNaN.acos(), Complex.NaN); + assertComplex(oneNaN.acos(), Complex.NAN); assertComplex(negInfOne.acos(), piNegInf); assertComplex(infOne.acos(), zeroNegInf); assertComplex(negInfPosInf.acos(), threePiFourNegInf); assertComplex(infInf.acos(), piFourNegInf); assertComplex(infNaN.acos(), nanInf); assertComplex(negInfNaN.acos(), nanNegInf); - assertComplex(nanOne.acos(), Complex.NaN); + assertComplex(nanOne.acos(), Complex.NAN); assertComplex(nanInf.acos(), nanNegInf); - assertComplex(Complex.NaN.acos(), Complex.NaN); + assertComplex(Complex.NAN.acos(), Complex.NAN); } /** @@ -136,14 +136,14 @@ public class CStandardTest { assertComplex(oneOne.conj().asinh(), oneOne.asinh().conj()); assertComplex(Complex.ZERO.asinh(), Complex.ZERO); assertComplex(oneInf.asinh(), infPiTwo); - assertComplex(oneNaN.asinh(), Complex.NaN); + assertComplex(oneNaN.asinh(), Complex.NAN); assertComplex(infOne.asinh(), infZero); assertComplex(infInf.asinh(), infPiFour); assertComplex(infNaN.asinh(), infNaN); assertComplex(nanZero.asinh(), nanZero); - assertComplex(nanOne.asinh(), Complex.NaN); + assertComplex(nanOne.asinh(), Complex.NAN); assertComplex(nanInf.asinh(), infNaN); - assertComplex(Complex.NaN, Complex.NaN); + assertComplex(Complex.NAN, Complex.NAN); } /** @@ -156,13 +156,13 @@ public class CStandardTest { assertComplex(zeroNaN.atanh(), zeroNaN); assertComplex(oneZero.atanh(), infZero); assertComplex(oneInf.atanh(),zeroPiTwo); - assertComplex(oneNaN.atanh(), Complex.NaN); + assertComplex(oneNaN.atanh(), Complex.NAN); assertComplex(infOne.atanh(), zeroPiTwo); assertComplex(infInf.atanh(), zeroPiTwo); assertComplex(infNaN.atanh(), zeroNaN); - assertComplex(nanOne.atanh(), Complex.NaN); + assertComplex(nanOne.atanh(), Complex.NAN); assertComplex(nanInf.atanh(), zeroPiTwo); - assertComplex(Complex.NaN.atanh(), Complex.NaN); + assertComplex(Complex.NAN.atanh(), Complex.NAN); } /** @@ -174,8 +174,8 @@ public class CStandardTest { assertComplex(Complex.ZERO.cosh(), Complex.ONE); assertComplex(zeroInf.cosh(), nanZero); assertComplex(zeroNaN.cosh(), nanZero); - assertComplex(oneInf.cosh(), Complex.NaN); - assertComplex(oneNaN.cosh(), Complex.NaN); + assertComplex(oneInf.cosh(), Complex.NAN); + assertComplex(oneNaN.cosh(), Complex.NAN); assertComplex(infZero.cosh(), infZero); // the next test does not appear to make sense: // (inf + iy) = inf + cis(y) @@ -183,8 +183,8 @@ public class CStandardTest { assertComplex(infInf.cosh(), infNaN); assertComplex(infNaN.cosh(), infNaN); assertComplex(nanZero.cosh(), nanZero); - assertComplex(nanOne.cosh(), Complex.NaN); - assertComplex(Complex.NaN.cosh(), Complex.NaN); + assertComplex(nanOne.cosh(), Complex.NAN); + assertComplex(Complex.NAN.cosh(), Complex.NAN); } /** @@ -196,15 +196,15 @@ public class CStandardTest { assertComplex(Complex.ZERO.sinh(), Complex.ZERO); assertComplex(zeroInf.sinh(), zeroNaN); assertComplex(zeroNaN.sinh(), zeroNaN); - assertComplex(oneInf.sinh(), Complex.NaN); - assertComplex(oneNaN.sinh(), Complex.NaN); + assertComplex(oneInf.sinh(), Complex.NAN); + assertComplex(oneNaN.sinh(), Complex.NAN); assertComplex(infZero.sinh(), infZero); // skipped test similar to previous section assertComplex(infInf.sinh(), infNaN); assertComplex(infNaN.sinh(), infNaN); assertComplex(nanZero.sinh(), nanZero); - assertComplex(nanOne.sinh(), Complex.NaN); - assertComplex(Complex.NaN.sinh(), Complex.NaN); + assertComplex(nanOne.sinh(), Complex.NAN); + assertComplex(Complex.NAN.sinh(), Complex.NAN); } /** @@ -214,14 +214,14 @@ public class CStandardTest { public void testTanh() { assertComplex(oneOne.tanh().conj(), oneOne.conj().tanh()); // AND CSINH IS ODD assertComplex(Complex.ZERO.tanh(), Complex.ZERO); - assertComplex(oneInf.tanh(), Complex.NaN); - assertComplex(oneNaN.tanh(), Complex.NaN); + assertComplex(oneInf.tanh(), Complex.NAN); + assertComplex(oneNaN.tanh(), Complex.NAN); //Do Not Understand the Next Test assertComplex(infInf.tanh(), oneZero); assertComplex(infNaN.tanh(), oneZero); assertComplex(nanZero.tanh(), nanZero); - assertComplex(nanOne.tanh(), Complex.NaN); - assertComplex(Complex.NaN.tanh(), Complex.NaN); + assertComplex(nanOne.tanh(), Complex.NAN); + assertComplex(Complex.NAN.tanh(), Complex.NAN); } /** @@ -232,8 +232,8 @@ public class CStandardTest { assertComplex(oneOne.conj().exp(), oneOne.exp().conj()); assertComplex(Complex.ZERO.exp(), oneZero); assertComplex(negZeroZero.exp(), oneZero); - assertComplex(oneInf.exp(), Complex.NaN); - assertComplex(oneNaN.exp(), Complex.NaN); + assertComplex(oneInf.exp(), Complex.NAN); + assertComplex(oneNaN.exp(), Complex.NAN); assertComplex(infZero.exp(), infZero); // Do not understand next test assertComplex(negInfInf.exp(), Complex.ZERO); @@ -241,8 +241,8 @@ public class CStandardTest { assertComplex(negInfNaN.exp(), Complex.ZERO); assertComplex(infNaN.exp(), infNaN); assertComplex(nanZero.exp(), nanZero); - assertComplex(nanOne.exp(), Complex.NaN); - assertComplex(Complex.NaN.exp(), Complex.NaN); + assertComplex(nanOne.exp(), Complex.NAN); + assertComplex(Complex.NAN.exp(), Complex.NAN); } /** @@ -254,14 +254,14 @@ public class CStandardTest { assertComplex(negZeroZero.log(), negInfPi); assertComplex(Complex.ZERO.log(), negInfZero); assertComplex(oneInf.log(), infPiTwo); - assertComplex(oneNaN.log(), Complex.NaN); + assertComplex(oneNaN.log(), Complex.NAN); assertComplex(negInfOne.log(), infPi); assertComplex(infOne.log(), infZero); assertComplex(infInf.log(), infPiFour); assertComplex(infNaN.log(), infNaN); - assertComplex(nanOne.log(), Complex.NaN); + assertComplex(nanOne.log(), Complex.NAN); assertComplex(nanInf.log(), infNaN); - assertComplex(Complex.NaN.log(), Complex.NaN); + assertComplex(Complex.NAN.log(), Complex.NAN); } /** @@ -276,7 +276,7 @@ public class CStandardTest { assertComplex(infOne.sqrt(), infZero); assertComplex(negInfNaN.sqrt(), nanInf); assertComplex(infNaN.sqrt(), infNaN); - assertComplex(nanOne.sqrt(), Complex.NaN); - assertComplex(Complex.NaN.sqrt(), Complex.NaN); + assertComplex(nanOne.sqrt(), Complex.NAN); + assertComplex(Complex.NAN.sqrt(), Complex.NAN); } } http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/2f1c0b5d/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexTest.java ---------------------------------------------------------------------- diff --git a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexTest.java b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexTest.java index b1ffab6..3e87320 100644 --- a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexTest.java +++ b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexTest.java @@ -81,7 +81,7 @@ public class ComplexTest { @Test public void testAbsNaN() { - Assert.assertTrue(Double.isNaN(Complex.NaN.abs())); + Assert.assertTrue(Double.isNaN(Complex.NAN.abs())); Complex z = new Complex(inf, nan); Assert.assertTrue(Double.isNaN(z.abs())); } @@ -146,7 +146,7 @@ public class ComplexTest { @Test public void testConjugateNaN() { - Complex z = Complex.NaN.conjugate(); + Complex z = Complex.NAN.conjugate(); Assert.assertTrue(z.isNaN()); } @@ -187,20 +187,20 @@ public class ComplexTest { Complex x = new Complex(3.0, 4.0); Complex z = x.divide(Complex.ZERO); // Assert.assertEquals(z, Complex.INF); // See MATH-657 - Assert.assertEquals(z, Complex.NaN); + Assert.assertEquals(z, Complex.NAN); } @Test public void testDivideZeroZero() { Complex x = new Complex(0.0, 0.0); Complex z = x.divide(Complex.ZERO); - Assert.assertEquals(z, Complex.NaN); + Assert.assertEquals(z, Complex.NAN); } @Test public void testDivideNaN() { Complex x = new Complex(3.0, 4.0); - Complex z = x.divide(Complex.NaN); + Complex z = x.divide(Complex.NAN); Assert.assertTrue(z.isNaN()); } @@ -274,7 +274,7 @@ public class ComplexTest { @Test public void testReciprocalNaN() { - Assert.assertTrue(Complex.NaN.reciprocal().isNaN()); + Assert.assertTrue(Complex.NAN.reciprocal().isNaN()); } @Test @@ -333,7 +333,7 @@ public class ComplexTest { @Test public void testNegateNaN() { - Complex z = Complex.NaN.negate(); + Complex z = Complex.NAN.negate(); Assert.assertTrue(z.isNaN()); } @@ -521,7 +521,7 @@ public class ComplexTest { Complex realNaN = new Complex(Double.NaN, 0.0); Complex imaginaryNaN = new Complex(0.0, Double.NaN); Assert.assertEquals(realNaN.hashCode(), imaginaryNaN.hashCode()); - Assert.assertEquals(imaginaryNaN.hashCode(), Complex.NaN.hashCode()); + Assert.assertEquals(imaginaryNaN.hashCode(), Complex.NAN.hashCode()); // MATH-1118 // "equals" and "hashCode" must be compatible: if two objects have @@ -578,7 +578,7 @@ public class ComplexTest { @Test public void testScalarPowNaNBase() { - Complex x = Complex.NaN; + Complex x = Complex.NAN; double yDouble = 5.0; Complex yComplex = new Complex(yDouble); Assert.assertEquals(x.pow(yComplex), x.pow(yDouble)); @@ -616,7 +616,7 @@ public class ComplexTest { @Test public void testSqrt1zNaN() { - Assert.assertTrue(Complex.NaN.sqrt1z().isNaN()); + Assert.assertTrue(Complex.NAN.sqrt1z().isNaN()); } /** @@ -799,7 +799,7 @@ public class ComplexTest { public void testGetArgumentNaN() { Assert.assertTrue(Double.isNaN(nanZero.getArgument())); Assert.assertTrue(Double.isNaN(zeroNaN.getArgument())); - Assert.assertTrue(Double.isNaN(Complex.NaN.getArgument())); + Assert.assertTrue(Double.isNaN(Complex.NAN.getArgument())); } /* http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/2f1c0b5d/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexUtilsTest.java ---------------------------------------------------------------------- diff --git a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexUtilsTest.java b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexUtilsTest.java index 9a7a29d..3989797 100644 --- a/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexUtilsTest.java +++ b/commons-numbers-complex/src/test/java/org/apache/commons/numbers/complex/ComplexUtilsTest.java @@ -209,17 +209,17 @@ public class ComplexUtilsTest { @Test public void testPolar2ComplexNaN() { - TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(nan, 1)); - TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(1, nan)); - TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(nan, nan)); + TestUtils.assertSame(Complex.NAN, ComplexUtils.polar2Complex(nan, 1)); + TestUtils.assertSame(Complex.NAN, ComplexUtils.polar2Complex(1, nan)); + TestUtils.assertSame(Complex.NAN, ComplexUtils.polar2Complex(nan, nan)); } @Test public void testPolar2ComplexInf() { - TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(1, inf)); - TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(1, negInf)); - TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(inf, inf)); - TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(inf, negInf)); + TestUtils.assertSame(Complex.NAN, ComplexUtils.polar2Complex(1, inf)); + TestUtils.assertSame(Complex.NAN, ComplexUtils.polar2Complex(1, negInf)); + TestUtils.assertSame(Complex.NAN, ComplexUtils.polar2Complex(inf, inf)); + TestUtils.assertSame(Complex.NAN, ComplexUtils.polar2Complex(inf, negInf)); TestUtils.assertSame(infInf, ComplexUtils.polar2Complex(inf, pi / 4)); TestUtils.assertSame(infNaN, ComplexUtils.polar2Complex(inf, 0)); TestUtils.assertSame(infNegInf, ComplexUtils.polar2Complex(inf, -pi / 4));