Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix inconsistent rounding of 0.5 when formatted to 0 decimal places #102935

Merged
merged 2 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library/core/src/num/flt2dec/strategy/dragon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ pub fn format_exact<'a>(
if order == Ordering::Greater
|| (order == Ordering::Equal
// SAFETY: `buf[len-1]` is initialized.
&& (len == 0 || unsafe { buf[len - 1].assume_init() } & 1 == 1))
&& len > 0 && unsafe { buf[len - 1].assume_init() } & 1 == 1)
{
// if rounding up changes the length, the exponent should also change.
// but we've been requested a fixed number of digits, so do not alter the buffer...
Expand Down
4 changes: 2 additions & 2 deletions library/core/tests/fmt/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fn test_format_f64() {
assert_eq!("10", format!("{:.0}", 9.9f64));
assert_eq!("9.8", format!("{:.1}", 9.849f64));
assert_eq!("9.9", format!("{:.1}", 9.851f64));
assert_eq!("1", format!("{:.0}", 0.5f64));
assert_eq!("0", format!("{:.0}", 0.5f64));
scottmcm marked this conversation as resolved.
Show resolved Hide resolved
assert_eq!("1.23456789e6", format!("{:e}", 1234567.89f64));
assert_eq!("1.23456789e3", format!("{:e}", 1234.56789f64));
assert_eq!("1.23456789E6", format!("{:E}", 1234567.89f64));
Expand All @@ -31,7 +31,7 @@ fn test_format_f32() {
assert_eq!("10", format!("{:.0}", 9.9f32));
assert_eq!("9.8", format!("{:.1}", 9.849f32));
assert_eq!("9.9", format!("{:.1}", 9.851f32));
assert_eq!("1", format!("{:.0}", 0.5f32));
assert_eq!("0", format!("{:.0}", 0.5f32));
assert_eq!("1.2345679e6", format!("{:e}", 1234567.89f32));
assert_eq!("1.2345679e3", format!("{:e}", 1234.56789f32));
assert_eq!("1.2345679E6", format!("{:E}", 1234567.89f32));
Expand Down
4 changes: 2 additions & 2 deletions library/core/tests/num/flt2dec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ where

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

assert_eq!(to_string(f, 0.5, Minus, 0), "1");
assert_eq!(to_string(f, 0.5, Minus, 0), "0");
assert_eq!(to_string(f, 0.5, Minus, 1), "0.5");
assert_eq!(to_string(f, 0.5, Minus, 2), "0.50");
assert_eq!(to_string(f, 0.5, Minus, 3), "0.500");
Expand Down