From d589a0e268f01754e50b3ec0fa1d91ba1c78494a Mon Sep 17 00:00:00 2001 From: Michael Mclaughlin Date: Sat, 9 Jul 2022 19:03:30 +0100 Subject: [PATCH] #191 Bugfix: round may result in improper coefficient --- big.js | 7 ++++--- big.mjs | 7 ++++--- test/methods/round.js | 3 +++ 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/big.js b/big.js index 2541e32..7544ce9 100644 --- a/big.js +++ b/big.js @@ -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; } } } diff --git a/big.mjs b/big.mjs index 7378d38..0ed3284 100644 --- a/big.mjs +++ b/big.mjs @@ -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; } } } diff --git a/test/methods/round.js b/test/methods/round.js index d1a98b1..621bf19 100644 --- a/test/methods/round.js +++ b/test/methods/round.js @@ -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); });