Bug 702412 - Fix float comparisons to use an epsilon [r=pcwalton]
--- 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;
}