Skip to content

Commit

Permalink
#191 Bugfix: round may result in improper coefficient
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeMcl committed Jul 9, 2022
1 parent f842264 commit d589a0e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
7 changes: 4 additions & 3 deletions big.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,17 +230,18 @@
rm === 3 && (more || !!xc[0]);

// Remove any digits after the required precision.
xc.length = sd--;
xc.length = sd;

// Round up?
if (more) {

// Rounding up may mean the previous digit has to be rounded up.
for (; ++xc[sd] > 9;) {
for (; ++xc[--sd] > 9;) {
xc[sd] = 0;
if (!sd--) {
if (sd === 0) {
++x.e;
xc.unshift(1);
break;
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions big.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -227,17 +227,18 @@ function round(x, sd, rm, more) {
rm === 3 && (more || !!xc[0]);

// Remove any digits after the required precision.
xc.length = sd--;
xc.length = sd;

// Round up?
if (more) {

// Rounding up may mean the previous digit has to be rounded up.
for (; ++xc[sd] > 9;) {
for (; ++xc[--sd] > 9;) {
xc[sd] = 0;
if (!sd--) {
if (sd === 0) {
++x.e;
xc.unshift(1);
break;
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions test/methods/round.js
Original file line number Diff line number Diff line change
Expand Up @@ -5286,4 +5286,7 @@ test('round', function () {
t('0', '98765000', -10, 1);
t('0', '98765000', -10, 2);
t('10000000000', '98765000', -10, 3);

// #191
test.isTrue(new Big('9.9').round().c['-1'] === u);
});

0 comments on commit d589a0e

Please sign in to comment.