Skip to content

Commit 373beed

Browse files
committed
#63 round: negative argument
1 parent 7aeb153 commit 373beed

File tree

3 files changed

+227
-10
lines changed

3 files changed

+227
-10
lines changed

big.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
Big.RM = RM;
108108
Big.NE = NE;
109109
Big.PE = PE;
110-
Big.version = '5.0.2';
110+
Big.version = '5.1.2';
111111

112112
return Big;
113113
}
@@ -191,7 +191,7 @@
191191
more = xc[i] > 5 || xc[i] == 5 &&
192192
(more || i < 0 || xc[i + 1] !== UNDEFINED || xc[i - 1] & 1);
193193
} else if (rm === 3) {
194-
more = more || xc[i] !== UNDEFINED || i < 0;
194+
more = more || !!xc[0];
195195
} else {
196196
more = false;
197197
if (rm !== 0) throw Error(INVALID_RM);
@@ -658,7 +658,7 @@
658658
xc = xc.slice();
659659

660660
// Prepend zeros to equalise exponents.
661-
// Note: Faster to use reverse than do unshifts.
661+
// Note: reverse faster than unshifts.
662662
if (a = xe - ye) {
663663
if (a > 0) {
664664
ye = xe;
@@ -733,6 +733,7 @@
733733
* Return a new Big whose value is the value of this Big rounded to a maximum of dp decimal
734734
* places using rounding mode rm.
735735
* If dp is not specified, round to 0 decimal places.
736+
* If dp is negative, round to a multiple of 10**abs(dp).
736737
* If rm is not specified, use Big.RM.
737738
*
738739
* dp? {number} Integer, 0 to MAX_DP inclusive.
@@ -741,7 +742,7 @@
741742
P.round = function (dp, rm) {
742743
var Big = this.constructor;
743744
if (dp === UNDEFINED) dp = 0;
744-
else if (dp !== ~~dp || dp < 0 || dp > MAX_DP) throw Error(INVALID_DP);
745+
else if (dp !== ~~dp || dp < -MAX_DP || dp > MAX_DP) throw Error(INVALID_DP);
745746
return round(new Big(this), dp, rm === UNDEFINED ? Big.RM : rm);
746747
};
747748

big.mjs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function _Big_() {
104104
Big.RM = RM;
105105
Big.NE = NE;
106106
Big.PE = PE;
107-
Big.version = '5.0.2';
107+
Big.version = '5.1.2';
108108

109109
return Big;
110110
}
@@ -188,7 +188,7 @@ function round(x, dp, rm, more) {
188188
more = xc[i] > 5 || xc[i] == 5 &&
189189
(more || i < 0 || xc[i + 1] !== UNDEFINED || xc[i - 1] & 1);
190190
} else if (rm === 3) {
191-
more = more || xc[i] !== UNDEFINED || i < 0;
191+
more = more || !!xc[0];
192192
} else {
193193
more = false;
194194
if (rm !== 0) throw Error(INVALID_RM);
@@ -655,7 +655,7 @@ P.plus = P.add = function (y) {
655655
xc = xc.slice();
656656

657657
// Prepend zeros to equalise exponents.
658-
// Note: Faster to use reverse then do unshifts.
658+
// Note: reverse faster than unshifts.
659659
if (a = xe - ye) {
660660
if (a > 0) {
661661
ye = xe;
@@ -730,6 +730,7 @@ P.pow = function (n) {
730730
* Return a new Big whose value is the value of this Big rounded to a maximum of dp decimal
731731
* places using rounding mode rm.
732732
* If dp is not specified, round to 0 decimal places.
733+
* If dp is negative, round to a multiple of 10**abs(dp).
733734
* If rm is not specified, use Big.RM.
734735
*
735736
* dp? {number} Integer, 0 to MAX_DP inclusive.
@@ -738,7 +739,7 @@ P.pow = function (n) {
738739
P.round = function (dp, rm) {
739740
var Big = this.constructor;
740741
if (dp === UNDEFINED) dp = 0;
741-
else if (dp !== ~~dp || dp < 0 || dp > MAX_DP) throw Error(INVALID_DP);
742+
else if (dp !== ~~dp || dp < -MAX_DP || dp > MAX_DP) throw Error(INVALID_DP);
742743
return round(new Big(this), dp, rm === UNDEFINED ? Big.RM : rm);
743744
};
744745

test/round.js

Lines changed: 217 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5007,7 +5007,6 @@ var count = (function round(Big) {
50075007
assertException(function () {new Big('12.345').round('99')}, ".round('99')");
50085008
assertException(function () {new Big('12.345').round('1.1e1')}, ".round('1.1e1')");
50095009
assertException(function () {new Big('12.345').round('-1')}, ".round('-1')");
5010-
assertException(function () {new Big('12.345').round(-23)}, ".round(-23)");
50115010
assertException(function () {new Big('12.345').round(1e9 + 1)}, ".round(1e9 + 1)");
50125011
assertException(function () {new Big('12.345').round(1e9 + 0.1)}, ".round(1e9 + 1)");
50135012
assertException(function () {new Big('12.345').round('-0.01')}, ".round('-0.01')");
@@ -5036,7 +5035,6 @@ var count = (function round(Big) {
50365035
assertException(function () {new Big('12.345').round(u, '99')}, ".round(u, '99')");
50375036
assertException(function () {new Big('12.345').round(u, '1.1e1')}, ".round(u, '1.1e1')");
50385037
assertException(function () {new Big('12.345').round(u, '-1')}, ".round('u, -1')");
5039-
assertException(function () {new Big('12.345').round(u, -23)}, ".round(u, -23)");
50405038
assertException(function () {new Big('12.345').round(u, 8.01)}, ".round(u, 8.01)");
50415039
assertException(function () {new Big('12.345').round(u, 9)}, ".round(u, 9)");
50425040
assertException(function () {new Big('12.345').round(u, -1)}, ".round(u, -1)");
@@ -5115,6 +5113,223 @@ var count = (function round(Big) {
51155113
T('13', '12.345', u);
51165114
T('12', '12.345', u, 2);
51175115

5116+
// Negative dp tests.
5117+
5118+
T('0', '0', -1, 0);
5119+
T('0', '0', -2, 0);
5120+
T('0', '0', -3, 0);
5121+
5122+
T('0', '0', -1, 1);
5123+
T('0', '0', -2, 1);
5124+
T('0', '0', -3, 1);
5125+
5126+
T('0', '0', -1, 2);
5127+
T('0', '0', -2, 2);
5128+
T('0', '0', -3, 2);
5129+
5130+
T('0', '0', -1, 3);
5131+
T('0', '0', -2, 3);
5132+
T('0', '0', -3, 3);
5133+
5134+
T('10', '10', -1, 3);
5135+
T('100', '10', -2, 3);
5136+
T('1000', '10', -3, 3);
5137+
5138+
T('1', '1', 0, 0);
5139+
T('1', '1', 0, 1);
5140+
T('1', '1', 0, 2);
5141+
T('1', '1', 0, 3);
5142+
5143+
T('0', '1', -1, 0);
5144+
T('0', '1', -1, 1);
5145+
T('0', '1', -1, 2);
5146+
T('10', '1', -1, 3);
5147+
5148+
T('0', '-1', -1, 0);
5149+
T('0', '-1', -1, 1);
5150+
T('0', '-1', -1, 2);
5151+
T('-10', '-1', -1, 3);
5152+
5153+
T('0', '1', -2, 0);
5154+
T('0', '1', -2, 1);
5155+
T('0', '1', -2, 2);
5156+
T('100', '1', -2, 3);
5157+
T('1000', '1', -3, 3);
5158+
T('10000', '1', -4, 3);
5159+
T('100000', '1', -5, 3);
5160+
5161+
T('0', '-1', -2, 0);
5162+
T('0', '-1', -2, 1);
5163+
T('0', '-1', -2, 2);
5164+
T('-100', '-1', -2, 3);
5165+
T('-1000', '-1', -3, 3);
5166+
T('-10000', '-1', -4, 3);
5167+
T('-100000', '-1', -5, 3);
5168+
5169+
T('0', '5', -1, 0);
5170+
T('10', '5', -1, 1);
5171+
T('0', '5', -1, 2);
5172+
T('10', '5', -1, 3);
5173+
5174+
T('0', '-5', -1, 0);
5175+
T('-10', '-5', -1, 1);
5176+
T('0', '-5', -1, 2);
5177+
T('-10', '-5', -1, 3);
5178+
5179+
T('0', '5', -2, 0);
5180+
T('0', '5', -2, 1);
5181+
T('0', '5', -2, 2);
5182+
T('100', '5', -2, 3);
5183+
5184+
T('0', '-5', -2, 0);
5185+
T('0', '-5', -2, 1);
5186+
T('0', '-5', -2, 2);
5187+
T('-100', '-5', -2, 3);
5188+
5189+
T('10', '14', -1, 0);
5190+
T('10', '14', -1, 1);
5191+
T('10', '14', -1, 2);
5192+
T('20', '14', -1, 3);
5193+
5194+
T('-10', '-14', -1, 0);
5195+
T('-10', '-14', -1, 1);
5196+
T('-10', '-14', -1, 2);
5197+
T('-20', '-14', -1, 3);
5198+
5199+
T('0', '14', -2, 0);
5200+
T('0', '14', -2, 1);
5201+
T('0', '14', -2, 2);
5202+
T('100', '14', -2, 3);
5203+
5204+
T('0', '-14', -2, 0);
5205+
T('0', '-14', -2, 1);
5206+
T('0', '-14', -2, 2);
5207+
T('-100', '-14', -2, 3);
5208+
5209+
T('10', '15', -1, 0);
5210+
T('20', '15', -1, 1);
5211+
T('20', '15', -1, 2);
5212+
T('20', '15', -1, 3);
5213+
5214+
T('-10', '-15', -1, 0);
5215+
T('-20', '-15', -1, 1);
5216+
T('-20', '-15', -1, 2);
5217+
T('-20', '-15', -1, 3);
5218+
5219+
T('0', '15', -2, 0);
5220+
T('0', '15', -2, 1);
5221+
T('0', '15', -2, 2);
5222+
T('100', '15', -2, 3);
5223+
5224+
T('0', '-15', -2, 0);
5225+
T('0', '-15', -2, 1);
5226+
T('0', '-15', -2, 2);
5227+
T('-100', '-15', -2, 3);
5228+
5229+
T('10', '16', -1, 0);
5230+
T('20', '16', -1, 1);
5231+
T('20', '16', -1, 2);
5232+
T('20', '16', -1, 3);
5233+
5234+
T('-10', '-16', -1, 0);
5235+
T('-20', '-16', -1, 1);
5236+
T('-20', '-16', -1, 2);
5237+
T('-20', '-16', -1, 3);
5238+
5239+
T('0', '16', -2, 0);
5240+
T('0', '16', -2, 1);
5241+
T('0', '16', -2, 2);
5242+
T('100', '16', -2, 3);
5243+
5244+
T('0', '-16', -2, 0);
5245+
T('0', '-16', -2, 1);
5246+
T('0', '-16', -2, 2);
5247+
T('-100', '-16', -2, 3);
5248+
5249+
T('10', '12.345', -1, 0);
5250+
T('10', '12.345', -1, 1);
5251+
T('10', '12.345', -1, 2);
5252+
T('20', '12.345', -1, 3);
5253+
5254+
T('-10', '-12.345', -1, 0);
5255+
T('-10', '-12.345', -1, 1);
5256+
T('-10', '-12.345', -1, 2);
5257+
T('-20', '-12.345', -1, 3);
5258+
5259+
T('0', '12.345', -2, 0);
5260+
T('0', '12.345', -2, 1);
5261+
T('0', '12.345', -2, 2);
5262+
T('100', '12.345', -2, 3);
5263+
T('1000', '12.345', -3, 3);
5264+
T('10000', '12.345', -4, 3);
5265+
T('100000', '12.345', -5, 3);
5266+
5267+
T('0', '-12.345', -2, 0);
5268+
T('0', '-12.345', -2, 1);
5269+
T('0', '-12.345', -2, 2);
5270+
T('-100', '-12.345', -2, 3);
5271+
T('-1000', '-12.345', -3, 3);
5272+
T('-10000', '-12.345', -4, 3);
5273+
T('-100000', '-12.345', -5, 3);
5274+
5275+
// Big.RM
5276+
// 0 Round towards zero (i.e. truncate, no rounding).
5277+
// 1 Round to nearest neighbour. If equidistant, round up.
5278+
// 2 Round to nearest neighbour. If equidistant, to even.
5279+
// 3 Round away from zero.
5280+
5281+
T('98765000', '98765000', -2, 0);
5282+
5283+
T('98765000', '98765000', -2, 1);
5284+
T('98765000', '98765000', -2, 2);
5285+
T('98765000', '98765000', -2, 3);
5286+
T('98765100', '98765000.000001', -2, 3);
5287+
5288+
T('98765000', '98765000', -3, 0);
5289+
T('98765000', '98765000', -3, 1);
5290+
T('98765000', '98765000', -3, 2);
5291+
T('98765000', '98765000', -3, 3);
5292+
5293+
T('98765000', '98765000.000001', -3, 0);
5294+
T('98765000', '98765000.000001', -3, 1);
5295+
T('98765000', '98765000.000001', -3, 2);
5296+
T('98766000', '98765000.000001', -3, 3);
5297+
5298+
T('98760000', '98765000', -4, 0);
5299+
T('98770000', '98765000', -4, 1);
5300+
T('98760000', '98765000', -4, 2);
5301+
T('98770000', '98765000', -4, 3);
5302+
5303+
T('98760000', '98765000.000001', -4, 0);
5304+
T('98770000', '98765000.000001', -4, 1);
5305+
T('98770000', '98765000.000001', -4, 2);
5306+
T('98770000', '98765000.000001', -4, 3);
5307+
5308+
T('98700000', '98765000', -5, 0);
5309+
T('98800000', '98765000', -5, 3);
5310+
T('98000000', '98765000', -6, 0);
5311+
T('99000000', '98765000', -6, 3);
5312+
5313+
T('90000000', '98765000', -7, 0);
5314+
T('100000000', '98765000', -7, 1);
5315+
T('100000000', '98765000', -7, 2);
5316+
T('100000000', '98765000', -7, 3);
5317+
5318+
T('0', '98765000', -8, 0);
5319+
T('100000000', '98765000', -8, 1);
5320+
T('100000000', '98765000', -8, 2);
5321+
T('100000000', '98765000', -8, 3);
5322+
5323+
T('0', '98765000', -9, 0);
5324+
T('0', '98765000', -9, 1);
5325+
T('0', '98765000', -9, 2);
5326+
T('1000000000', '98765000', -9, 3);
5327+
5328+
T('0', '98765000', -10, 0);
5329+
T('0', '98765000', -10, 1);
5330+
T('0', '98765000', -10, 2);
5331+
T('10000000000', '98765000', -10, 3);
5332+
51185333
log('\n ' + passed + ' of ' + total + ' tests passed in ' + (+new Date() - start) + ' ms \n');
51195334
return [passed, total];;
51205335
})(this.Big);

0 commit comments

Comments
 (0)