A value is compared with == or != to a float literal with a fraction, which binary floating point rarely matches exactly
What it finds
Most decimal fractions, such as 0.1, have no exact binary representation, and a value computed by arithmetic often differs from the literal in the last bit: 0.1 + 0.2 == 0.3 is false. math.isclose or a tolerance compares what was meant. Literals with an exact binary value, such as 0.5 or 2.0, are left alone.
From the rule's source, float_literal_equality.rs.
def check(total, ratio): if total == 0.3:tx0 reports: this float literal is rarely matched exactly return 1 if ratio != 0.1:tx0 reports: this float literal is rarely matched exactly return 2 if ratio == 0.5 or total == 2.0 or total == 0.25: return 3 return total < 0.3 Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/python:float-literal-equality" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
# tx0: ignore[tx0/python:float-literal-equality] reason="..."tx0 explain tx0/python:float-literal-equality shows the same in the terminal. Found a false positive? Report it.