Precision { public static int compareTo(double x, double y, double eps) { if (equals(x, y, eps)) { return 0; } else if (x < y) { return -1; } return 1; } private  Precision(); static int compareTo(double x, double y, double eps); static int compareTo(final double x, final double y, final int maxUlps); static boolean equals(float x, float y); static boolean equalsIncludingNaN(float x, float y); static boolean equals(float x, float y, float eps); static boolean equalsIncludingNaN(float x, float y, float eps); static boolean equals(final float x, final float y, final int maxUlps); static boolean equalsIncludingNaN(float x, float y, int maxUlps); static boolean equals(double x, double y); static boolean equalsIncludingNaN(double x, double y); static boolean equals(double x, double y, double eps); static boolean equalsWithRelativeTolerance(double x, double y, double eps); static boolean equalsIncludingNaN(double x, double y, double eps); static boolean equals(final double x, final double y, final int maxUlps); static boolean equalsIncludingNaN(double x, double y, int maxUlps); static double round(double x, int scale); static double round(double x, int scale, int roundingMethod); static float round(float x, int scale); static float round(float x, int scale, int roundingMethod); static double representableDelta(double x,
                                            double originalDelta); static final double EPSILON; static final double SAFE_MIN; }
@Test public void testCompareToEpsilon() { Assert.assertEquals(0, Precision.compareTo(152.33, 152.32, .011)); Assert.assertTrue(Precision.compareTo(152.308, 152.32, .011) < 0); Assert.assertTrue(Precision.compareTo(152.33, 152.318, .011) > 0); Assert.assertEquals(0, Precision.compareTo(Double.MIN_VALUE, +0.0, Double.MIN_VALUE)); Assert.assertEquals(0, Precision.compareTo(Double.MIN_VALUE, -0.0, Double.MIN_VALUE)); }
@Test public void testCompareToMaxUlps() { double a = 152.32; double delta = FastMath.ulp(a); for (int i = 0; i <= 10; ++i) { if (i <= 5) { Assert.assertEquals( 0, Precision.compareTo(a, a + i * delta, 5)); Assert.assertEquals( 0, Precision.compareTo(a, a - i * delta, 5)); } else { Assert.assertEquals(-1, Precision.compareTo(a, a + i * delta, 5)); Assert.assertEquals(+1, Precision.compareTo(a, a - i * delta, 5)); } } Assert.assertEquals( 0, Precision.compareTo(-0.0, 0.0, 0)); Assert.assertEquals(-1, Precision.compareTo(-Double.MIN_VALUE, -0.0, 0)); Assert.assertEquals( 0, Precision.compareTo(-Double.MIN_VALUE, -0.0, 1)); Assert.assertEquals(-1, Precision.compareTo(-Double.MIN_VALUE, +0.0, 0)); Assert.assertEquals( 0, Precision.compareTo(-Double.MIN_VALUE, +0.0, 1)); Assert.assertEquals(+1, Precision.compareTo( Double.MIN_VALUE, -0.0, 0)); Assert.assertEquals( 0, Precision.compareTo( Double.MIN_VALUE, -0.0, 1)); Assert.assertEquals(+1, Precision.compareTo( Double.MIN_VALUE, +0.0, 0)); Assert.assertEquals( 0, Precision.compareTo( Double.MIN_VALUE, +0.0, 1)); Assert.assertEquals(-1, Precision.compareTo(-Double.MIN_VALUE, Double.MIN_VALUE, 0)); Assert.assertEquals(-1, Precision.compareTo(-Double.MIN_VALUE, Double.MIN_VALUE, 1)); Assert.assertEquals( 0, Precision.compareTo(-Double.MIN_VALUE, Double.MIN_VALUE, 2)); Assert.assertEquals( 0, Precision.compareTo(Double.MAX_VALUE, Double.POSITIVE_INFINITY, 1)); Assert.assertEquals(-1, Precision.compareTo(Double.MAX_VALUE, Double.POSITIVE_INFINITY, 0)); Assert.assertEquals(+1, Precision.compareTo(Double.MAX_VALUE, Double.NaN, Integer.MAX_VALUE)); Assert.assertEquals(+1, Precision.compareTo(Double.NaN, Double.MAX_VALUE, Integer.MAX_VALUE)); }