-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Some accuracy lints for floating point operations #5443
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a6f1af7
Lint for x.powi(2) => x * x
thiagoarrais f627984
Lint (x * x + y * y).sqrt() => x.hypot(y)
thiagoarrais 0c8afa3
Lint x.log(b) / y.log(b) => x.log(y)
thiagoarrais 076ec87
Lint for to_radians and to_degrees
thiagoarrais f559682
Better copy for lint message
thiagoarrais db7bc6b
Place radian lints under suboptimal_flops
thiagoarrais 6dc066f
Includes TODO comment for hypot lint
thiagoarrais 6be9491
Reclassify powi(2) lint under suboptimal_flops
thiagoarrais 3065201
Includes TODO for constants equivalent to π/180
thiagoarrais File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// run-rustfix | ||
#![warn(clippy::imprecise_flops)] | ||
|
||
fn main() { | ||
let x = 3f32; | ||
let y = 4f32; | ||
let _ = x.hypot(y); | ||
let _ = (x + 1f32).hypot(y); | ||
let _ = x.hypot(y); | ||
// Cases where the lint shouldn't be applied | ||
// TODO: linting this adds some complexity, but could be done | ||
let _ = x.mul_add(x, y * y).sqrt(); | ||
thiagoarrais marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let _ = (x * 4f32 + y * y).sqrt(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// run-rustfix | ||
#![warn(clippy::imprecise_flops)] | ||
|
||
fn main() { | ||
let x = 3f32; | ||
let y = 4f32; | ||
let _ = (x * x + y * y).sqrt(); | ||
let _ = ((x + 1f32) * (x + 1f32) + y * y).sqrt(); | ||
let _ = (x.powi(2) + y.powi(2)).sqrt(); | ||
// Cases where the lint shouldn't be applied | ||
// TODO: linting this adds some complexity, but could be done | ||
let _ = x.mul_add(x, y * y).sqrt(); | ||
let _ = (x * 4f32 + y * y).sqrt(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
error: hypotenuse can be computed more accurately | ||
--> $DIR/floating_point_hypot.rs:7:13 | ||
| | ||
LL | let _ = (x * x + y * y).sqrt(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.hypot(y)` | ||
| | ||
= note: `-D clippy::imprecise-flops` implied by `-D warnings` | ||
|
||
error: hypotenuse can be computed more accurately | ||
--> $DIR/floating_point_hypot.rs:8:13 | ||
| | ||
LL | let _ = ((x + 1f32) * (x + 1f32) + y * y).sqrt(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x + 1f32).hypot(y)` | ||
|
||
error: hypotenuse can be computed more accurately | ||
--> $DIR/floating_point_hypot.rs:9:13 | ||
| | ||
LL | let _ = (x.powi(2) + y.powi(2)).sqrt(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.hypot(y)` | ||
|
||
error: aborting due to 3 previous errors | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.