rust-lang / rust-lang/rust-clippy
float_cmp false positive: catching underflow
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Presently, float_cmp informs that
| if t == t + h {
| ^^^^^^^^^^ help: consider comparing them within some error: `(t - (t + h)).abs() < error`
|
= note: #[deny(float_cmp)] on by default
note: std::f32::EPSILON and std::f64::EPSILON are available.
is an incorrect comparison of float. If I was comparing two distinct floats, then I would agree; however, the check here is meant to detect when the addition of h to t doesn't actually increase t.
The check for an exact equality is necessary here, and the suggestion of using EPSILON is incorrect as t + h might still be distinct from t (especially if t and h are both much less than EPSILON). Technically, one would check that (t + h) - t < MIN_POSITIVE, but this is only because it is equivalent to (t + h) - t == 0.0 (and has the drawback of obscuring the real intention of the check in the first place).
In my case, I'm using this check to ensure that the step size used when solving an ODE never stops increasing t (which is not the best check in this use-case and can be improved, but I'm sure there are other cases where this check might still be desirable and valid).
From checking the standard library, I'm not sure whether there is a better way of checking for this situation other than through t == t + h.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the float_cmp lint implementation and its existing tests, then compare their behavior with the underflow case described here. Determine whether the equality check should be exempted or handled differently, and add a regression test demonstrating the intended result.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100