Skip to content

Commit af55deb

Browse files
Correct max_digits for floats
1 parent 871ad80 commit af55deb

File tree

4 files changed

+69
-89
lines changed

4 files changed

+69
-89
lines changed

clippy_lints/src/float_literal.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,19 @@ impl<'tcx> LateLintPass<'tcx> for FloatLiteral {
127127
}
128128
}
129129

130+
/// The maximum number of significant decimal digits a float can contain.
131+
/// f32: 23+1 bit significand, storing 6 to 9 decimal digits
132+
/// f64: 52+1 bit significand, storing 15 to 17 decimal digits
133+
/// Above the range's lower bound, precision in the decimal string may be lost,
134+
/// but it is recommended to use the maximum number of digits, not the minimum,
135+
/// when feeding these to a parser, as it preserves the accuracy in binary.
136+
/// In addition, suggesting someone truncate an 8 decimal digit string
137+
/// may change the result of the program if they apply it.
130138
#[must_use]
131139
fn max_digits(fty: FloatTy) -> u32 {
132140
match fty {
133-
FloatTy::F32 => f32::DIGITS,
134-
FloatTy::F64 => f64::DIGITS,
141+
FloatTy::F32 => 9,
142+
FloatTy::F64 => 17,
135143
}
136144
}
137145

tests/ui/excessive_precision.fixed

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,22 @@ fn main() {
88
const GOOD32_SM: f32 = 0.000_000_000_1;
99
const GOOD32_DOT: f32 = 10_000_000_000.0;
1010
const GOOD32_EDGE: f32 = 1.000_000_8;
11+
// the Kahan Krew wuz here
12+
const GOOD32_IEEE754_1: f32 = 0.123_456_789_f32;
13+
const GOOD32_IEEE754_2: f32 = 0.123_456_789;
14+
const GOOD32_IEEE754_3: f32 = 1.000_000_9;
15+
16+
const BAD32_1: f32 = 0.1;
17+
const BAD32_2: f32 = 0.123_456_79;
18+
1119
const GOOD64: f64 = 0.123_456_789_012;
1220
const GOOD64_SM: f32 = 0.000_000_000_000_000_1;
1321
const GOOD64_DOT: f32 = 10_000_000_000_000_000.0;
22+
const GOOD64_IEEE754_1: f64 = 0.123_456_789_012_345_67f64;
23+
const GOOD64_IEEE754_2: f64 = 0.123_456_789_012_345_67;
1424

15-
const BAD32_1: f32 = 0.123_456_79_f32;
16-
const BAD32_2: f32 = 0.123_456_79;
17-
const BAD32_3: f32 = 0.1;
18-
const BAD32_EDGE: f32 = 1.000_001;
19-
20-
const BAD64_1: f64 = 0.123_456_789_012_345_66_f64;
21-
const BAD64_2: f64 = 0.123_456_789_012_345_66;
22-
const BAD64_3: f64 = 0.1;
25+
const BAD64_1: f64 = 0.1;
26+
const BAD64_2: f64 = 0.123_456_789_012_345_68;
2327

2428
// Literal as param
2529
println!("{:?}", 8.888_888_888_888_89);
@@ -37,16 +41,16 @@ fn main() {
3741
let bad32_suf: f32 = 1.123_456_8_f32;
3842
let bad32_inf = 1.123_456_8_f32;
3943

40-
let bad64: f64 = 0.123_456_789_012_345_66;
41-
let bad64_suf: f64 = 0.123_456_789_012_345_66_f64;
42-
let bad64_inf = 0.123_456_789_012_345_66;
44+
let bad64: f64 = 0.123_456_789_012_345_67;
45+
let bad64_suf: f64 = 0.123_456_789_012_345_67f64;
46+
let bad64_inf = 0.123_456_789_012_345_67;
4347

4448
// Vectors
45-
let good_vec32: Vec<f32> = vec![0.123_456];
46-
let good_vec64: Vec<f64> = vec![0.123_456_789];
47-
49+
let good_vec32: Vec<f32> = vec![0.123_456_789];
4850
let bad_vec32: Vec<f32> = vec![0.123_456_79];
49-
let bad_vec64: Vec<f64> = vec![0.123_456_789_123_456_78];
51+
52+
let good_vec64: Vec<f64> = vec![0.123_456_789_012_345_67];
53+
let bad_vec64: Vec<f64> = vec![0.123_456_789_012_345_68];
5054

5155
// Exponential float notation
5256
let good_e32: f32 = 1e-10;

tests/ui/excessive_precision.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,22 @@ fn main() {
88
const GOOD32_SM: f32 = 0.000_000_000_1;
99
const GOOD32_DOT: f32 = 10_000_000_000.0;
1010
const GOOD32_EDGE: f32 = 1.000_000_8;
11+
// the Kahan Krew wuz here
12+
const GOOD32_IEEE754_1: f32 = 0.123_456_789_f32;
13+
const GOOD32_IEEE754_2: f32 = 0.123_456_789;
14+
const GOOD32_IEEE754_3: f32 = 1.000_000_9;
15+
16+
const BAD32_1: f32 = 0.100_000_000_000_1;
17+
const BAD32_2: f32 = 0.123_456_789_012;
18+
1119
const GOOD64: f64 = 0.123_456_789_012;
1220
const GOOD64_SM: f32 = 0.000_000_000_000_000_1;
1321
const GOOD64_DOT: f32 = 10_000_000_000_000_000.0;
22+
const GOOD64_IEEE754_1: f64 = 0.123_456_789_012_345_67f64;
23+
const GOOD64_IEEE754_2: f64 = 0.123_456_789_012_345_67;
1424

15-
const BAD32_1: f32 = 0.123_456_789_f32;
16-
const BAD32_2: f32 = 0.123_456_789;
17-
const BAD32_3: f32 = 0.100_000_000_000_1;
18-
const BAD32_EDGE: f32 = 1.000_000_9;
19-
20-
const BAD64_1: f64 = 0.123_456_789_012_345_67f64;
21-
const BAD64_2: f64 = 0.123_456_789_012_345_67;
22-
const BAD64_3: f64 = 0.100_000_000_000_000_000_1;
25+
const BAD64_1: f64 = 0.100_000_000_000_000_000_1;
26+
const BAD64_2: f64 = 0.123_456_789_012_345_678_9;
2327

2428
// Literal as param
2529
println!("{:?}", 8.888_888_888_888_888_888_888);
@@ -42,11 +46,11 @@ fn main() {
4246
let bad64_inf = 0.123_456_789_012_345_67;
4347

4448
// Vectors
45-
let good_vec32: Vec<f32> = vec![0.123_456];
46-
let good_vec64: Vec<f64> = vec![0.123_456_789];
49+
let good_vec32: Vec<f32> = vec![0.123_456_789];
50+
let bad_vec32: Vec<f32> = vec![0.123_456_789_012];
4751

48-
let bad_vec32: Vec<f32> = vec![0.123_456_789];
49-
let bad_vec64: Vec<f64> = vec![0.123_456_789_123_456_789];
52+
let good_vec64: Vec<f64> = vec![0.123_456_789_012_345_67];
53+
let bad_vec64: Vec<f64> = vec![0.123_456_789_012_345_678_9];
5054

5155
// Exponential float notation
5256
let good_e32: f32 = 1e-10;

tests/ui/excessive_precision.stderr

Lines changed: 24 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,76 @@
1-
error: float has excessive precision
2-
--> $DIR/excessive_precision.rs:15:26
3-
|
4-
LL | const BAD32_1: f32 = 0.123_456_789_f32;
5-
| ^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_79_f32`
6-
|
7-
= note: `-D clippy::excessive-precision` implied by `-D warnings`
8-
91
error: float has excessive precision
102
--> $DIR/excessive_precision.rs:16:26
113
|
12-
LL | const BAD32_2: f32 = 0.123_456_789;
13-
| ^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_79`
14-
15-
error: float has excessive precision
16-
--> $DIR/excessive_precision.rs:17:26
17-
|
18-
LL | const BAD32_3: f32 = 0.100_000_000_000_1;
4+
LL | const BAD32_1: f32 = 0.100_000_000_000_1;
195
| ^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.1`
20-
21-
error: float has excessive precision
22-
--> $DIR/excessive_precision.rs:18:29
236
|
24-
LL | const BAD32_EDGE: f32 = 1.000_000_9;
25-
| ^^^^^^^^^^^ help: consider changing the type or truncating it to: `1.000_001`
7+
= note: `-D clippy::excessive-precision` implied by `-D warnings`
268

279
error: float has excessive precision
28-
--> $DIR/excessive_precision.rs:20:26
10+
--> $DIR/excessive_precision.rs:17:26
2911
|
30-
LL | const BAD64_1: f64 = 0.123_456_789_012_345_67f64;
31-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_789_012_345_66_f64`
12+
LL | const BAD32_2: f32 = 0.123_456_789_012;
13+
| ^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_79`
3214

3315
error: float has excessive precision
34-
--> $DIR/excessive_precision.rs:21:26
16+
--> $DIR/excessive_precision.rs:25:26
3517
|
36-
LL | const BAD64_2: f64 = 0.123_456_789_012_345_67;
37-
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_789_012_345_66`
18+
LL | const BAD64_1: f64 = 0.100_000_000_000_000_000_1;
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.1`
3820

3921
error: float has excessive precision
40-
--> $DIR/excessive_precision.rs:22:26
22+
--> $DIR/excessive_precision.rs:26:26
4123
|
42-
LL | const BAD64_3: f64 = 0.100_000_000_000_000_000_1;
43-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.1`
24+
LL | const BAD64_2: f64 = 0.123_456_789_012_345_678_9;
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_789_012_345_68`
4426

4527
error: float has excessive precision
46-
--> $DIR/excessive_precision.rs:25:22
28+
--> $DIR/excessive_precision.rs:29:22
4729
|
4830
LL | println!("{:?}", 8.888_888_888_888_888_888_888);
4931
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `8.888_888_888_888_89`
5032

5133
error: float has excessive precision
52-
--> $DIR/excessive_precision.rs:36:22
34+
--> $DIR/excessive_precision.rs:40:22
5335
|
5436
LL | let bad32: f32 = 1.123_456_789;
5537
| ^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `1.123_456_8`
5638

5739
error: float has excessive precision
58-
--> $DIR/excessive_precision.rs:37:26
40+
--> $DIR/excessive_precision.rs:41:26
5941
|
6042
LL | let bad32_suf: f32 = 1.123_456_789_f32;
6143
| ^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `1.123_456_8_f32`
6244

6345
error: float has excessive precision
64-
--> $DIR/excessive_precision.rs:38:21
46+
--> $DIR/excessive_precision.rs:42:21
6547
|
6648
LL | let bad32_inf = 1.123_456_789_f32;
6749
| ^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `1.123_456_8_f32`
6850

6951
error: float has excessive precision
70-
--> $DIR/excessive_precision.rs:40:22
71-
|
72-
LL | let bad64: f64 = 0.123_456_789_012_345_67;
73-
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_789_012_345_66`
74-
75-
error: float has excessive precision
76-
--> $DIR/excessive_precision.rs:41:26
77-
|
78-
LL | let bad64_suf: f64 = 0.123_456_789_012_345_67f64;
79-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_789_012_345_66_f64`
80-
81-
error: float has excessive precision
82-
--> $DIR/excessive_precision.rs:42:21
83-
|
84-
LL | let bad64_inf = 0.123_456_789_012_345_67;
85-
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_789_012_345_66`
86-
87-
error: float has excessive precision
88-
--> $DIR/excessive_precision.rs:48:36
52+
--> $DIR/excessive_precision.rs:50:36
8953
|
90-
LL | let bad_vec32: Vec<f32> = vec![0.123_456_789];
91-
| ^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_79`
54+
LL | let bad_vec32: Vec<f32> = vec![0.123_456_789_012];
55+
| ^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_79`
9256

9357
error: float has excessive precision
94-
--> $DIR/excessive_precision.rs:49:36
58+
--> $DIR/excessive_precision.rs:53:36
9559
|
96-
LL | let bad_vec64: Vec<f64> = vec![0.123_456_789_123_456_789];
97-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_789_123_456_78`
60+
LL | let bad_vec64: Vec<f64> = vec![0.123_456_789_012_345_678_9];
61+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_789_012_345_68`
9862

9963
error: float has excessive precision
100-
--> $DIR/excessive_precision.rs:53:24
64+
--> $DIR/excessive_precision.rs:57:24
10165
|
10266
LL | let bad_e32: f32 = 1.123_456_788_888e-10;
10367
| ^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `1.123_456_8e-10`
10468

10569
error: float has excessive precision
106-
--> $DIR/excessive_precision.rs:56:27
70+
--> $DIR/excessive_precision.rs:60:27
10771
|
10872
LL | let bad_bige32: f32 = 1.123_456_788_888E-10;
10973
| ^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `1.123_456_8E-10`
11074

111-
error: aborting due to 18 previous errors
75+
error: aborting due to 12 previous errors
11276

0 commit comments

Comments
 (0)