Skip to content

float_cmp false positive: catching underflow #2834

Open
@JP-Ellis

Description

@JP-Ellis

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions