···11+import static org.junit.Assert.assertFalse;
22+import static org.junit.Assert.assertTrue;
33+44+import org.junit.Test;
55+66+/**
77+ * Demonstrates a good method for comparing floating-point values
88+ * using an epsilon. Run via JUnit 4
99+ *
1010+ * From http://floating-point-gui.de
1111+ *
1212+ * @author Michael Borgwardt
1313+ */
1414+public class NearlyEqualsTest
1515+{
1616+ public static boolean nearlyEqual(float a, float b)
1717+ {
1818+ final float epsilon = 0.000001f;
1919+ final float absA = Math.abs(a);
2020+ final float absB = Math.abs(b);
2121+ final float diff = Math.abs(a-b);
2222+2323+ if (a*b==0) { // a or b or both are zero
2424+ // relative error is not meaningful here
2525+ return diff < Float.MIN_VALUE / epsilon;
2626+ } else { // use relative error
2727+ return diff / (absA+absB) < epsilon;
2828+ }
2929+ }
3030+3131+ /** Regular large numbers - generally not problematic */
3232+ @Test public void big()
3333+ {
3434+ assertTrue(nearlyEqual(1000000f, 1000001f));
3535+ assertTrue(nearlyEqual(1000001f, 1000000f));
3636+ assertFalse(nearlyEqual(10000f, 10001f));
3737+ assertFalse(nearlyEqual(10001f, 10000f));
3838+ }
3939+4040+ /** Negative large numbers */
4141+ @Test public void bigNeg()
4242+ {
4343+ assertTrue(nearlyEqual(-1000000f, -1000001f));
4444+ assertTrue(nearlyEqual(-1000001f, -1000000f));
4545+ assertFalse(nearlyEqual(-10000f, -10001f));
4646+ assertFalse(nearlyEqual(-10001f, -10000f));
4747+ }
4848+4949+ /** Numbers around 1 */
5050+ @Test public void mid()
5151+ {
5252+ assertTrue(nearlyEqual(1.0000001f, 1.0000002f));
5353+ assertTrue(nearlyEqual(1.0000002f, 1.0000001f));
5454+ assertFalse(nearlyEqual(1.0002f, 1.0001f));
5555+ assertFalse(nearlyEqual(1.0001f, 1.0002f));
5656+ }
5757+5858+ /** Numbers around -1 */
5959+ @Test public void midNeg()
6060+ {
6161+ assertTrue(nearlyEqual(-1.000001f, -1.000002f));
6262+ assertTrue(nearlyEqual(-1.000002f, -1.000001f));
6363+ assertFalse(nearlyEqual(-1.0001f, -1.0002f));
6464+ assertFalse(nearlyEqual(-1.0002f, -1.0001f));
6565+ }
6666+6767+ /** Numbers between 1 and 0 */
6868+ @Test public void small()
6969+ {
7070+ assertTrue(nearlyEqual(0.000000001000001f, 0.000000001000002f));
7171+ assertTrue(nearlyEqual(0.000000001000002f, 0.000000001000001f));
7272+ assertFalse(nearlyEqual(0.000000000001002f, 0.000000000001001f));
7373+ assertFalse(nearlyEqual(0.000000000001001f, 0.000000000001002f));
7474+ }
7575+7676+ /** Numbers between -1 and 0 */
7777+ @Test public void smallNeg()
7878+ {
7979+ assertTrue(nearlyEqual(-0.000000001000001f, -0.000000001000002f));
8080+ assertTrue(nearlyEqual(-0.000000001000002f, -0.000000001000001f));
8181+ assertFalse(nearlyEqual(-0.000000000001002f, -0.000000000001001f));
8282+ assertFalse(nearlyEqual(-0.000000000001001f, -0.000000000001002f));
8383+ }
8484+8585+ /** Comparisons involving zero */
8686+ @Test public void zero()
8787+ {
8888+ assertTrue(nearlyEqual(0.0f, 0.0f));
8989+ assertFalse(nearlyEqual(0.00000001f, 0.0f));
9090+ assertFalse(nearlyEqual(0.0f, 0.00000001f));
9191+ }
9292+9393+ /** Comparisons of numbers on opposite sides of 0 */
9494+ @Test public void opposite()
9595+ {
9696+ assertFalse(nearlyEqual(1.000000001f, -1.0f));
9797+ assertFalse(nearlyEqual(-1.0f, 1.000000001f));
9898+ assertFalse(nearlyEqual(-1.000000001f, 1.0f));
9999+ assertFalse(nearlyEqual(1.0f, -1.000000001f));
100100+ assertTrue(nearlyEqual(10000f*Float.MIN_VALUE, -10000f*Float.MIN_VALUE));
101101+ }
102102+103103+ /**
104104+ * The really tricky part - comparisons of numbers
105105+ * very close to zero.
106106+ */
107107+ @Test public void ulp()
108108+ {
109109+ assertTrue(nearlyEqual(Float.MIN_VALUE, -Float.MIN_VALUE));
110110+ assertTrue(nearlyEqual(-Float.MIN_VALUE, Float.MIN_VALUE));
111111+ assertTrue(nearlyEqual(Float.MIN_VALUE, 0));
112112+ assertTrue(nearlyEqual(0, Float.MIN_VALUE));
113113+ assertTrue(nearlyEqual(-Float.MIN_VALUE, 0));
114114+ assertTrue(nearlyEqual(0, -Float.MIN_VALUE));
115115+116116+ assertFalse(nearlyEqual(0.000000001f, -Float.MIN_VALUE));
117117+ assertFalse(nearlyEqual(0.000000001f, Float.MIN_VALUE));
118118+ assertFalse(nearlyEqual(Float.MIN_VALUE, 0.000000001f));
119119+ assertFalse(nearlyEqual(-Float.MIN_VALUE, 0.000000001f));
120120+121121+ assertFalse(nearlyEqual(1e20f*Float.MIN_VALUE, 0.0f));
122122+ assertFalse(nearlyEqual(0.0f, 1e20f*Float.MIN_VALUE));
123123+ assertFalse(nearlyEqual(1e20f*Float.MIN_VALUE, -1e20f*Float.MIN_VALUE));
124124+ }
125125+126126+}
+24-21
content/errors/comparison.html
···88to be equal (e.g. when calculating the same result through different correct methods) often differ
99slightly, and a simple equality test fails. For example:
10101111- a = 0.15 + 0.15
1212- b = 0.1 + 0.2
1111+ float a = 0.15 + 0.15
1212+ float b = 0.1 + 0.2
1313 if(a == b) // can be false!
1414 if(a >= b) // can also be false!
1515···34343535* When both `a` and `b` are zero. `0.0/0.0` is "not a number", which causes an exception on some platforms, or returns false for all comparisons.
3636* When only `b` is zero, the division yields "infinity", which may also cause an exception, or is greater than epsilon even when `a` is smaller.
3737+* It returns `false` when both `a` and `b` are very small but on opposite sides of zero, even when they're the smallest possible non-zero numbers.
37383839Also, the result is not commutative (`nearlyEquals(a,b)` is not always the same as `nearlyEquals(b,a)`). To fix these problems, the code has to get a lot more complex, so we really need to put it into a function of its own:
39404040- function nearlyEqual(a,b)
4141- {
4242- epsilon = 0.00001;
4343- if (a==0.0){
4444- return Math.abs(b) < epsilon;
4545- }else if (b==0.0){
4646- return Math.abs(a) < epsilon;
4747- } else { // ensure commutativity
4848- return Math.abs((a-b)/a) < epsilon &&
4949- Math.abs((b-a)/b) < epsilon;
5050- }
5151- }
4141+ public static boolean nearlyEqual(float a, float b)
4242+ {
4343+ float epsilon = 0.000001f;
4444+ float absA = Math.abs(a);
4545+ float absB = Math.abs(b);
4646+ float diff = Math.abs(a-b);
52475353- if(nearlyEqual(a,b))
4848+ if (a*b==0) { // a or b or both are zero
4949+ // relative error is not meaningful here
5050+ return diff < Float.MIN_VALUE / epsilon;
5151+ } else { // use relative error
5252+ return diff / (absA+absB) < epsilon;
5353+ }
5454+ }
54555555-Unfortunately, this is *still* not perfect; there are at least two problems that are not easy to fix:
5656+This method [passes tests](../NearlyEqualsTest.java) for many important special cases, but as you can see, it
5757+uses some quite non-obvious logic. In particular, it has to use a completely different definition of error margin
5858+when `a` or `b` is zero, because the classical definition of relative error becomes meaningless in those cases.
56595757-* It reverts to using epsilon as an absolute error measure when `a` or `b` is zero.
5858-* It returns `false` when both `a` and `b` are very small but on opposite sides of zero, even when they're the smallest possible non-zero numbers.
6060+There probably are some cases where the method above still produces unexpected results, and some of the tests
6161+it was developed to pass probably specify behaviour that is not appropriate for some applications. Before using it, make sure it's appropriate for your application!
59626060-Compare floating-point values as integers
6161------------------------------------------
6262-But the there is an alternative to adding even more conceptual complexity to such an apparently simple task: instead of comparing `a` and `b` as [real numbers](http://en.wikipedia.org/wiki/Real_numbers), we can think about them as discrete steps and define the error margin as the maximum number of possible floating-point values between the two values.
6363+Comparing floating-point values as integers
6464+-------------------------------------------
6565+There is an alternative to heaping conceptual complexity onto such an apparently simple task: instead of comparing `a` and `b` as [real numbers](http://en.wikipedia.org/wiki/Real_numbers), we can think about them as discrete steps and define the error margin as the maximum number of possible floating-point values between the two values.
63666467This is conceptually very clear and easy and has the advantage of implicitly scaling the relative error margin with the magnitude of the values. Technically, it's a bit more complex, but not as much as you might think, because IEEE 754 floats are designed to maintain their order when their bit patterns are interpreted as integers.
6568