Skip to content

Commit 8487444

Browse files
committedOct 11, 2022
Fix inconsistent rounding of 0.5 when formatted to 0 decimal places
1 parent db0597f commit 8487444

File tree

3 files changed

+5
-5
lines changed

3 files changed

+5
-5
lines changed
 

‎library/core/src/num/flt2dec/strategy/dragon.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ pub fn format_exact<'a>(
366366
if order == Ordering::Greater
367367
|| (order == Ordering::Equal
368368
// SAFETY: `buf[len-1]` is initialized.
369-
&& (len == 0 || unsafe { buf[len - 1].assume_init() } & 1 == 1))
369+
&& len > 0 && unsafe { buf[len - 1].assume_init() } & 1 == 1)
370370
{
371371
// if rounding up changes the length, the exponent should also change.
372372
// but we've been requested a fixed number of digits, so do not alter the buffer...

‎library/core/tests/fmt/float.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fn test_format_f64() {
55
assert_eq!("10", format!("{:.0}", 9.9f64));
66
assert_eq!("9.8", format!("{:.1}", 9.849f64));
77
assert_eq!("9.9", format!("{:.1}", 9.851f64));
8-
assert_eq!("1", format!("{:.0}", 0.5f64));
8+
assert_eq!("0", format!("{:.0}", 0.5f64));
99
assert_eq!("1.23456789e6", format!("{:e}", 1234567.89f64));
1010
assert_eq!("1.23456789e3", format!("{:e}", 1234.56789f64));
1111
assert_eq!("1.23456789E6", format!("{:E}", 1234567.89f64));
@@ -31,7 +31,7 @@ fn test_format_f32() {
3131
assert_eq!("10", format!("{:.0}", 9.9f32));
3232
assert_eq!("9.8", format!("{:.1}", 9.849f32));
3333
assert_eq!("9.9", format!("{:.1}", 9.851f32));
34-
assert_eq!("1", format!("{:.0}", 0.5f32));
34+
assert_eq!("0", format!("{:.0}", 0.5f32));
3535
assert_eq!("1.2345679e6", format!("{:e}", 1234567.89f32));
3636
assert_eq!("1.2345679e3", format!("{:e}", 1234.56789f32));
3737
assert_eq!("1.2345679E6", format!("{:E}", 1234567.89f32));

‎library/core/tests/num/flt2dec/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ where
138138

139139
// check exact rounding for zero- and negative-width cases
140140
let start;
141-
if expected[0] >= b'5' {
141+
if expected[0] > b'5' {
142142
try_fixed!(f(&decoded) => &mut buf, expectedk, b"1", expectedk + 1;
143143
"zero-width rounding-up mismatch for v={v}: \
144144
actual {actual:?}, expected {expected:?}",
@@ -1007,7 +1007,7 @@ where
10071007
assert_eq!(to_string(f, 999.5, Minus, 3), "999.500");
10081008
assert_eq!(to_string(f, 999.5, Minus, 30), "999.500000000000000000000000000000");
10091009

1010-
assert_eq!(to_string(f, 0.5, Minus, 0), "1");
1010+
assert_eq!(to_string(f, 0.5, Minus, 0), "0");
10111011
assert_eq!(to_string(f, 0.5, Minus, 1), "0.5");
10121012
assert_eq!(to_string(f, 0.5, Minus, 2), "0.50");
10131013
assert_eq!(to_string(f, 0.5, Minus, 3), "0.500");

0 commit comments

Comments
 (0)