Note: There are a few cases of duplicates in user autocompletion which are being worked on.

Avoid using == with float

RESOLVED FIXED in mozilla17

Status

()

Core
Layout: Text
RESOLVED FIXED
5 years ago
5 years ago

People

(Reporter: espindola, Unassigned)

Tracking

unspecified
mozilla17
x86_64
Linux
Points:
---

Firefox Tracking Flags

(Not tracked)

Details

Attachments

(1 attachment)

Created attachment 645875 [details] [diff] [review]
don't use == with float

layout/generic/nsTextFrameThebes.cpp currently has some equality comparison with float point. While it looks like they should be valid in a really strict compiler, on x86 there can be some problems.

The x87 stack has higher precision than a float. If one side of the comparison is coming from a computation on the stack and the other one is loaded from memory, there can be a small difference that causes the comparison to fail.
Attachment #645875 - Flags: review?(roc)
A try push is at https://tbpl.mozilla.org/?tree=Try&pusher=respindola@mozilla.com.
Attachment #645875 - Flags: review?(roc) → review+
Comment on attachment 645875 [details] [diff] [review]
don't use == with float

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

::: layout/generic/nsTextFrameThebes.cpp
@@ +6710,5 @@
>    }
>  }
>  
> +bool nsTextFrame::IsCurrentFontInflation(float aInflation) const {
> +  return fabsf(aInflation - GetFontSizeInflation()) < 1e-6;

If aInflation is greater than 1e6 * epsilon, which is roughly 1e+1, then this is no different than the == comparison it replaces.

So:
 - either you know something very specific here about the order of magnitude of aInflation?
 - or you should use a relative (multiplicative), not absolute, fuzzy comparison. Typically:
  abs(a - b) < precision * min(abs(a), abs(b))
From what I see in the code, the comparison is always used to check if a value is from a previous call to GetFontSizeInflation. The case I noticed it failing was because one side had been stored (narrowed to a float) and the other wasn't. The difference from the long double to the float was about 1e-8, so I just used 1e-6, but I agree it is not a general solution.

Do we have a routine for relative comparison somewhere in the codebase?
Font inflation values will generally be between 0.1 and 10 so this is fine.
btw, gcc >= 3.0 has a -Wfloat-equal warning flag that will warn when a floating point value is compared for equality using the == or != operators.

http://gcc.gnu.org/onlinedocs/gcc-3.0.4/gcc_3.html#SEC11

Comment 6

5 years ago
https://hg.mozilla.org/mozilla-central/rev/05852ba29df2
Status: NEW → RESOLVED
Last Resolved: 5 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla17
(In reply to Chris Peterson (:cpeterson) from comment #5)
> btw, gcc >= 3.0 has a -Wfloat-equal warning flag that will warn when a
> floating point value is compared for equality using the == or != operators.
> 
> http://gcc.gnu.org/onlinedocs/gcc-3.0.4/gcc_3.html#SEC11

Filed bug 777803 to enable this warning.
You need to log in before you can comment on or make changes to this bug.