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 FixedDecimal rounding bug; add more tests #3644

Merged
merged 7 commits into from
Jul 7, 2023
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
28 changes: 23 additions & 5 deletions utils/fixed_decimal/src/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,12 @@ impl FixedDecimal {
/// assert_eq!("1", dec.to_string());
/// ```
pub fn trunc(&mut self, position: i16) {
self.trunc_internal(position, true);
#[cfg(debug_assertions)]
self.check_invariants();
}

fn trunc_internal(&mut self, position: i16, strip_trailing_zeros: bool) {
self.lower_magnitude = cmp::min(position, 0);
if position == i16::MIN {
// Nothing more to do
Expand All @@ -1059,14 +1065,13 @@ impl FixedDecimal {
if magnitude <= self.magnitude {
self.digits
.truncate(crate::ops::i16_abs_sub(self.magnitude, magnitude) as usize);
self.remove_trailing_zeros_from_digits_list();
if strip_trailing_zeros {
self.remove_trailing_zeros_from_digits_list();
}
} else {
self.digits.clear();
self.magnitude = 0;
}

#[cfg(debug_assertions)]
self.check_invariants();
}

/// Half Truncates the number on the right to a particular position, deleting
Expand Down Expand Up @@ -1171,7 +1176,7 @@ impl FixedDecimal {
let before_truncate_is_zero = self.is_zero();
let before_truncate_bottom_magnitude = self.nonzero_magnitude_end();
let before_truncate_magnitude = self.magnitude;
self.trunc(position);
self.trunc_internal(position, false);

if before_truncate_is_zero || position <= before_truncate_bottom_magnitude {
#[cfg(debug_assertions)]
Expand Down Expand Up @@ -3338,6 +3343,19 @@ fn test_rounding() {
let mut dec = FixedDecimal::from_str("-0.009").unwrap();
dec.half_expand(-1);
assert_eq!("-0.0", dec.to_string());

// // Test specific cases
let mut dec = FixedDecimal::from_str("1.108").unwrap();
dec.half_even(-2);
assert_eq!("1.11", dec.to_string());

let mut dec = FixedDecimal::from_str("1.108").unwrap();
dec.expand(-2);
assert_eq!("1.11", dec.to_string());

let mut dec = FixedDecimal::from_str("1.108").unwrap();
dec.trunc(-2);
assert_eq!("1.10", dec.to_string());
}

#[test]
Expand Down
221 changes: 221 additions & 0 deletions utils/fixed_decimal/tests/rounding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use core::ops::RangeInclusive;
use fixed_decimal::FixedDecimal;
use fixed_decimal::Sign;
use writeable::Writeable;

#[test]
pub fn test_ecma402_table() {
// Source: <https://tc39.es/ecma402/#table-intl-rounding-modes>
let cases: [(
&'static str,
fn(&mut FixedDecimal, i16),
i32,
i32,
i32,
i32,
i32,
); 9] = [
("ceil", FixedDecimal::ceil, -1, 1, 1, 1, 2),
("floor", FixedDecimal::floor, -2, 0, 0, 0, 1),
("expand", FixedDecimal::expand, -2, 1, 1, 1, 2),
("trunc", FixedDecimal::trunc, -1, 0, 0, 0, 1),
("half_ceil", FixedDecimal::half_ceil, -1, 0, 1, 1, 2),
("half_floor", FixedDecimal::half_floor, -2, 0, 0, 1, 1),
("half_expand", FixedDecimal::half_expand, -2, 0, 1, 1, 2),
("half_trunc", FixedDecimal::half_trunc, -1, 0, 0, 1, 1),
("half_even", FixedDecimal::half_even, -2, 0, 0, 1, 2),
];
for (rounding_mode, f, e1, e2, e3, e4, e5) in cases.into_iter() {
let mut fd1: FixedDecimal = "-1.5".parse().unwrap();
let mut fd2: FixedDecimal = "0.4".parse().unwrap();
let mut fd3: FixedDecimal = "0.5".parse().unwrap();
let mut fd4: FixedDecimal = "0.6".parse().unwrap();
let mut fd5: FixedDecimal = "1.5".parse().unwrap();
f(&mut fd1, 0);
f(&mut fd2, 0);
f(&mut fd3, 0);
f(&mut fd4, 0);
f(&mut fd5, 0);
assert_eq!(
fd1.write_to_string(),
e1.write_to_string(),
"-1.5 failed for {rounding_mode}"
);
assert_eq!(
fd2.write_to_string(),
e2.write_to_string(),
"0.4 failed for {rounding_mode}"
);
assert_eq!(
fd3.write_to_string(),
e3.write_to_string(),
"0.5 failed for {rounding_mode}"
);
assert_eq!(
fd4.write_to_string(),
e4.write_to_string(),
"0.6 failed for {rounding_mode}"
);
assert_eq!(
fd5.write_to_string(),
e5.write_to_string(),
"1.5 failed for {rounding_mode}"
);
}
}

#[test]
pub fn test_within_ranges() {
struct TestCase {
rounding_mode: &'static str,
f: fn(&mut FixedDecimal, i16),
range_n2000: RangeInclusive<i32>,
range_n1000: RangeInclusive<i32>,
range_0: RangeInclusive<i32>,
range_1000: RangeInclusive<i32>,
range_2000: RangeInclusive<i32>,
}
let cases: [TestCase; 9] = [
TestCase {
rounding_mode: "ceil",
f: FixedDecimal::ceil,
range_n2000: -2999..=-2000,
range_n1000: -1999..=-1000,
range_0: -999..=0,
range_1000: 1..=1000,
range_2000: 1001..=2000,
},
TestCase {
rounding_mode: "floor",
f: FixedDecimal::floor,
range_n2000: -2000..=-1001,
range_n1000: -1000..=-1,
range_0: 0..=999,
range_1000: 1000..=1999,
range_2000: 2000..=2999,
},
TestCase {
rounding_mode: "expand",
f: FixedDecimal::expand,
range_n2000: -2000..=-1001,
range_n1000: -1000..=-1,
range_0: 0..=0,
range_1000: 1..=1000,
range_2000: 1001..=2000,
},
TestCase {
rounding_mode: "trunc",
f: FixedDecimal::trunc,
range_n2000: -2999..=-2000,
range_n1000: -1999..=-1000,
range_0: -999..=999,
range_1000: 1000..=1999,
range_2000: 2000..=2999,
},
TestCase {
rounding_mode: "half_ceil",
f: FixedDecimal::half_ceil,
range_n2000: -2500..=-1501,
range_n1000: -1500..=-501,
range_0: -500..=449,
range_1000: 500..=1449,
range_2000: 1500..=2449,
},
TestCase {
rounding_mode: "half_floor",
f: FixedDecimal::half_floor,
range_n2000: -2449..=-1500,
range_n1000: -1449..=-500,
range_0: -449..=500,
range_1000: 501..=1500,
range_2000: 1501..=2500,
},
TestCase {
rounding_mode: "half_expand",
f: FixedDecimal::half_expand,
range_n2000: -2449..=-1500,
range_n1000: -1449..=-500,
range_0: -449..=449,
range_1000: 500..=1449,
range_2000: 1500..=2449,
},
TestCase {
rounding_mode: "half_trunc",
f: FixedDecimal::half_trunc,
range_n2000: -2500..=-1501,
range_n1000: -1500..=-501,
range_0: -500..=500,
range_1000: 501..=1500,
range_2000: 1501..=2500,
},
TestCase {
rounding_mode: "half_even",
f: FixedDecimal::half_even,
range_n2000: -2500..=-1500,
range_n1000: -1449..=-501,
range_0: -500..=500,
range_1000: 501..=1449,
range_2000: 1500..=2500,
},
];
for TestCase {
rounding_mode,
f,
range_n2000,
range_n1000,
range_0,
range_1000,
range_2000,
} in cases
{
for n in range_n2000 {
let mut fd = FixedDecimal::from(n);
f(&mut fd, 3);
assert_eq!(fd.write_to_string(), "-2000", "{rounding_mode}: {n}");
let mut fd = FixedDecimal::from(n - 1000000).multiplied_pow10(-5);
f(&mut fd, -2);
assert_eq!(fd.write_to_string(), "-10.02", "{rounding_mode}: {n} ÷ 10^5 ± 10");
}
for n in range_n1000 {
let mut fd = FixedDecimal::from(n);
f(&mut fd, 3);
assert_eq!(fd.write_to_string(), "-1000", "{rounding_mode}: {n}");
let mut fd = FixedDecimal::from(n - 1000000).multiplied_pow10(-5);
f(&mut fd, -2);
assert_eq!(fd.write_to_string(), "-10.01", "{rounding_mode}: {n} ÷ 10^5 ± 10");
}
for n in range_0 {
let mut fd = FixedDecimal::from(n);
f(&mut fd, 3);
fd.set_sign(Sign::None); // get rid of -0
assert_eq!(fd.write_to_string(), "000", "{rounding_mode}: {n}");
let (mut fd, expected) = if n < 0 {
(FixedDecimal::from(n - 1000000).multiplied_pow10(-5), "-10.00")
} else {
(FixedDecimal::from(n + 1000000).multiplied_pow10(-5), "10.00")
};
f(&mut fd, -2);
assert_eq!(fd.write_to_string(), expected, "{rounding_mode}: {n} ÷ 10^5 ± 10");
}
for n in range_1000 {
let mut fd = FixedDecimal::from(n);
f(&mut fd, 3);
assert_eq!(fd.write_to_string(), "1000", "{rounding_mode}: {n}");
let mut fd = FixedDecimal::from(n + 1000000).multiplied_pow10(-5);
f(&mut fd, -2);
assert_eq!(fd.write_to_string(), "10.01", "{rounding_mode}: {n} ÷ 10^5 ± 10");
}
for n in range_2000 {
let mut fd = FixedDecimal::from(n);
f(&mut fd, 3);
assert_eq!(fd.write_to_string(), "2000", "{rounding_mode}: {n}");
let mut fd = FixedDecimal::from(n + 1000000).multiplied_pow10(-5);
f(&mut fd, -2);
assert_eq!(fd.write_to_string(), "10.02", "{rounding_mode}: {n} ÷ 10^5 ± 10");
}
}
}
Loading