Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.Sign up
Add new `Lint/FloatComparison` cop #8432
This is a known fact that comparing floats is bad, so this cop checks for the presence of precise comparison of floating point numbers.
a = (0.3 * 3) + 0.1 # 0.9999999999999999 b = 1.0 a == b # false
# bad x == 0.1 x != 0.1 # good (x - 0.1).abs < Float::EPSILON # good tolerance = 0.0001 (x - 0.1).abs < tolerance
Should this flag comparisons to 0 or 0.0? I don't believe there are precision issues with zero (
See: https://github.com/ruby/ruby/blob/c87043283ef7114db54857ae13798e3d7cb031c0/numeric.c#L1743-L1747 and https://github.com/ruby/ruby/blob/1035a3b202ee86bf2b0a1d00eefcfff0d7ab9f6b/internal/numeric.h#L137-L141