Closed
Description
Summary
Clippy suggests that I change 1_f64 - (x - 1.0).powi(2)
into (x - 1.0).mul_add(-x - 1.0, 1_f64)
, but these expressions do not yield identical results.
Lint Name
clippy::suboptimal_flops
Reproducer
I tried this code:
1_f64 - (x - 1.0).powi(2)
I saw this happen:
multiply and add expressions can be calculated more efficiently and accurately
for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#suboptimal_flopsclippy[Click for full compiler diagnostic](rust-analyzer-diagnostics-view:/diagnostic%20message?2#file[...]%2Fsrc%2Feasing%2Ffunction.rs)
function.rs(70, 5): consider using: `(x - 1.0).mul_add(-x - 1.0, 1_f64)`
The suggested expression is not equivalent to the original. Some examples:
1_f64 - (x - 1.0).powi(2), where x = 0.25, gives 0.4375
(x - 1_f64).mul_add(-x - 1_f64, 1_f64), where x = 0.25, gives 1.9375
1_f64 - (x - 1.0).powi(2), where x = 0.5, gives 0.75
(x - 1_f64).mul_add(-x - 1_f64, 1_f64), where x = 0.5, gives 1.75
1_f64 - (x - 1.0).powi(2), where x = 0.75, gives 0.9375
(x - 1_f64).mul_add(-x - 1_f64, 1_f64), where x = 0.75, gives 1.4375
1_f64 - (x - 1.0).powi(2), where x = 1, gives 1
(x - 1_f64).mul_add(-x - 1_f64, 1_f64), where x = 1, gives 1
1_f64 - (x - 1.0).powi(2), where x = 0, gives 0
(x - 1_f64).mul_add(-x - 1_f64, 1_f64), where x = 0, gives 2
Version
rustc 1.67.0-nightly (1eb62b123 2022-11-27)
binary: rustc
commit-hash: 1eb62b1235fd77200e6bd967d70e83c0f2497233
commit-date: 2022-11-27
host: x86_64-unknown-linux-gnu
release: 1.67.0-nightly
LLVM version: 15.0.4
Related
Probably related to #4735 and #4751, but this is fairly dangerous.
Additional Labels
No response