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

Incorrect suggestion for clippy::suboptimal_flops #10003

Closed
bsdrks opened this issue Nov 30, 2022 · 1 comment · Fixed by #10113
Closed

Incorrect suggestion for clippy::suboptimal_flops #10003

bsdrks opened this issue Nov 30, 2022 · 1 comment · Fixed by #10113
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

@bsdrks
Copy link

bsdrks commented Nov 30, 2022

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

@bsdrks bsdrks added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Nov 30, 2022
jqnatividad added a commit to jqnatividad/qsv-stats that referenced this issue Dec 4, 2022
as it appears to have issues as evinced by our failing CI on macOS

rust-lang/rust-clippy#10003
@ericwu17
Copy link
Contributor

Thanks for the report! I found that the reason why clippy was giving this suggestion was because it was trying to negate the expression x - 1.0 and did so by simply adding a negative sign in front. Obviously, this did not achieve the desired effect. I'm about to open a PR for this issue.

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