From caaba6d51372a8abd34e54887bb727204efeda9c Mon Sep 17 00:00:00 2001 From: Lucia Velasco Date: Fri, 2 Mar 2018 21:55:54 +0000 Subject: [PATCH 1/4] Adds toNearest tests for numbers to round that are less-than-even This will test numbers that are less than half the first argument (y) of the toNearest function. If the y value is ten, the decimal value on which toNearest is tested will be a multiplication of ten plus less than 5. --- test/modules/toNearest.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/modules/toNearest.js b/test/modules/toNearest.js index b0a7252..7ee527c 100644 --- a/test/modules/toNearest.js +++ b/test/modules/toNearest.js @@ -112,6 +112,16 @@ T('toNearest', function () { t('83105511539', '83105511539.5', new Decimal('-1'), 3, 5); t('83105511540', '83105511539.5000000000000000000001', -1, 3, 5); + t('83105511540', '83105511539.4', 1, 30, 0); + t('83105511539', '83105511539.4', 1, 30, 1); + t('83105511540', '83105511539.4', 1, 30, 2); + t('83105511539', '83105511539.4', 1, 30, 3); + t('83105511539', '83105511539.4', 1, 30, 4); + t('83105511539', '83105511539.4', 1, 30, 5); + t('83105511539', '83105511539.4', 1, 30, 6); + t('83105511539', '83105511539.4', 1, 30, 7); + t('83105511539', '83105511539.4', 1, 30, 8); + t('83105511540', '83105511539.5', 1, 30, 0); t('83105511539', '83105511539.5', 1, 30, 1); t('83105511540', '83105511539.5', 1, 30, 2); From 4949869b5e5824e5805ddd0ae1d64116956ecdd4 Mon Sep 17 00:00:00 2001 From: Lucia Velasco Date: Fri, 2 Mar 2018 22:07:56 +0000 Subject: [PATCH 2/4] Allows round up and round ceil to work in the toNearest function. This makes the new tests in the previous commit work. A value of less than half of the first parameter of toNearest will now round up to the nearest multiplication of that first parameter. --- decimal.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/decimal.js b/decimal.js index d69768e..6504aee 100644 --- a/decimal.js +++ b/decimal.js @@ -2165,7 +2165,7 @@ // If y is not zero, calculate the nearest multiple of y to x. if (y.d[0]) { external = false; - if (rm < 4) rm = [4, 5, 7, 8][rm]; + if (rm === 1 || rm === 3) rm = [4, 5, 7, 8][rm]; x = divide(x, y, 0, rm, 1).times(y); external = true; finalise(x); From 2c3198d55523c4132430228ae11a2d50dd626ec4 Mon Sep 17 00:00:00 2001 From: Lucia Velasco Date: Fri, 2 Mar 2018 22:11:22 +0000 Subject: [PATCH 3/4] Fixes pre-existing test to successfully round up --- test/modules/toNearest.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/modules/toNearest.js b/test/modules/toNearest.js index 7ee527c..ed3b5e1 100644 --- a/test/modules/toNearest.js +++ b/test/modules/toNearest.js @@ -156,7 +156,7 @@ T('toNearest', function () { Decimal.rounding = 1; t('83105511539', '83105511539.5', void 0, 11); - t('3847560', '3847561.00000749', 10, 11, 0); + t('3847570', '3847561.00000749', 10, 11, 0); t('42840000000000000', '42835000000000001', '1e+13', 2, 0); t('42840000000000000', '42835000000000001', '1e+13', 2, 1); t('42840000000000000', '42835000000000000.0002', '1e+13', 200, 0); From caed629d5a1c3828a12f5278a281e60711246225 Mon Sep 17 00:00:00 2001 From: Lucia Velasco Date: Mon, 5 Mar 2018 09:10:46 +0000 Subject: [PATCH 4/4] Code Review Changes: Updates test file and void check Also removes superfluous line that the tests now cater for. --- decimal.js | 7 +- test/modules/toNearest.js | 214 +++++++++++++++++++++++++------------- 2 files changed, 145 insertions(+), 76 deletions(-) diff --git a/decimal.js b/decimal.js index 6504aee..b0697fb 100644 --- a/decimal.js +++ b/decimal.js @@ -2150,7 +2150,11 @@ rm = Ctor.rounding; } else { y = new Ctor(y); - if (rm !== void 0) checkInt32(rm, 0, 8); + if (rm === void 0) { + rm = Ctor.rounding; + } else { + checkInt32(rm, 0, 8); + } // If x is not finite, return x if y is not NaN, else NaN. if (!x.d) return y.s ? x : y; @@ -2165,7 +2169,6 @@ // If y is not zero, calculate the nearest multiple of y to x. if (y.d[0]) { external = false; - if (rm === 1 || rm === 3) rm = [4, 5, 7, 8][rm]; x = divide(x, y, 0, rm, 1).times(y); external = true; finalise(x); diff --git a/test/modules/toNearest.js b/test/modules/toNearest.js index ed3b5e1..e3ac89f 100644 --- a/test/modules/toNearest.js +++ b/test/modules/toNearest.js @@ -27,8 +27,7 @@ T('toNearest', function () { t(!isMinusZero(new Decimal(1).toNearest(-3))); t( isMinusZero(new Decimal(-1).toNearest(-3))); - t = function (expected, n, v, sd, rm) { - if (sd) Decimal.precision = sd; + t = function (expected, n, v, rm) { T.assertEqual(expected, new Decimal(n).toNearest(v, rm).valueOf()); } @@ -67,9 +66,9 @@ T('toNearest', function () { t('0', 1, -3); t('-0', -1, -3); - t('3', 1.5, -3, 20, 0); - t('-0', -1.5, -3, 20, 1); - t('-3', -1.5, -3, 20, 2); + t('3', 1.5, -3, 0); + t('-0', -1.5, -3, 1); + t('-3', -1.5, -3, 2); t('123', 123.456); t('123', 123.456, 1); @@ -87,78 +86,145 @@ T('toNearest', function () { t('123.46', 123.456, '-0.02'); t('123.456', 123.456, '-0.002'); - t('83105511540', '83105511539.5', 1, 11, 4); - t('83105511539', '83105511539.499999999999999999999999999999', 1, 11, 4); - t('83105511539', '83105511539.5', '1', 11, 5); - t('83105511540', '83105511539.5000000000000000000001', 1, 11, 5); - - t('83105511540', '83105511539.5', new Decimal(1), 3, 4); - t('83105511539', '83105511539.499999999999999999999999999999', 1, 3, 4); - t('83105511539', '83105511539.5', new Decimal('1'), 3, 5); - t('83105511540', '83105511539.5000000000000000000001', 1, 3, 5); - - t('83105511540', '83105511539.5', Decimal.ONE, 30, 4); - t('83105511539', '83105511539.499999999999999999999999999999', 1, 30, 4); - t('83105511539', '83105511539.5', 1, 30, 5); - t('83105511540', '83105511539.5000000000000000000001', 1, 30, 5); - - t('83105511540', '83105511539.5', -1, 11, 4); - t('83105511539', '83105511539.499999999999999999999999999999', -1, 11, 4); - t('83105511539', '83105511539.5', '-1', 11, 5); - t('83105511540', '83105511539.5000000000000000000001', -1, 11, 5); - - t('83105511540', '83105511539.5', new Decimal(-1), 3, 4); - t('83105511539', '83105511539.499999999999999999999999999999', 1, 3, 4); - t('83105511539', '83105511539.5', new Decimal('-1'), 3, 5); - t('83105511540', '83105511539.5000000000000000000001', -1, 3, 5); - - t('83105511540', '83105511539.4', 1, 30, 0); - t('83105511539', '83105511539.4', 1, 30, 1); - t('83105511540', '83105511539.4', 1, 30, 2); - t('83105511539', '83105511539.4', 1, 30, 3); - t('83105511539', '83105511539.4', 1, 30, 4); - t('83105511539', '83105511539.4', 1, 30, 5); - t('83105511539', '83105511539.4', 1, 30, 6); - t('83105511539', '83105511539.4', 1, 30, 7); - t('83105511539', '83105511539.4', 1, 30, 8); - - t('83105511540', '83105511539.5', 1, 30, 0); - t('83105511539', '83105511539.5', 1, 30, 1); - t('83105511540', '83105511539.5', 1, 30, 2); - t('83105511539', '83105511539.5', 1, 30, 3); - t('83105511540', '83105511539.5', 1, 30, 4); - t('83105511539', '83105511539.5', 1, 30, 5); - t('83105511540', '83105511539.5', 1, 30, 6); - t('83105511540', '83105511539.5', 1, 30, 7); - t('83105511539', '83105511539.5', 1, 30, 8); - t('83105511539', '83105511539.499999999999999999999999999999', void 0, 30, 0); - t('83105511539', '83105511539.499999999999999999999999999999', 1, 30, 1); - t('83105511539', '83105511539.499999999999999999999999999999', void 0, 30, 2); - t('83105511539', '83105511539.499999999999999999999999999999', 1, 30, 3); - t('83105511539', '83105511539.499999999999999999999999999999', void 0, 30, 4); - t('83105511539', '83105511539.499999999999999999999999999999', 1, 30, 5); - t('83105511539', '83105511539.499999999999999999999999999999', void 0, 30, 6); - t('83105511539', '83105511539.499999999999999999999999999999', 1, 30, 7); - t('83105511539', '83105511539.499999999999999999999999999999', void 0, 30, 8); - t('83105511540', '83105511539.5000000000000000000001', void 0, 30, 0); - t('83105511540', '83105511539.5000000000000000000001', 1, 30, 1); - t('83105511540', '83105511539.5000000000000000000001', void 0, 30, 2); - t('83105511540', '83105511539.5000000000000000000001', 1, 30, 3); - t('83105511540', '83105511539.5000000000000000000001', void 0, 30, 4); - t('83105511540', '83105511539.5000000000000000000001', 1, 30, 5); - t('83105511540', '83105511539.5000000000000000000001', void 0, 30, 6); - t('83105511540', '83105511539.5000000000000000000001', 1, 30, 7); - t('83105511540', '83105511539.5000000000000000000001', void 0, 30, 8); + t('83105511540', '83105511539.5', 1, 4); + t('83105511539', '83105511539.499999999999999999999999999999', 1, 4); + t('83105511539', '83105511539.5', '1', 5); + t('83105511540', '83105511539.5000000000000000000001', 1, 5); + + Decimal.precision = 3; + + t('83105511540', '83105511539.5', new Decimal(1), 4); + t('83105511539', '83105511539.499999999999999999999999999999', 1, 4); + t('83105511539', '83105511539.5', new Decimal('1'), 5); + t('83105511540', '83105511539.5000000000000000000001', 1, 5); + + Decimal.precision = 20; + + t('83105511540', '83105511539.5', -1, 4); + t('83105511539', '83105511539.499999999999999999999999999999', -1, 4); + t('83105511539', '83105511539.5', '-1', 5); + t('83105511540', '83105511539.5000000000000000000001', -1, 5); + + t('-83105511540', '-83105511539.5', new Decimal(-1), 4); + t('-83105511539', '-83105511539.499999999999999999999999999999', 1, 4); + t('-83105511539', '-83105511539.5', new Decimal('-1'), 5); + t('-83105511540', '-83105511539.5000000000000000000001', -1, 5); + + t('83105511540', '83105511539.5', 1, 0); + t('83105511539', '83105511539.5', 1, 1); + t('83105511540', '83105511539.5', 1, 2); + t('83105511539', '83105511539.5', 1, 3); + t('83105511540', '83105511539.5', 1, 4); + t('83105511539', '83105511539.5', 1, 5); + t('83105511540', '83105511539.5', 1, 6); + t('83105511540', '83105511539.5', 1, 7); + t('83105511539', '83105511539.5', 1, 8); + t('83105511539', '83105511539.499999999999999999999999999999', void 0, 0); + t('83105511539', '83105511539.499999999999999999999999999999', 1, 1); + t('83105511539', '83105511539.499999999999999999999999999999', void 0, 2); + t('83105511539', '83105511539.499999999999999999999999999999', 1, 3); + t('83105511539', '83105511539.499999999999999999999999999999', void 0, 4); + t('83105511539', '83105511539.499999999999999999999999999999', 1, 5); + t('83105511539', '83105511539.499999999999999999999999999999', void 0, 6); + t('83105511539', '83105511539.499999999999999999999999999999', 1, 7); + t('83105511539', '83105511539.499999999999999999999999999999', void 0, 8); + t('83105511540', '83105511539.5000000000000000000001', void 0, 0); + t('83105511539', '83105511539.5000000000000000000001', 1, 1); + t('83105511540', '83105511539.5000000000000000000001', void 0, 2); + t('83105511539', '83105511539.5000000000000000000001', 1, 3); + t('83105511540', '83105511539.5000000000000000000001', void 0, 4); + t('83105511540', '83105511539.5000000000000000000001', 1, 5); + t('83105511540', '83105511539.5000000000000000000001', void 0, 6); + t('83105511540', '83105511539.5000000000000000000001', 1, 7); + t('83105511540', '83105511539.5000000000000000000001', void 0, 8); Decimal.rounding = 0; - t('83105511540', '83105511539.5', void 0, 11); + t('83105511540', '83105511539.5'); Decimal.rounding = 1; - t('83105511539', '83105511539.5', void 0, 11); + t('83105511539', '83105511539.5'); + + t('3847570', '3847561.00000749', 10, 0); + t('42840000000000000', '42835000000000001', '1e+13', 0); + t('42830000000000000', '42835000000000001', '1e+13', 1); + t('42840000000000000', '42835000000000000.0002', '1e+13', 0); + t('42830000000000000', '42835000000000000.0002', '1e+13', 1); + + t('500', '449.999', 100, 0); + t('400', '449.999', 100, 1); + t('500', '449.999', 100, 2); + t('400', '449.999', 100, 3); + t('400', '449.999', 100, 4); + t('400', '449.999', 100, 5); + t('400', '449.999', 100, 6); + t('400', '449.999', 100, 7); + t('400', '449.999', 100, 8); + + t('-500', '-449.999', 100, 0); + t('-400', '-449.999', 100, 1); + t('-400', '-449.999', 100, 2); + t('-500', '-449.999', 100, 3); + t('-400', '-449.999', 100, 4); + t('-400', '-449.999', 100, 5); + t('-400', '-449.999', 100, 6); + t('-400', '-449.999', 100, 7); + t('-400', '-449.999', 100, 8); + + t('500', '450', 100, 0); + t('400', '450', 100, 1); + t('500', '450', 100, 2); + t('400', '450', 100, 3); + t('500', '450', 100, 4); + t('400', '450', 100, 5); + t('400', '450', 100, 6); + t('500', '450', 100, 7); + t('400', '450', 100, 8); + + t('-500', '-450', 100, 0); + t('-400', '-450', 100, 1); + t('-400', '-450', 100, 2); + t('-500', '-450', 100, 3); + t('-500', '-450', 100, 4); + t('-400', '-450', 100, 5); + t('-400', '-450', 100, 6); + t('-400', '-450', 100, 7); + t('-500', '-450', 100, 8); - t('3847570', '3847561.00000749', 10, 11, 0); - t('42840000000000000', '42835000000000001', '1e+13', 2, 0); - t('42840000000000000', '42835000000000001', '1e+13', 2, 1); - t('42840000000000000', '42835000000000000.0002', '1e+13', 200, 0); - t('42840000000000000', '42835000000000000.0002', '1e+13', 200, 1); + Decimal.rounding = 0; + t('500', '450.001', 100); + Decimal.rounding = 1; + t('400', '450.001', 100); + Decimal.rounding = 2; + t('500', '450.001', 100); + Decimal.rounding = 3; + t('400', '450.001', 100); + Decimal.rounding = 4; + t('500', '450.001', 100); + Decimal.rounding = 5; + t('500', '450.001', 100); + Decimal.rounding = 6; + t('500', '450.001', 100); + Decimal.rounding = 7; + t('500', '450.001', 100); + Decimal.rounding = 8; + t('500', '450.001', 100); + + Decimal.rounding = 0; + t('-500', '-450.001', 100); + Decimal.rounding = 1; + t('-400', '-450.001', 100); + Decimal.rounding = 2; + t('-400', '-450.001', 100); + Decimal.rounding = 3; + t('-500', '-450.001', 100); + Decimal.rounding = 4; + t('-500', '-450.001', 100); + Decimal.rounding = 5; + t('-500', '-450.001', 100); + Decimal.rounding = 6; + t('-500', '-450.001', 100); + Decimal.rounding = 7; + t('-500', '-450.001', 100); + Decimal.rounding = 8; + t('-500', '-450.001', 100); });