Skip to content

Commit

Permalink
Rollup merge of rust-lang#46831 - Diggsey:float-debug-fmt, r=dtolnay
Browse files Browse the repository at this point in the history
Always `Debug` floats with a decimal point

Fixes rust-lang#30967

r? @dtolnay
  • Loading branch information
kennytm authored Dec 20, 2017
2 parents 5e98112 + 3e98f18 commit 16095b3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
17 changes: 9 additions & 8 deletions src/libcore/fmt/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,23 @@ fn float_to_decimal_common_exact<T>(fmt: &mut Formatter, num: &T,
// Don't inline this so callers that call both this and the above won't wind
// up using the combined stack space of both functions in some cases.
#[inline(never)]
fn float_to_decimal_common_shortest<T>(fmt: &mut Formatter,
num: &T, sign: flt2dec::Sign) -> Result
fn float_to_decimal_common_shortest<T>(fmt: &mut Formatter, num: &T,
sign: flt2dec::Sign, precision: usize) -> Result
where T: flt2dec::DecodableFloat
{
unsafe {
// enough for f32 and f64
let mut buf: [u8; flt2dec::MAX_SIG_DIGITS] = mem::uninitialized();
let mut parts: [flt2dec::Part; 4] = mem::uninitialized();
let formatted = flt2dec::to_shortest_str(flt2dec::strategy::grisu::format_shortest,
*num, sign, 0, false, &mut buf, &mut parts);
let formatted = flt2dec::to_shortest_str(flt2dec::strategy::grisu::format_shortest, *num,
sign, precision, false, &mut buf, &mut parts);
fmt.pad_formatted_parts(&formatted)
}
}

// Common code of floating point Debug and Display.
fn float_to_decimal_common<T>(fmt: &mut Formatter, num: &T, negative_zero: bool) -> Result
fn float_to_decimal_common<T>(fmt: &mut Formatter, num: &T,
negative_zero: bool, min_precision: usize) -> Result
where T: flt2dec::DecodableFloat
{
let force_sign = fmt.sign_plus();
Expand All @@ -61,7 +62,7 @@ fn float_to_decimal_common<T>(fmt: &mut Formatter, num: &T, negative_zero: bool)
if let Some(precision) = fmt.precision {
float_to_decimal_common_exact(fmt, num, sign, precision)
} else {
float_to_decimal_common_shortest(fmt, num, sign)
float_to_decimal_common_shortest(fmt, num, sign, min_precision)
}
}

Expand Down Expand Up @@ -125,14 +126,14 @@ macro_rules! floating {
#[stable(feature = "rust1", since = "1.0.0")]
impl Debug for $ty {
fn fmt(&self, fmt: &mut Formatter) -> Result {
float_to_decimal_common(fmt, self, true)
float_to_decimal_common(fmt, self, true, 1)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl Display for $ty {
fn fmt(&self, fmt: &mut Formatter) -> Result {
float_to_decimal_common(fmt, self, false)
float_to_decimal_common(fmt, self, false, 0)
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/libcore/tests/fmt/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ fn test_format_f64() {
assert_eq!("1.23456789e3", format!("{:e}", 1234.56789f64));
assert_eq!("1.23456789E6", format!("{:E}", 1234567.89f64));
assert_eq!("1.23456789E3", format!("{:E}", 1234.56789f64));
assert_eq!("0.0", format!("{:?}", 0.0f64));
assert_eq!("1.01", format!("{:?}", 1.01f64));
}

#[test]
Expand All @@ -34,4 +36,6 @@ fn test_format_f32() {
assert_eq!("1.2345679e3", format!("{:e}", 1234.56789f32));
assert_eq!("1.2345679E6", format!("{:E}", 1234567.89f32));
assert_eq!("1.2345679E3", format!("{:E}", 1234.56789f32));
assert_eq!("0.0", format!("{:?}", 0.0f32));
assert_eq!("1.01", format!("{:?}", 1.01f32));
}
4 changes: 2 additions & 2 deletions src/test/run-pass/ifmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ pub fn main() {

// Float edge cases
t!(format!("{}", -0.0), "0");
t!(format!("{:?}", -0.0), "-0");
t!(format!("{:?}", 0.0), "0");
t!(format!("{:?}", -0.0), "-0.0");
t!(format!("{:?}", 0.0), "0.0");

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

0 comments on commit 16095b3

Please sign in to comment.