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

Some accuracy lints for floating point operations #5443

Merged
merged 9 commits into from
Jul 13, 2020

Conversation

thiagoarrais
Copy link
Contributor

@thiagoarrais thiagoarrais commented Apr 9, 2020

This will add some lints for accuracy on floating point operations suggested by @clarfon in #2040 (fixes #2040).

These are the remaining lints:

  • x.powi(2) => x * x
  • x.logN() / y.logN() => x.logbase(y)
  • x.logbase(E) => x.log()
  • x.logbase(10) => x.log10()
  • x.logbase(2) => x.log2().
  • x * PI / 180 => x.to_radians()
  • x * 180 / PI => x.to_degrees()
  • (x + 1).log() => x.log_1p()
  • sqrt(x * x + y * y) => x.hypot(y)

changelog: Included some accuracy lints for floating point operations

@phansch phansch added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Apr 10, 2020
@flip1995
Copy link
Member

LGTM. Please read up in #4897 on the discussion of naming these lints and which lint should apply to which float expression.

@flip1995 flip1995 added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Apr 15, 2020
@thiagoarrais
Copy link
Contributor Author

I'll make a note to review which lints goes into IMPRECISE_FLOPS or SUBOPTIMAL_FLOPS before removing the WIP marker. I'm not really sure which one goes where right now to be honest.

About naming, I assume I'm ok if I stick to those two names?

@flip1995
Copy link
Member

You can just put the different cases where you think they fit best (and note that in the PR body for easier review please). The names SGTM.

@thiagoarrais
Copy link
Contributor Author

thiagoarrais commented Apr 27, 2020

I'm trying to make sqrt(x * x + y * y) => x.hypot(y) but bumping into the error "could not replace range (...) maybe parts of it were already replaced". AFAIK, that is happening because x * x + y * y already gets fixed into x.mul_add(x, x * y).

Any tips on how to avoid that? Should I change the mul_add lint to skip that case?

@flip1995
Copy link
Member

I think making mul_add skip the case is the best solution here.

@thiagoarrais
Copy link
Contributor Author

Still working on this. Quick question: how do you folks usually deal with cross lint errors?

I've got a problem because the lint for x.powi(2) + y produces x * x + y, which is rejected by the mul_add lint. It wants the expression to be x.mul_add(x, y).

@flip1995
Copy link
Member

If a lint suggests code that again gets linted by another lint, that is not a problem.

Only if 2 lints trigger on the same code simultaneously is a problem. In those cases one lint should take preference.

@thiagoarrais
Copy link
Contributor Author

Should I do anything differently here, then? I'm getting the error below when I run TESTNAME=floating_point_powi cargo uitest with my current code:

error: failed to compile fixed code
status: exit code: 1
command: "/data/rust-clippy/target/debug/clippy-driver" "tests/ui/floating_point_powi.fixed" "-L" "/data/rust-clippy/target/debug/test_build_base" "--target=x86_64-unknown-linux-gnu" "--error-format" "json" "-C" "prefer-dynamic" "-o" "/data/rust-clippy/target/debug/test_build_base/floating_point_powi.stage-id" "-L" "/data/rust-clippy/target/debug/deps" "-L" "/data/rust-clippy/target/debug/deps" "-Dwarnings" "-Zui-testing" "--extern" "regex=/data/rust-clippy/target/debug/deps/libregex-c986894c984c8eac.rlib" "--extern" "serde=/data/rust-clippy/target/debug/deps/libserde-bdb8a48e18d84a1f.rlib" "--extern" "clippy_lints=/data/rust-clippy/target/debug/deps/libclippy_lints-0033f303daa204f4.rlib" "-L" "/data/rust-clippy/target/debug/test_build_base/floating_point_powi.stage-id.aux"
stdout:
------------------------------------------

------------------------------------------
stderr:
------------------------------------------
{"message":"multiply and add expressions can be calculated more efficiently and accurately","code":{"code":"clippy::suboptimal_flops","explanation":null},"level":"error","spans":[{"file_name":"tests/ui/floating_point_powi.fixed","byte_start":192,"byte_end":203,"line_start":11,"line_end":11,"column_start":13,"column_end":24,"is_primary":true,"text":[{"text":"    let _ = (x * x + y).sqrt();","highlight_start":13,"highlight_end":24}],"label":null,"suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[{"message":"`-D clippy::suboptimal-flops` implied by `-D warnings`","code":null,"level":"note","spans":[],"children":[],"rendered":null},{"message":"consider using","code":null,"level":"help","spans":[{"file_name":"tests/ui/floating_point_powi.fixed","byte_start":192,"byte_end":203,"line_start":11,"line_end":11,"column_start":13,"column_end":24,"is_primary":true,"text":[{"text":"    let _ = (x * x + y).sqrt();","highlight_start":13,"highlight_end":24}],"label":null,"suggested_replacement":"x.mul_add(x, y)","suggestion_applicability":"MachineApplicable","expansion":null}],"children":[],"rendered":null}],"rendered":"error: multiply and add expressions can be calculated more efficiently and accurately\n  --> tests/ui/floating_point_powi.fixed:11:13\n   |\nLL |     let _ = (x * x + y).sqrt();\n   |             ^^^^^^^^^^^ help: consider using: `x.mul_add(x, y)`\n   |\n   = note: `-D clippy::suboptimal-flops` implied by `-D warnings`\n\n"}
{"message":"multiply and add expressions can be calculated more efficiently and accurately","code":{"code":"clippy::suboptimal_flops","explanation":null},"level":"error","spans":[{"file_name":"tests/ui/floating_point_powi.fixed","byte_start":224,"byte_end":235,"line_start":12,"line_end":12,"column_start":13,"column_end":24,"is_primary":true,"text":[{"text":"    let _ = (x + y * y).sqrt();","highlight_start":13,"highlight_end":24}],"label":null,"suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[{"message":"consider using","code":null,"level":"help","spans":[{"file_name":"tests/ui/floating_point_powi.fixed","byte_start":224,"byte_end":235,"line_start":12,"line_end":12,"column_start":13,"column_end":24,"is_primary":true,"text":[{"text":"    let _ = (x + y * y).sqrt();","highlight_start":13,"highlight_end":24}],"label":null,"suggested_replacement":"y.mul_add(y, x)","suggestion_applicability":"MachineApplicable","expansion":null}],"children":[],"rendered":null}],"rendered":"error: multiply and add expressions can be calculated more efficiently and accurately\n  --> tests/ui/floating_point_powi.fixed:12:13\n   |\nLL |     let _ = (x + y * y).sqrt();\n   |             ^^^^^^^^^^^ help: consider using: `y.mul_add(y, x)`\n\n"}
{"message":"aborting due to 2 previous errors","code":null,"level":"error","spans":[],"children":[],"rendered":"error: aborting due to 2 previous errors\n\n"}

@flip1995
Copy link
Member

Just allow the other lint in this test file (or just for this test case).

@thiagoarrais thiagoarrais force-pushed the issue-2040 branch 4 times, most recently from 12befdd to 149c3ea Compare June 1, 2020 19:08
@flip1995
Copy link
Member

flip1995 commented Jun 2, 2020

FYI: since this is a big PR, which seems to be under active development, I won't review every change here (At least as long as there is WIP in the title). @thiagoarrais if you have questions or want to have a review anyway, feel free to ask anytime though.

@thiagoarrais
Copy link
Contributor Author

No problem! Thanks for the feedback!

@thiagoarrais
Copy link
Contributor Author

I'm removing the WIP marker as this is ready for review.

Some of the proposed lints were already covered in master:

  • x.logbase(E) => x.log()
  • x.logbase(10) => x.log10()
  • x.logbase(2) => x.log2()
  • (x + 1).log() => x.log_1p()

And here is a summary of which lint went into each group. It wasn't really clear to me what should go where. Please let me know if any of these are in the wrong drawer.

Lint Lint group
x.powi(2) => x * x imprecise_flops
x.logN() / y.logN() => x.logbase(y) suboptimal_flops
x * PI / 180 => x.to_radians() imprecise_flops
x * 180 / PI => x.to_degrees() imprecise_flops
sqrt(x * x + y * y) => x.hypot(y) imprecise_flops

@thiagoarrais thiagoarrais changed the title WIP: Some accuracy lints for floating point operations Some accuracy lints for floating point operations Jun 12, 2020
@flip1995 flip1995 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Jun 12, 2020
@thiagoarrais
Copy link
Contributor Author

Added TODO comment and rebased. This should be good to go.

@flip1995
Copy link
Member

@bors r+

@bors
Copy link
Collaborator

bors commented Jul 13, 2020

📌 Commit 3065201 has been approved by flip1995

@bors
Copy link
Collaborator

bors commented Jul 13, 2020

⌛ Testing commit 3065201 with merge 3749ea4...

bors added a commit that referenced this pull request Jul 13, 2020
Some accuracy lints for floating point operations

This will add some lints for accuracy on floating point operations suggested by @clarfon in #2040 (fixes #2040).

These are the remaining lints:

- [x] x.powi(2) => x * x
- [x] x.logN() / y.logN() => x.logbase(y)
- [x] x.logbase(E) => x.log()
- [x] x.logbase(10) => x.log10()
- [x] x.logbase(2) => x.log2().
- [x] x * PI / 180 => x.to_radians()
- [x] x * 180 / PI => x.to_degrees()
- [x] (x + 1).log() => x.log_1p()
- [x] sqrt(x * x + y * y) => x.hypot(y)

changelog: Included some accuracy lints for floating point operations
@flip1995
Copy link
Member

@bors rollup=iffy

@bors bors mentioned this pull request Jul 13, 2020
@flip1995
Copy link
Member

@bors retry (yeet)

flip1995 added a commit to flip1995/rust-clippy that referenced this pull request Jul 13, 2020
Some accuracy lints for floating point operations

This will add some lints for accuracy on floating point operations suggested by @clarfon in rust-lang#2040 (fixes rust-lang#2040).

These are the remaining lints:

- [x] x.powi(2) => x * x
- [x] x.logN() / y.logN() => x.logbase(y)
- [x] x.logbase(E) => x.log()
- [x] x.logbase(10) => x.log10()
- [x] x.logbase(2) => x.log2().
- [x] x * PI / 180 => x.to_radians()
- [x] x * 180 / PI => x.to_degrees()
- [x] (x + 1).log() => x.log_1p()
- [x] sqrt(x * x + y * y) => x.hypot(y)

changelog: Included some accuracy lints for floating point operations
This was referenced Jul 13, 2020
bors added a commit that referenced this pull request Jul 13, 2020
Some accuracy lints for floating point operations

This will add some lints for accuracy on floating point operations suggested by @clarfon in #2040 (fixes #2040).

These are the remaining lints:

- [x] x.powi(2) => x * x
- [x] x.logN() / y.logN() => x.logbase(y)
- [x] x.logbase(E) => x.log()
- [x] x.logbase(10) => x.log10()
- [x] x.logbase(2) => x.log2().
- [x] x * PI / 180 => x.to_radians()
- [x] x * 180 / PI => x.to_degrees()
- [x] (x + 1).log() => x.log_1p()
- [x] sqrt(x * x + y * y) => x.hypot(y)

changelog: Included some accuracy lints for floating point operations
@bors
Copy link
Collaborator

bors commented Jul 13, 2020

⌛ Testing commit 3065201 with merge 52f7940...

@flip1995
Copy link
Member

@bors retry (yeet yeet)

bors added a commit that referenced this pull request Jul 13, 2020
Rollup of 5 pull requests

Successful merges:

 - #5443 (Some accuracy lints for floating point operations)
 - #5752 (Move range_minus_one to pedantic)
 - #5756 (unnecessary_sort_by: avoid linting if key borrows)
 - #5784 (Fix out of bounds access by checking length equality BEFORE accessing by index.)
 - #5786 (fix phrase in new_lint issue template)

Failed merges:

r? @ghost

changelog: rollup
@bors
Copy link
Collaborator

bors commented Jul 13, 2020

⌛ Testing commit 3065201 with merge 4b87008...

@bors
Copy link
Collaborator

bors commented Jul 13, 2020

☔ The latest upstream changes (presumably #5792) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Suggest more-accurate float functions for hand-written formulas
5 participants