A value is compared with == to a decimal fraction that no float or double holds exactly
What it finds
total == 0.1 compares with the double nearest to 0.1, and arithmetic that should give 0.1 often gives a neighbor of it, so the comparison fails. Comparing within a tolerance, or using BigDecimal for decimal amounts, says what is meant.
Reported only for literals that binary floating point cannot represent exactly, so x == 0.5 and x == 0.0 are left alone.
From the rule's source, float_equality.rs.
fun check(total: Double, ratio: Float) { if (total == 0.1) println("rarely")tx0 reports: 0.1 is not exactly representable if (ratio != 0.3f) println("often")tx0 reports: 0.3f is not exactly representable if (total == 0.5) println("exact") if (total == 0.0) println("zero") if (total == 2.0) println("whole")} 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/kotlin: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/kotlin:float-literal-equality] reason="..."tx0 explain tx0/kotlin:float-literal-equality shows the same in the terminal. Found a false positive? Report it.