Skip to content

Commit

Permalink
Partially implement rounding
Browse files Browse the repository at this point in the history
  • Loading branch information
printfn committed Sep 29, 2024
1 parent de597ea commit b7605f5
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions core/src/num/bigrat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -724,11 +724,17 @@ impl BigRat {
let next_digit =
|i: usize, num: BigUint, base: &BigUint| -> Result<(BigUint, BigUint), NextDigitErr> {
test_int(int)?;
if num == 0.into()
|| max_digits == MaxDigitsToPrint::DecimalPlaces(i)
if num == 0.into() {
// reached the end of the number
return Err(NextDigitErr::Terminated { round_up: false });
}
if max_digits == MaxDigitsToPrint::DecimalPlaces(i)
|| max_digits == MaxDigitsToPrint::DpButIgnoreLeadingZeroes(i)
{
return Err(NextDigitErr::Terminated);
// round up if remaining fraction is >1/2
return Err(NextDigitErr::Terminated {
round_up: num.mul(&2.into(), int)? >= *denominator,
});
}
// digit = base * numerator / denominator
// next_numerator = base * numerator - digit * denominator
Expand Down Expand Up @@ -785,7 +791,7 @@ impl BigRat {
trailing_digits.push(')');
Ok((sign, Exact::new(trailing_digits, true))) // the recurring decimal is exact
}
Err(NextDigitErr::Terminated) => {
Err(NextDigitErr::Terminated { round_up: _ }) => {
panic!("decimal number terminated unexpectedly");
}
Err(NextDigitErr::Error(e)) => Err(e),
Expand Down Expand Up @@ -844,7 +850,7 @@ impl BigRat {
i += 1;
}
}
Err(NextDigitErr::Terminated) => {
Err(NextDigitErr::Terminated { round_up }) => {
let sign = if let Some(actual_sign) = actual_sign {
actual_sign
} else {
Expand All @@ -854,6 +860,9 @@ impl BigRat {
trailing_digits.push_str(&formatted_int);
sign
};
if round_up {
// todo
}
// is the number exact, or did we need to truncate?
let exact = current_numerator == 0.into();
return Ok((sign, Exact::new(trailing_digits, exact)));
Expand Down Expand Up @@ -1079,7 +1088,11 @@ impl BigRat {
}
enum NextDigitErr {
Error(FendError),
Terminated,
/// Stop printing digits because we've reached the end of the number or the
/// limit of how much we want to print
Terminated {
round_up: bool,
},
}

impl From<FendError> for NextDigitErr {
Expand Down

0 comments on commit b7605f5

Please sign in to comment.