float-comparison
What it does
Section titled “What it does”Reports ==, !=, eql? or equal? with a float on one side (a literal, to_f, arithmetic on a float), and a float in a when. Comparing with 0 or nil is not reported.
Why it matters
Section titled “Why it matters”Floating point numbers are binary approximations: 0.1 + 0.2 == 0.3 is false. A comparison that is true in the test with round numbers can fail with real data.
To fix, compare with a tolerance ((a - b).abs < Float::EPSILON) or use BigDecimal.
Example
Section titled “Example”...f == 0.1f == 0begin x...Configuration
Section titled “Configuration”ruby: lint: rules: float-comparison: warn # off, warn (default), errorEnabled by the presets: ruby/rubocop.
Suppress one line with sudsy-disable-next-line float-comparison in a comment of the file’s language.
Upstream equivalents
Section titled “Upstream equivalents”The same check in other tools; Sudsy reports what each upstream rule reports.
| Tool | Rule | Match |
|---|---|---|
| rubocop | Lint/FloatComparison |
full |