Precision { public static boolean equals(float x, float y) { return equals(x, y, 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 testEqualsWithAllowedDelta() { Assert.assertTrue(Precision.equals(153.0000, 153.0000, .0625)); Assert.assertTrue(Precision.equals(153.0000, 153.0625, .0625)); Assert.assertTrue(Precision.equals(152.9375, 153.0000, .0625)); Assert.assertFalse(Precision.equals(153.0000, 153.0625, .0624)); Assert.assertFalse(Precision.equals(152.9374, 153.0000, .0625)); Assert.assertFalse(Precision.equals(Double.NaN, Double.NaN, 1.0)); Assert.assertTrue(Precision.equals(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, 1.0)); Assert.assertTrue(Precision.equals(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, 1.0)); Assert.assertFalse(Precision.equals(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 1.0)); }
@Test public void testMath475() { final double a = 1.7976931348623182E16; final double b = FastMath.nextUp(a); double diff = FastMath.abs(a - b); Assert.assertTrue(Precision.equals(a, b, 0.5 * diff)); final double c = FastMath.nextUp(b); diff = FastMath.abs(a - c); Assert.assertTrue(Precision.equals(a, c, diff)); Assert.assertFalse(Precision.equals(a, c, (1 - 1e-16) * diff)); }
@Test public void testFloatEqualsWithAllowedUlps() { Assert.assertTrue("+0.0f == -0.0f",Precision.equals(0.0f, -0.0f)); Assert.assertTrue("+0.0f == -0.0f (1 ulp)",Precision.equals(0.0f, -0.0f, 1)); float oneFloat = 1.0f; Assert.assertTrue("1.0f == 1.0f + 1 ulp",Precision.equals(oneFloat, Float.intBitsToFloat(1 + Float.floatToIntBits(oneFloat)))); Assert.assertTrue("1.0f == 1.0f + 1 ulp (1 ulp)",Precision.equals(oneFloat, Float.intBitsToFloat(1 + Float.floatToIntBits(oneFloat)), 1)); Assert.assertFalse("1.0f != 1.0f + 2 ulp (1 ulp)",Precision.equals(oneFloat, Float.intBitsToFloat(2 + Float.floatToIntBits(oneFloat)), 1)); Assert.assertTrue(Precision.equals(153.0f, 153.0f, 1)); Assert.assertTrue(Precision.equals(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY, 1)); Assert.assertTrue(Precision.equals(Double.MAX_VALUE, Float.POSITIVE_INFINITY, 1)); Assert.assertTrue(Precision.equals(Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY, 1)); Assert.assertTrue(Precision.equals(-Float.MAX_VALUE, Float.NEGATIVE_INFINITY, 1)); Assert.assertFalse(Precision.equals(Float.NaN, Float.NaN, 1)); Assert.assertFalse(Precision.equals(Float.NaN, Float.NaN, 0)); Assert.assertFalse(Precision.equals(Float.NaN, 0, 0)); Assert.assertFalse(Precision.equals(Float.NaN, Float.POSITIVE_INFINITY, 0)); Assert.assertFalse(Precision.equals(Float.NaN, Float.NEGATIVE_INFINITY, 0)); Assert.assertFalse(Precision.equals(Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY, 100000)); }
@Test public void testEqualsWithAllowedUlps() { Assert.assertTrue(Precision.equals(0.0, -0.0, 1)); Assert.assertTrue(Precision.equals(1.0, 1 + FastMath.ulp(1d), 1)); Assert.assertFalse(Precision.equals(1.0, 1 + 2 * FastMath.ulp(1d), 1)); final double nUp1 = FastMath.nextAfter(1d, Double.POSITIVE_INFINITY); final double nnUp1 = FastMath.nextAfter(nUp1, Double.POSITIVE_INFINITY); Assert.assertTrue(Precision.equals(1.0, nUp1, 1)); Assert.assertTrue(Precision.equals(nUp1, nnUp1, 1)); Assert.assertFalse(Precision.equals(1.0, nnUp1, 1)); Assert.assertTrue(Precision.equals(0.0, FastMath.ulp(0d), 1)); Assert.assertTrue(Precision.equals(0.0, -FastMath.ulp(0d), 1)); Assert.assertTrue(Precision.equals(153.0, 153.0, 1)); Assert.assertTrue(Precision.equals(153.0, 153.00000000000003, 1)); Assert.assertFalse(Precision.equals(153.0, 153.00000000000006, 1)); Assert.assertTrue(Precision.equals(153.0, 152.99999999999997, 1)); Assert.assertFalse(Precision.equals(153, 152.99999999999994, 1)); Assert.assertTrue(Precision.equals(-128.0, -127.99999999999999, 1)); Assert.assertFalse(Precision.equals(-128.0, -127.99999999999997, 1)); Assert.assertTrue(Precision.equals(-128.0, -128.00000000000003, 1)); Assert.assertFalse(Precision.equals(-128.0, -128.00000000000006, 1)); Assert.assertTrue(Precision.equals(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, 1)); Assert.assertTrue(Precision.equals(Double.MAX_VALUE, Double.POSITIVE_INFINITY, 1)); Assert.assertTrue(Precision.equals(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, 1)); Assert.assertTrue(Precision.equals(-Double.MAX_VALUE, Double.NEGATIVE_INFINITY, 1)); Assert.assertFalse(Precision.equals(Double.NaN, Double.NaN, 1)); Assert.assertFalse(Precision.equals(Double.NaN, Double.NaN, 0)); Assert.assertFalse(Precision.equals(Double.NaN, 0, 0)); Assert.assertFalse(Precision.equals(Double.NaN, Double.POSITIVE_INFINITY, 0)); Assert.assertFalse(Precision.equals(Double.NaN, Double.NEGATIVE_INFINITY, 0)); Assert.assertFalse(Precision.equals(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 100000)); }
@Test public void testMath1127() { Assert.assertFalse(Precision.equals(2.0, -2.0, 1)); Assert.assertTrue(Precision.equals(0.0, -0.0, 0)); Assert.assertFalse(Precision.equals(2.0f, -2.0f, 1)); Assert.assertTrue(Precision.equals(0.0f, -0.0f, 0)); }