Skip to content

Commit 16095b3

Browse files
authored
Rollup merge of rust-lang#46831 - Diggsey:float-debug-fmt, r=dtolnay
Always `Debug` floats with a decimal point Fixes rust-lang#30967 r? @dtolnay
2 parents 5e98112 + 3e98f18 commit 16095b3

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

src/libcore/fmt/float.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,23 @@ fn float_to_decimal_common_exact<T>(fmt: &mut Formatter, num: &T,
3232
// Don't inline this so callers that call both this and the above won't wind
3333
// up using the combined stack space of both functions in some cases.
3434
#[inline(never)]
35-
fn float_to_decimal_common_shortest<T>(fmt: &mut Formatter,
36-
num: &T, sign: flt2dec::Sign) -> Result
35+
fn float_to_decimal_common_shortest<T>(fmt: &mut Formatter, num: &T,
36+
sign: flt2dec::Sign, precision: usize) -> Result
3737
where T: flt2dec::DecodableFloat
3838
{
3939
unsafe {
4040
// enough for f32 and f64
4141
let mut buf: [u8; flt2dec::MAX_SIG_DIGITS] = mem::uninitialized();
4242
let mut parts: [flt2dec::Part; 4] = mem::uninitialized();
43-
let formatted = flt2dec::to_shortest_str(flt2dec::strategy::grisu::format_shortest,
44-
*num, sign, 0, false, &mut buf, &mut parts);
43+
let formatted = flt2dec::to_shortest_str(flt2dec::strategy::grisu::format_shortest, *num,
44+
sign, precision, false, &mut buf, &mut parts);
4545
fmt.pad_formatted_parts(&formatted)
4646
}
4747
}
4848

4949
// Common code of floating point Debug and Display.
50-
fn float_to_decimal_common<T>(fmt: &mut Formatter, num: &T, negative_zero: bool) -> Result
50+
fn float_to_decimal_common<T>(fmt: &mut Formatter, num: &T,
51+
negative_zero: bool, min_precision: usize) -> Result
5152
where T: flt2dec::DecodableFloat
5253
{
5354
let force_sign = fmt.sign_plus();
@@ -61,7 +62,7 @@ fn float_to_decimal_common<T>(fmt: &mut Formatter, num: &T, negative_zero: bool)
6162
if let Some(precision) = fmt.precision {
6263
float_to_decimal_common_exact(fmt, num, sign, precision)
6364
} else {
64-
float_to_decimal_common_shortest(fmt, num, sign)
65+
float_to_decimal_common_shortest(fmt, num, sign, min_precision)
6566
}
6667
}
6768

@@ -125,14 +126,14 @@ macro_rules! floating {
125126
#[stable(feature = "rust1", since = "1.0.0")]
126127
impl Debug for $ty {
127128
fn fmt(&self, fmt: &mut Formatter) -> Result {
128-
float_to_decimal_common(fmt, self, true)
129+
float_to_decimal_common(fmt, self, true, 1)
129130
}
130131
}
131132

132133
#[stable(feature = "rust1", since = "1.0.0")]
133134
impl Display for $ty {
134135
fn fmt(&self, fmt: &mut Formatter) -> Result {
135-
float_to_decimal_common(fmt, self, false)
136+
float_to_decimal_common(fmt, self, false, 0)
136137
}
137138
}
138139

src/libcore/tests/fmt/float.rs

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ fn test_format_f64() {
2020
assert_eq!("1.23456789e3", format!("{:e}", 1234.56789f64));
2121
assert_eq!("1.23456789E6", format!("{:E}", 1234567.89f64));
2222
assert_eq!("1.23456789E3", format!("{:E}", 1234.56789f64));
23+
assert_eq!("0.0", format!("{:?}", 0.0f64));
24+
assert_eq!("1.01", format!("{:?}", 1.01f64));
2325
}
2426

2527
#[test]
@@ -34,4 +36,6 @@ fn test_format_f32() {
3436
assert_eq!("1.2345679e3", format!("{:e}", 1234.56789f32));
3537
assert_eq!("1.2345679E6", format!("{:E}", 1234567.89f32));
3638
assert_eq!("1.2345679E3", format!("{:E}", 1234.56789f32));
39+
assert_eq!("0.0", format!("{:?}", 0.0f32));
40+
assert_eq!("1.01", format!("{:?}", 1.01f32));
3741
}

src/test/run-pass/ifmt.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ pub fn main() {
158158

159159
// Float edge cases
160160
t!(format!("{}", -0.0), "0");
161-
t!(format!("{:?}", -0.0), "-0");
162-
t!(format!("{:?}", 0.0), "0");
161+
t!(format!("{:?}", -0.0), "-0.0");
162+
t!(format!("{:?}", 0.0), "0.0");
163163

164164
// sign aware zero padding
165165
t!(format!("{:<3}", 1), "1 ");

0 commit comments

Comments
 (0)