[layers] float comparisons should use an epsilon

RESOLVED FIXED

Status

()

Firefox for Android
General
P2
normal
RESOLVED FIXED
6 years ago
6 years ago

People

(Reporter: kats, Assigned: kats)

Tracking

unspecified
All
Android
Points:
---

Firefox Tracking Flags

(firefox11 fixed, fennec11+)

Details

Attachments

(2 attachments, 1 obsolete attachment)

Floating point comparisons in java should generally use an epsilon rather than being compared to each other directly. Doing (foo == 0.0f) may fail when even though (Math.abs(foo) < 1e-7) passes, due to floating point rounding issues.

Code that uses == for float comparisons exists in PanZoomPosition.java and a few other classes in the layers code.

Updated

6 years ago
Blocks: 695448
Priority: -- → P2
Created attachment 574962 [details] [diff] [review]
Add epsilon to float compares

Turns out the other places this happened were in PanZoomPosition, which existed only briefly in the patch queue.
Attachment #574962 - Flags: review?(pwalton)
Created attachment 574963 [details] [diff] [review]
Add epsilon to float compares

Oops, wrong version of patch
Attachment #574962 - Attachment is obsolete: true
Attachment #574962 - Flags: review?(pwalton)
Attachment #574963 - Flags: review?(pwalton)

Comment 3

6 years ago
Comment on attachment 574963 [details] [diff] [review]
Add epsilon to float compares

Review of attachment 574963 [details] [diff] [review]:
-----------------------------------------------------------------

getExcess() actually explicitly returns 0.0f, so I'd have thought the comparison would be ok (I assume that 0.0f == 0.0f holds - but perhaps it doesn't after assignment?). Does this fix the crash you were seeing?
getExcess() explicitly returns 0.0f, but it could also return things like viewportPos or getViewportEnd() - mPageLength, and those values might be really really close to zero without actually being zero.

I've attached a test case that shows how rounding error can result in values just slightly off because of floating point representation.

I thought that this might have caused the crash in bug 700559 by sending the state into WAITING_TO_SNAP instead of STOPPED, but after looking through the logic again I don't think that's possible.
Created attachment 575161 [details]
java code that demonstrates rounding issues
Comment on attachment 574963 [details] [diff] [review]
Add epsilon to float compares

Review of attachment 574963 [details] [diff] [review]:
-----------------------------------------------------------------

Fine by me, with the method name renamed.

::: embedding/android/ui/PanZoomController.java
@@ +333,5 @@
>      private float computeElasticity(float excess, float viewportLength) {
>          return 1.0f - excess / (viewportLength * SNAP_LIMIT);
>      }
>  
> +    private static boolean floatsEqual(float a, float b) {

I'd call this floatsNearlyEqual() or something -- floatsEqual() isn't accurate.
Attachment #574963 - Flags: review?(pwalton) → review+
http://hg.mozilla.org/projects/birch/rev/e056708c00aa
Status: ASSIGNED → RESOLVED
Last Resolved: 6 years ago
Resolution: --- → FIXED
tracking-fennec: --- → 11+
status-firefox11: --- → fixed
You need to log in before you can comment on or make changes to this bug.