Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

float_cmp false positive: catching underflow #2834

Open
JP-Ellis opened this issue Jun 7, 2018 · 0 comments · May be fixed by #11948
Open

float_cmp false positive: catching underflow #2834

JP-Ellis opened this issue Jun 7, 2018 · 0 comments · May be fixed by #11948
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@JP-Ellis
Copy link

JP-Ellis commented Jun 7, 2018

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.

@phansch phansch added the C-bug Category: Clippy is not doing the correct thing label Jun 12, 2018
@phansch phansch added the I-false-positive Issue: The lint was triggered on code it shouldn't have label Dec 18, 2020
@Jarcho Jarcho linked a pull request Dec 10, 2023 that will close this issue
bors added a commit that referenced this issue Jul 16, 2024
`float_cmp` changes

fixes #2834
fixes #6816

---

changelog: This:
* Deprecated `float_cmp_const` in favor of a config option on [`float_cmp`].
  [#11948](#11948)
* [`float_cmp`]: Don't lint literal and self comparisons.
  [#11948](#11948)
* [`float_cmp`] [#11948](#11948)
    * Add the [`float-cmp-ignore-named-constants`] configuration to ignore comparisons to named constants.
    * Add the [`float-cmp-ignore-change-detection`] configuration to ignore const-evaluatabled values.
    * Add the [`float-cmp-ignore-constant-comparisons`] configuration to ignore comparisons to the modification of an operand (e.g. `x == f(x)`).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants