Skip to content

Commit 969384c

Browse files
authored
Merge pull request #890 from frvannes16/fix/888-std-floats
fix(clippy1): Trigger approx_constant lint rule instead of downgraded float_cmp.
2 parents acaee79 + f2650de commit 969384c

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

exercises/clippy/clippy1.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@
88

99
// I AM NOT DONE
1010

11+
use std::f32;
12+
1113
fn main() {
12-
let x = 1.2331f64;
13-
let y = 1.2332f64;
14-
if y != x {
15-
println!("Success!");
16-
}
14+
let pi = 3.14f32;
15+
let radius = 5.00f32;
16+
17+
let area = pi * f32::powi(radius, 2);
18+
19+
println!(
20+
"The area of a circle with radius {:.2} is {:.5}!",
21+
radius, area
22+
)
1723
}

info.toml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -906,15 +906,15 @@ name = "clippy1"
906906
path = "exercises/clippy/clippy1.rs"
907907
mode = "clippy"
908908
hint = """
909-
Not every floating point value can be represented exactly in binary values in
910-
memory. Take a look at the description of
911-
https://doc.rust-lang.org/stable/std/primitive.f32.html
912-
When using the binary compare operators with floating points you won't compare
913-
the floating point values but the binary representation in memory. This is
914-
usually not what you would like to do.
909+
Rust stores the highest precision version of any long or inifinite precision
910+
mathematical constants in the rust standard library.
911+
https://doc.rust-lang.org/stable/std/f32/consts/index.html
912+
913+
We may be tempted to use our own approximations for certain mathematical constants,
914+
but clippy recognizes those imprecise mathematical constants as a source of
915+
potential error.
915916
See the suggestions of the clippy warning in compile output and use the
916-
machine epsilon value...
917-
https://doc.rust-lang.org/stable/std/primitive.f32.html#associatedconstant.EPSILON"""
917+
appropriate replacement constant from std::f32::consts..."""
918918

919919
[[exercises]]
920920
name = "clippy2"

0 commit comments

Comments
 (0)