class RuboCop::Cop::Lint::FloatComparison

Checks for the presence of precise comparison of floating point numbers.

Floating point values are inherently inaccurate, and comparing them for exact equality is almost never the desired semantics. Comparison via the ‘==/!=` operators checks floating-point value representation to be exactly the same, which is very unlikely if you perform any arithmetic operations involving precision loss.

@example

# bad
x == 0.1
x != 0.1

# bad
case value
when 1.0
  foo
when 2.0
  bar
end

# good - using BigDecimal
x.to_d == 0.1.to_d

# good - comparing against zero
x == 0.0
x != 0.0

# good
(x - 0.1).abs < Float::EPSILON

# good
tolerance = 0.0001
(x - 0.1).abs < tolerance

# good - comparing against nil
Float(x, exception: false) == nil

# good - using epsilon comparison in case expression
case
when (value - 1.0).abs < Float::EPSILON
  foo
when (value - 2.0).abs < Float::EPSILON
  bar
end

# Or some other epsilon based type of comparison:
# https://www.embeddeduse.com/2019/08/26/qt-compare-two-floats/