Skip to content

float-comparison

Rubysuspiciousno fixdefault: warnruby/rubocop

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.

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.

Reported
...
f == 0.1
f == 0
begin
x
...
ruby:
lint:
rules:
float-comparison: warn # off, warn (default), error

Enabled by the presets: ruby/rubocop.

Suppress one line with sudsy-disable-next-line float-comparison in a comment of the file’s language.

The same check in other tools; Sudsy reports what each upstream rule reports.

Tool Rule Match
rubocop Lint/FloatComparison full