Clippy lints / Nursery
Clippy lint · Nursery · checked with Clippy 0.1.99
clippy::imprecise_flops: cube-root of a number can be computed more accurately
What it meansLooks for floating-point expressions that can be expressed using built-in methods to improve accuracy at the cost of performance. It is in the nursery group and off by default.
What you see
warning: cube-root of a number can be computed more accurately --> main.rs:4:9 | 4 | let _ = a.powf(1.0 / 3.0); | ^^^^^^^^^^^^^^^^^ help: consider using: `a.cbrt()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#imprecise_flops = note: `-W clippy::imprecise-flops` implied by `-W clippy::nursery`
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
Negatively impacts accuracy.
New lints still being finished. Off by default and may have false positives.
How to fix it
Clippy can fix this one for you: run cargo clippy --fix (add --allow-dirty if you have uncommitted changes). Review the diff before you commit.
Flagged
let a = 3f32;
let _ = a.powf(1.0 / 3.0);
let _ = (1.0 + a).ln();
let _ = a.exp() - 1.0;
Write this instead
let a = 3f32;
let _ = a.cbrt();
let _ = a.ln_1p();
let _ = a.exp_m1();
Turn it off, or on
It is off by default. To use it, set it to warn or deny:
Whole crate (Cargo.toml)
[lints.clippy]
imprecise_flops = "warn"In code
#![warn(clippy::imprecise_flops)]One run
cargo clippy -- -W clippy::imprecise-flopsThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Clippy's own documentation
What it does
Looks for floating-point expressions that can be expressed using built-in methods to improve accuracy at the cost of performance.
Why is this bad?
Negatively impacts accuracy.
Example
let a = 3f32;
let _ = a.powf(1.0 / 3.0);
let _ = (1.0 + a).ln();
let _ = a.exp() - 1.0;
Use instead:
let a = 3f32;
let _ = a.cbrt();
let _ = a.ln_1p();
let _ = a.exp_m1();
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- redundant_cloneChecks for a redundant clone() (and its relatives) which clones an owned value that is going to be dropped without further use
- equatable_if_letChecks for pattern matchings that can be expressed using equality
- fallible_impl_fromconsider implementing `TryFrom` instead
- future_not_sendfuture cannot be sent between threads safely
- iter_on_empty_collectionsChecks for calls to iter, iter_mut or into_iter on empty collections
- iter_on_single_items`iter` call on a collection with only one item
- iter_with_drainChecks for usage of .drain(..) on Vec and VecDeque for iteration
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::redundant_cloneChecks for a redundant clone() (and its relatives) which clones an owned value that is going to be dropped without further useRead it next →Did this answer your question?