···44import org.junit.Test;
5566/**
77- * Test suite to demonstrate a good method for comparing floating-point values using an epsilon. Run via JUnit 4.
88- *
99- * Note: this function attempts a "one size fits all" solution. There may be some edge cases for which it still
1010- * produces unexpected results, and some of the tests it was developed to pass probably specify behaviour that is
1111- * not appropriate for some applications. Before using it, make sure it's appropriate for your application!
1212- *
77+ * Test suite to demonstrate a good method for comparing floating-point values
88+ * using an epsilon. Run via JUnit 4.
99+ *
1010+ * Note: this function attempts a "one size fits all" solution. There may be
1111+ * some edge cases for which it still produces unexpected results, and some of
1212+ * the tests it was developed to pass probably specify behaviour that is not
1313+ * appropriate for some applications. Before using it, make sure it's
1414+ * appropriate for your application!
1515+ *
1316 * From http://floating-point-gui.de
1414- *
1717+ *
1518 * @author Michael Borgwardt
1619 */
1720public class NearlyEqualsTest {
···2023 final float absB = Math.abs(b);
2124 final float diff = Math.abs(a - b);
22252323- if (a * b == 0) { // a or b or both are zero
2626+ if (a == b) { // shortcut, handles infinities
2727+ return true;
2828+ } else if (a * b == 0) { // a or b or both are zero
2429 // relative error is not meaningful here
2530 return diff < (epsilon * epsilon);
2631 } else { // use relative error
···108113 assertFalse(nearlyEqual(0.0f, -0.00000001f, 0.00000001f));
109114 }
110115116116+ /**
117117+ * Comparisons involving infinities
118118+ */
119119+ @Test
120120+ public void infinities() {
121121+ assertTrue(nearlyEqual(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY));
122122+ assertTrue(nearlyEqual(Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY));
123123+ assertFalse(nearlyEqual(Float.NEGATIVE_INFINITY,
124124+ Float.POSITIVE_INFINITY));
125125+ assertFalse(nearlyEqual(Float.POSITIVE_INFINITY, Float.MAX_VALUE));
126126+ assertFalse(nearlyEqual(Float.NEGATIVE_INFINITY, -Float.MAX_VALUE));
127127+ }
128128+129129+ /**
130130+ * Comparisons involving NaN values
131131+ */
132132+ @Test
133133+ public void nan() {
134134+ assertFalse(nearlyEqual(Float.NaN, Float.NaN));
135135+ }
136136+111137 /** Comparisons of numbers on opposite sides of 0 */
112138 @Test
113139 public void opposite() {
···115141 assertFalse(nearlyEqual(-1.0f, 1.000000001f));
116142 assertFalse(nearlyEqual(-1.000000001f, 1.0f));
117143 assertFalse(nearlyEqual(1.0f, -1.000000001f));
118118- assertTrue(nearlyEqual(1e10f * Float.MIN_VALUE, -1e10f * Float.MIN_VALUE));
144144+ assertTrue(nearlyEqual(1e10f * Float.MIN_VALUE, -1e10f
145145+ * Float.MIN_VALUE));
119146 }
120147121148 /**
···137164138165 assertFalse(nearlyEqual(1e25f * Float.MIN_VALUE, 0.0f, 1e-12f));
139166 assertFalse(nearlyEqual(0.0f, 1e25f * Float.MIN_VALUE, 1e-12f));
140140- assertFalse(nearlyEqual(1e25f * Float.MIN_VALUE, -1e25f * Float.MIN_VALUE, 1e-12f));
167167+ assertFalse(nearlyEqual(1e25f * Float.MIN_VALUE, -1e25f
168168+ * Float.MIN_VALUE, 1e-12f));
141169142170 assertTrue(nearlyEqual(1e25f * Float.MIN_VALUE, 0.0f, 1e-5f));
143171 assertTrue(nearlyEqual(0.0f, 1e25f * Float.MIN_VALUE, 1e-5f));
144144- assertTrue(nearlyEqual(1e20f * Float.MIN_VALUE, -1e20f * Float.MIN_VALUE, 1e-5f));
172172+ assertTrue(nearlyEqual(1e20f * Float.MIN_VALUE, -1e20f
173173+ * Float.MIN_VALUE, 1e-5f));
145174 }
146175147176}
+3-1
content/errors/comparison.html
···4444 final float absB = Math.abs(b);
4545 final float diff = Math.abs(a - b);
46464747- if (a * b == 0) { // a or b or both are zero
4747+ if (a == b) { // shortcut, handles infinities
4848+ return true;
4949+ } else if (a * b == 0) { // a or b or both are zero
4850 // relative error is not meaningful here
4951 return diff < (epsilon * epsilon);
5052 } else { // use relative error