Bug 702412 - Fix float comparisons to use an epsilon [r=pcwalton]
authorKartikaya Gupta <kgupta@mozilla.com>
Thu, 17 Nov 2011 14:43:28 -0500
changeset 81882 e056708c00aa9537a2750a9637d37bd26082870e
parent 81881 a6a429a48e60cf01777373b5318459054c4134e7
child 81883 ef629731171ff26e93d69af6e49ad5ab0a8de599
push idunknown
push userunknown
push dateunknown
reviewerspcwalton
bugs702412
milestone11.0a1
Bug 702412 - Fix float comparisons to use an epsilon [r=pcwalton]
embedding/android/ui/PanZoomController.java
--- a/embedding/android/ui/PanZoomController.java
+++ b/embedding/android/ui/PanZoomController.java
@@ -330,16 +330,21 @@ public class PanZoomController
             }
         }
     }
 
     private float computeElasticity(float excess, float viewportLength) {
         return 1.0f - excess / (viewportLength * SNAP_LIMIT);
     }
 
+    private static boolean floatsApproxEqual(float a, float b) {
+        // account for floating point rounding errors
+        return Math.abs(a - b) < 1e-6;
+    }
+
     // Physics information for one axis (X or Y).
     private static class Axis {
         public enum FlingStates {
             STOPPED,
             SCROLLING,
             WAITING_TO_SNAP,
             SNAPPING,
         }
@@ -403,17 +408,17 @@ public class PanZoomController
 
         public void startFling(boolean stopped) {
             if (!stopped) {
                 mFlingState = FlingStates.SCROLLING;
                 return;
             }
 
             float excess = getExcess();
-            if (excess == 0.0f)
+            if (floatsApproxEqual(excess, 0.0f))
                 mFlingState = FlingStates.STOPPED;
             else
                 mFlingState = FlingStates.WAITING_TO_SNAP;
         }
 
         // Advances a fling animation by one step.
         public void advanceFling() {
             switch (mFlingState) {
@@ -428,17 +433,17 @@ public class PanZoomController
                 return;
             }
         }
 
         // Performs one frame of a scroll operation if applicable.
         private void scroll() {
             // If we aren't overscrolled, just apply friction.
             float excess = getExcess();
-            if (excess == 0.0f) {
+            if (floatsApproxEqual(excess, 0.0f)) {
                 velocity *= FRICTION;
                 if (Math.abs(velocity) < 0.1f) {
                     velocity = 0.0f;
                     mFlingState = FlingStates.STOPPED;
                 }
                 return;
             }