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/29 22:06:03 UTC

[commons-numbers] 06/07: Remove the static equals(Complex, Complex) methods using Precision.

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 23a490614ee7e252fc2fa334857247deae6cb19f
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Sun Dec 29 21:53:10 2019 +0000

    Remove the static equals(Complex,Complex) methods using Precision.
    
    These methods do not represent all ways to compute equality between a
    pair of complex numbers. These methods are present in commons-math3
    Complex. Helper methods can be added later if the use case is well
    defined.
---
 .../apache/commons/numbers/complex/Complex.java    | 83 --------------------
 .../commons/numbers/complex/CStandardTest.java     |  5 +-
 .../commons/numbers/complex/ComplexTest.java       | 89 ----------------------
 3 files changed, 3 insertions(+), 174 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 f492988..e7c247f 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
@@ -793,89 +793,6 @@ public final class Complex implements Serializable  {
     }
 
     /**
-     * Test for the floating-point equality between Complex objects.
-     * It returns {@code true} if both arguments are equal or within the
-     * range of allowed error (inclusive).
-     *
-     * <p>Returns {@code false} if either of the arguments is NaN.
-     *
-     * @param x First value (cannot be {@code null}).
-     * @param y Second value (cannot be {@code null}).
-     * @param maxUlps {@code (maxUlps - 1)} is the number of floating point
-     * values between the real or imaginary, respectively, parts of {@code x} and
-     * {@code y}.
-     * @return {@code true} if there are fewer than {@code maxUlps} floating
-     * point values between the real or imaginary, respectively, parts of {@code x}
-     * and {@code y}.
-     *
-     * @see Precision#equals(double,double,int)
-     */
-    public static boolean equals(Complex x,
-                                 Complex y,
-                                 int maxUlps) {
-        return Precision.equals(x.real, y.real, maxUlps) &&
-            Precision.equals(x.imaginary, y.imaginary, maxUlps);
-    }
-
-    /**
-     * Returns {@code true} iff the values are equal as defined by
-     * {@link #equals(Complex,Complex,int) equals(x, y, 1)}.
-     *
-     * @param x First value (cannot be {@code null}).
-     * @param y Second value (cannot be {@code null}).
-     * @return {@code true} if the values are equal.
-     */
-    public static boolean equals(Complex x,
-                                 Complex y) {
-        return equals(x, y, 1);
-    }
-
-    /**
-     * Returns {@code true} if, both for the real part and for the imaginary
-     * part, there is no double value strictly between the arguments or the
-     * difference between them is within the range of allowed error
-     * (inclusive).
-     *
-     * <p>Returns {@code false} if either of the arguments is NaN.
-     *
-     * @param x First value (cannot be {@code null}).
-     * @param y Second value (cannot be {@code null}).
-     * @param eps Amount of allowed absolute error.
-     * @return {@code true} if the values are two adjacent floating point
-     * numbers or they are within range of each other.
-     *
-     * @see Precision#equals(double, double, double)
-     */
-    public static boolean equals(Complex x,
-                                 Complex y,
-                                 double eps) {
-        return Precision.equals(x.real, y.real, eps) &&
-            Precision.equals(x.imaginary, y.imaginary, eps);
-    }
-
-    /**
-     * Returns {@code true} if, both for the real part and for the imaginary
-     * part, there is no double value strictly between the arguments or the
-     * relative difference between them is smaller or equal to the given
-     * tolerance.
-     *
-     * <p>Returns {@code false} if either of the arguments is NaN.
-     *
-     * @param x First value (cannot be {@code null}).
-     * @param y Second value (cannot be {@code null}).
-     * @param eps Amount of allowed relative error.
-     * @return {@code true} if the values are two adjacent floating point
-     * numbers or they are within range of each other.
-     *
-     * @see Precision#equalsWithRelativeTolerance(double, double, double)
-     */
-    public static boolean equalsWithRelativeTolerance(Complex x, Complex y,
-                                                      double eps) {
-        return Precision.equalsWithRelativeTolerance(x.real, y.real, eps) &&
-            Precision.equalsWithRelativeTolerance(x.imaginary, y.imaginary, eps);
-    }
-
-    /**
      * Gets a hash code for the complex number.
      *
      * <p>The behavior is the same as if the components of the complex number were passed
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 24cbd67..b47ec5e 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
@@ -588,13 +588,14 @@ public class CStandardTest {
     }
 
     /**
-     * Checks if the complex is zero.
+     * Checks if the complex is zero. This method uses the {@code ==} operator and allows
+     * equality between signed zeros: {@code -0.0 == 0.0}.
      *
      * @param c the complex
      * @return true if zero
      */
     private static boolean isZero(Complex c) {
-        return Complex.equals(c, Complex.ZERO, 0);
+        return c.getReal() == 0 && c.getImaginary() == 0;
     }
 
     /**
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 577c986..b791c9b 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
@@ -1199,95 +1199,6 @@ public class ComplexTest {
     }
 
     @Test
-    public void testFloatingPointEqualsPrecondition1() {
-        Assertions.assertThrows(NullPointerException.class,
-            () -> Complex.equals(Complex.ofCartesian(3.0, 4.0), null, 3));
-    }
-
-    @Test
-    public void testFloatingPointEqualsPrecondition2() {
-        Assertions.assertThrows(NullPointerException.class,
-            () -> Complex.equals(null, Complex.ofCartesian(3.0, 4.0), 3));
-    }
-
-    @Test
-    public void testFloatingPointEquals() {
-        double re = -3.21;
-        double im = 456789e10;
-
-        final Complex x = Complex.ofCartesian(re, im);
-        Complex y = Complex.ofCartesian(re, im);
-
-        Assertions.assertEquals(x, y);
-        Assertions.assertTrue(Complex.equals(x, y));
-
-        final int maxUlps = 5;
-        for (int i = 0; i < maxUlps; i++) {
-            re = Math.nextUp(re);
-            im = Math.nextUp(im);
-        }
-        y = Complex.ofCartesian(re, im);
-        Assertions.assertTrue(Complex.equals(x, y, maxUlps));
-
-        re = Math.nextUp(re);
-        im = Math.nextUp(im);
-        y = Complex.ofCartesian(re, im);
-        Assertions.assertFalse(Complex.equals(x, y, maxUlps));
-    }
-
-    @Test
-    public void testFloatingPointEqualsNaN() {
-        Complex c = Complex.ofCartesian(Double.NaN, 1);
-        Assertions.assertFalse(Complex.equals(c, c));
-
-        c = Complex.ofCartesian(1, Double.NaN);
-        Assertions.assertFalse(Complex.equals(c, c));
-    }
-
-    @Test
-    public void testFloatingPointEqualsWithAllowedDelta() {
-        final double re = 153.0000;
-        final double im = 152.9375;
-        final double tol1 = 0.0625;
-        final Complex x = Complex.ofCartesian(re, im);
-        final Complex y = Complex.ofCartesian(re + tol1, im + tol1);
-        Assertions.assertTrue(Complex.equals(x, y, tol1));
-
-        final double tol2 = 0.0624;
-        Assertions.assertFalse(Complex.equals(x, y, tol2));
-    }
-
-    @Test
-    public void testFloatingPointEqualsWithAllowedDeltaNaN() {
-        final Complex x = Complex.ofCartesian(0, Double.NaN);
-        final Complex y = Complex.ofCartesian(Double.NaN, 0);
-        Assertions.assertFalse(Complex.equals(x, Complex.ZERO, 0.1));
-        Assertions.assertFalse(Complex.equals(x, x, 0.1));
-        Assertions.assertFalse(Complex.equals(x, y, 0.1));
-    }
-
-    @Test
-    public void testFloatingPointEqualsWithRelativeTolerance() {
-        final double tol = 1e-4;
-        final double re = 1;
-        final double im = 1e10;
-
-        final double f = 1 + tol;
-        final Complex x = Complex.ofCartesian(re, im);
-        final Complex y = Complex.ofCartesian(re * f, im * f);
-        Assertions.assertTrue(Complex.equalsWithRelativeTolerance(x, y, tol));
-    }
-
-    @Test
-    public void testFloatingPointEqualsWithRelativeToleranceNan() {
-        final Complex x = Complex.ofCartesian(0, Double.NaN);
-        final Complex y = Complex.ofCartesian(Double.NaN, 0);
-        Assertions.assertFalse(Complex.equalsWithRelativeTolerance(x, Complex.ZERO, 0.1));
-        Assertions.assertFalse(Complex.equalsWithRelativeTolerance(x, x, 0.1));
-        Assertions.assertFalse(Complex.equalsWithRelativeTolerance(x, y, 0.1));
-    }
-
-    @Test
     public void testEqualsWithNull() {
         final Complex x = Complex.ofCartesian(3.0, 4.0);
         Assertions.assertFalse(x.equals(null));