forked from rust-lang/rust
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#5443 - thiagoarrais:issue-2040, r=flip1995
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
- Loading branch information
Showing
22 changed files
with
499 additions
and
32 deletions.
There are no files selected for viewing
This file contains 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 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(); | ||
let _ = (x * 4f32 + y * y).sqrt(); | ||
} |
This file contains 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 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 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 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.