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

Precision::round improvements #21

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
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
92 changes: 78 additions & 14 deletions packages/kujira-std/src/precision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,21 @@ impl Precise for Decimal {
fn round(&self, p: &Precision) -> Self {
match p {
Precision::SignificantFigures(sf) => {
// early return for zero significant figures
if sf == &0 {
return Self::zero();
}
let int = self.numerator();
let len = int.to_string().as_str().bytes().len() as u32;
let decimals: u32 = len - *sf as u32;
let pow = Uint128::from(10u128).pow(decimals);
let truncated = Self::from_ratio(int, pow) * Uint128::from(1u128);
Self::from_ratio(truncated * pow, self.denominator())
let figures = int.to_string().len() as u32;
let pow = Uint128::new(10u128).pow(figures - *sf as u32);
let significant_part = int / pow; // integer division truncates
let truncated = significant_part * pow;
Self::new(truncated)
}
Precision::DecimalPlaces(dp) => {
let pow = Uint128::from(10u128).pow(18 - *dp as u32);
let x = Self::from_ratio(self.numerator(), self.denominator() * pow);
Self::from_ratio(x.numerator() * pow, x.denominator())
let significant_part = self.numerator() / pow; // integer division truncates
Self::new(significant_part * pow)
}
}
}
Expand All @@ -54,17 +58,21 @@ impl Precise for Decimal256 {
fn round(&self, p: &Precision) -> Self {
match p {
Precision::SignificantFigures(sf) => {
// early return for zero significant figures
if sf == &0 {
return Self::zero();
}
let int = self.numerator();
let len = int.to_string().as_str().bytes().len() as u32;
let decimals: u32 = len - *sf as u32;
let pow = Uint256::from(10u128).pow(decimals);
let truncated = Self::from_ratio(int, pow) * Uint256::from(1u128);
Self::from_ratio(truncated * pow, self.denominator())
let figures = int.to_string().len() as u32;
let pow = Uint256::from(10u128).pow(figures - *sf as u32);
let significant_part = int / pow; // integer division truncates
let truncated = significant_part * pow;
Self::new(truncated)
}
Precision::DecimalPlaces(dp) => {
let pow = Uint256::from(10u128).pow(18 - *dp as u32);
let x = Self::from_ratio(self.numerator(), self.denominator() * pow);
Self::from_ratio(x.numerator() * pow, x.denominator())
let significant_part = self.numerator() / pow; // integer division truncates
Self::new(significant_part * pow)
}
}
}
Expand Down Expand Up @@ -92,4 +100,60 @@ mod tests {
assert_eq!(p.validate(&Decimal::from_str("12.343").unwrap()), None);
assert_eq!(p.validate(&Decimal::from_str("1.2").unwrap()), Some(()));
}

#[test]
fn test_significant_figures_round() {
const CASES: [(u8, &str, &str); 7] = [
(2, "123.456", "120"),
(4, "12.345678910111213141", "12.34"),
(18, "0.123456789101213141", "0.123456789101213141"),
(2, "0.123456", "0.12"),
(18 + 3, "123.456", "123.456"),
(0, "123.456", "0"),
(1, "123", "100"),
];

for (precision, unrounded, expected) in CASES {
let p = Precision::SignificantFigures(precision);
assert_eq!(
Decimal::from_str(unrounded).unwrap().round(&p),
Decimal::from_str(expected).unwrap()
);
assert_eq!(
Decimal256::from_str(unrounded).unwrap().round(&p),
Decimal256::from_str(expected).unwrap()
);
}
}

#[test]
#[should_panic]
fn test_significant_figures_round_panic() {
let p = Precision::SignificantFigures(18 + 4);
_ = Decimal::from_str("123.456").unwrap().round(&p);
}

#[test]
fn test_decimal_places_round() {
const CASES: [(u8, &str, &str); 6] = [
(2, "123.456", "123.45"),
(4, "12.345678910111213141", "12.3456"),
(18, "0.123456789101213141", "0.123456789101213141"),
(2, "0.123456", "0.12"),
(0, "123.456", "123"),
(1, "123", "123"),
];

for (precision, unrounded, expected) in CASES {
let p = Precision::DecimalPlaces(precision);
assert_eq!(
Decimal::from_str(unrounded).unwrap().round(&p),
Decimal::from_str(expected).unwrap()
);
assert_eq!(
Decimal256::from_str(unrounded).unwrap().round(&p),
Decimal256::from_str(expected).unwrap()
);
}
}
}