Always print floats with a decimal point with the Debug formatter
This commit is contained in:
parent
b058dc0107
commit
3e98f18280
3 changed files with 15 additions and 10 deletions
|
@ -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
|
// 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.
|
// up using the combined stack space of both functions in some cases.
|
||||||
#[inline(never)]
|
#[inline(never)]
|
||||||
fn float_to_decimal_common_shortest<T>(fmt: &mut Formatter,
|
fn float_to_decimal_common_shortest<T>(fmt: &mut Formatter, num: &T,
|
||||||
num: &T, sign: flt2dec::Sign) -> Result
|
sign: flt2dec::Sign, precision: usize) -> Result
|
||||||
where T: flt2dec::DecodableFloat
|
where T: flt2dec::DecodableFloat
|
||||||
{
|
{
|
||||||
unsafe {
|
unsafe {
|
||||||
// enough for f32 and f64
|
// enough for f32 and f64
|
||||||
let mut buf: [u8; flt2dec::MAX_SIG_DIGITS] = mem::uninitialized();
|
let mut buf: [u8; flt2dec::MAX_SIG_DIGITS] = mem::uninitialized();
|
||||||
let mut parts: [flt2dec::Part; 4] = mem::uninitialized();
|
let mut parts: [flt2dec::Part; 4] = mem::uninitialized();
|
||||||
let formatted = flt2dec::to_shortest_str(flt2dec::strategy::grisu::format_shortest,
|
let formatted = flt2dec::to_shortest_str(flt2dec::strategy::grisu::format_shortest, *num,
|
||||||
*num, sign, 0, false, &mut buf, &mut parts);
|
sign, precision, false, &mut buf, &mut parts);
|
||||||
fmt.pad_formatted_parts(&formatted)
|
fmt.pad_formatted_parts(&formatted)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Common code of floating point Debug and Display.
|
// 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
|
where T: flt2dec::DecodableFloat
|
||||||
{
|
{
|
||||||
let force_sign = fmt.sign_plus();
|
let force_sign = fmt.sign_plus();
|
||||||
|
@ -61,7 +62,7 @@ fn float_to_decimal_common<T>(fmt: &mut Formatter, num: &T, negative_zero: bool)
|
||||||
if let Some(precision) = fmt.precision {
|
if let Some(precision) = fmt.precision {
|
||||||
float_to_decimal_common_exact(fmt, num, sign, precision)
|
float_to_decimal_common_exact(fmt, num, sign, precision)
|
||||||
} else {
|
} else {
|
||||||
float_to_decimal_common_shortest(fmt, num, sign)
|
float_to_decimal_common_shortest(fmt, num, sign, min_precision)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -125,14 +126,14 @@ macro_rules! floating {
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
impl Debug for $ty {
|
impl Debug for $ty {
|
||||||
fn fmt(&self, fmt: &mut Formatter) -> Result {
|
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")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
impl Display for $ty {
|
impl Display for $ty {
|
||||||
fn fmt(&self, fmt: &mut Formatter) -> Result {
|
fn fmt(&self, fmt: &mut Formatter) -> Result {
|
||||||
float_to_decimal_common(fmt, self, false)
|
float_to_decimal_common(fmt, self, false, 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,8 @@ fn test_format_f64() {
|
||||||
assert_eq!("1.23456789e3", format!("{:e}", 1234.56789f64));
|
assert_eq!("1.23456789e3", format!("{:e}", 1234.56789f64));
|
||||||
assert_eq!("1.23456789E6", format!("{:E}", 1234567.89f64));
|
assert_eq!("1.23456789E6", format!("{:E}", 1234567.89f64));
|
||||||
assert_eq!("1.23456789E3", format!("{:E}", 1234.56789f64));
|
assert_eq!("1.23456789E3", format!("{:E}", 1234.56789f64));
|
||||||
|
assert_eq!("0.0", format!("{:?}", 0.0f64));
|
||||||
|
assert_eq!("1.01", format!("{:?}", 1.01f64));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -34,4 +36,6 @@ fn test_format_f32() {
|
||||||
assert_eq!("1.2345679e3", format!("{:e}", 1234.56789f32));
|
assert_eq!("1.2345679e3", format!("{:e}", 1234.56789f32));
|
||||||
assert_eq!("1.2345679E6", format!("{:E}", 1234567.89f32));
|
assert_eq!("1.2345679E6", format!("{:E}", 1234567.89f32));
|
||||||
assert_eq!("1.2345679E3", format!("{:E}", 1234.56789f32));
|
assert_eq!("1.2345679E3", format!("{:E}", 1234.56789f32));
|
||||||
|
assert_eq!("0.0", format!("{:?}", 0.0f32));
|
||||||
|
assert_eq!("1.01", format!("{:?}", 1.01f32));
|
||||||
}
|
}
|
||||||
|
|
|
@ -158,8 +158,8 @@ pub fn main() {
|
||||||
|
|
||||||
// Float edge cases
|
// 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");
|
t!(format!("{:?}", 0.0), "0.0");
|
||||||
|
|
||||||
// sign aware zero padding
|
// sign aware zero padding
|
||||||
t!(format!("{:<3}", 1), "1 ");
|
t!(format!("{:<3}", 1), "1 ");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue