Skip to content

Commit

Permalink
round to even
Browse files Browse the repository at this point in the history
  • Loading branch information
mj10021 committed Nov 9, 2023
1 parent 33761b0 commit 4e4c972
Show file tree
Hide file tree
Showing 2 changed files with 219 additions and 224 deletions.
8 changes: 3 additions & 5 deletions library/core/src/fmt/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,19 +331,17 @@ macro_rules! impl_Exp {
let rem = n % 10;
n /= 10;
exponent += 1;
// round up last digit
if rem >= 5 {
// round up last digit, round to even on a tie
if rem > 5 || (rem == 5 && n % 2 != 0) {
n += 1;
// if the digit is rounded to the next power
// instead adjust the exponent
if n % 10 == 0 {
if n.ilog10() > (n - 1).ilog10() {
n /= 10;
exponent += 1;
}
}
// n = 100
}
// assert!(n == 666, "{}\n{}\n{}\n",n, exponent, added_precision);
(n, exponent, exponent, added_precision)
};

Expand Down
Loading

0 comments on commit 4e4c972

Please sign in to comment.