1
0
mirror of https://github.com/MikeMcl/decimal.js.git synced 2026-03-02 03:49:24 +00:00
Avoid potential confusion over the round method: ceil, floor, round and
trunc no longer accept arguments and so they match their JS Math object
equivalents. Removed toInteger as round now handles rounding to integer. Added
toSignificantDigits as round no longer rounds to precision. Updated tests
accordingly. Calling config without argument no longer throws.
This commit is contained in:
Michael Mclaughlin
2014-04-10 19:30:38 +01:00
parent 714eb63369
commit b141f3480d
18 changed files with 30307 additions and 30437 deletions

View File

@@ -46,10 +46,10 @@
'toFixed',
'toFormat',
'toFraction',
'toInt',
'toNearest',
'toNumber',
'toPrecision',
'toSD',
'toStringEtc',
'trunc'
];

View File

@@ -39,10 +39,10 @@
<!-- <script src='../toFixed.js'></script> -->
<!-- <script src='../toFormat.js'></script> -->
<!-- <script src='../toFraction.js'></script> -->
<!-- <script src='../toInt.js'></script> -->
<!-- <script src='../toNearest.js'></script> -->
<!-- <script src='../toNumber.js'></script> -->
<!-- <script src='../toPrecision.js'></script> -->
<!-- <script src='../toSD.js'></script> -->
<!-- <script src='../toStringEtc.js'></script> -->
<!-- <script src='../trunc.js'></script> -->

View File

@@ -49,8 +49,8 @@ var count = (function ceil(Decimal) {
}
}
function T(expected, value, n) {
assert(String(expected), new Decimal(String(value)).ceil(n).toString());
function T(expected, value) {
assert(String(expected), new Decimal(String(value)).ceil().toString());
}
log('\n Testing ceil...');
@@ -58,8 +58,8 @@ var count = (function ceil(Decimal) {
Decimal.config({
precision: 20,
rounding: 4,
toExpNeg: -9e15,
toExpPos: 9e15,
toExpNeg: -1e3,
toExpPos: 1e3,
minE: -9e15,
maxE: 9e15,
errors: true
@@ -105,6 +105,11 @@ var count = (function ceil(Decimal) {
T('2949426983040960', '2949426983040959.8911208825380208568451907');
T('25167', '25166.125888418871654557352055849116604612621573251770362');
T('4560569286496', '4560569286495.98300685103599898554605198');
T('11', '10.5');
T('0', '-0.0101');
T('-1', '-1.0101');
T('98743655', '98743654.999999999999999999');
T('-98743654', '-98743654.999999999999999999');
T('14', '13.763105480576616251068323541559825687');
T('176037174185746614410406167888', '176037174185746614410406167887.42317518');
T('9050999219307', '9050999219306.7846946346757664893036971777');
@@ -223,34 +228,17 @@ var count = (function ceil(Decimal) {
// ------------------------------------------------------------------ v8 end
assertException(function () {new Decimal('0.6').ceil(0)}, "ceil(0)");
assertException(function () {new Decimal('1.6').ceil(0)}, "ceil(0)");
assert('2', new Decimal('1.0000000000000000001').ceil().toString());
T('0.7', '0.66', 1);
T('2', '1.66', 1);
T('0.6667', '0.66666', 4);
T('1.667', '1.66666', 4);
T('0.6', '0.6', 1);
T('2', '1.6', 1);
T('0.67', '0.666', 2);
T('1.7', '1.66', 2);
T('0.66667', '0.666666', 5);
T('1.6667', '1.66666', 5);
T('-1', '-1.0', 1);
T('-1', '-1.00001', 1);
T('-1', '-1.010203', 2);
T('-2.99', '-2.999', 3);
T('-0.0001', '-0.000123', 1);
T('-0.000123', '-0.000123', 4);
T('0.010203', '0.010203', 5);
T('0.0102031', '0.01020301', 6);
T('-9.999', '-9.999', 6);
T('-9.9999', '-9.999900000001', 6);
T('1', '1e-9000000000000000');
T('0', '-1e-9000000000000000');
T('0', '-9.9e-9000000000000001');
T('9.999999e+9000000000000000', '9.999999e+9000000000000000');
T('-9.999999e+9000000000000000', '-9.999999e+9000000000000000');
T('Infinity', '1E9000000000000001');
T('-Infinity', '-1e+9000000000000001');
T('5.5879983320336874473209567979e+287894365', '5.5879983320336874473209567979e+287894365');
T('-5.5879983320336874473209567979e+287894365', '-5.5879983320336874473209567979e+287894365');
log('\n ' + passed + ' of ' + total + ' tests passed in ' + (+new Date() - start) + ' ms \n');
return [passed, total];

View File

@@ -91,10 +91,11 @@ var count = (function config(Decimal) {
modulo: 4
});
// Throw if no object is passed.
assertException(function () {Decimal.config()}, "config()");
assertException(function () {Decimal.config(undefined)}, "config(undefined)");
assertException(function () {Decimal.config(null)}, "config(null)");
assert(true, Decimal.config() === Decimal);
assert(true, Decimal.config(undefined) === Decimal);
assert(true, Decimal.config(null) === Decimal);
// Throw if not an object object.
assertException(function () {Decimal.config('')}, "config('')");
assertException(function () {Decimal.config('hi')}, "config('hi')");
assertException(function () {Decimal.config(4)}, "config(4)");

View File

@@ -124,8 +124,8 @@ var count = (function constructor(Decimal) {
assert(false, new Decimal(9.99).eq(new D3('-9.99')));
assert(true, new Decimal(9.99).eq(new D5('9.99')));
assert(false, new Decimal(123.456789).round().eq(new D3('123.456789').round()));
assert(true, new Decimal(123.456789).round(5).eq(new D3('123.456789').round(5)));
assert(false, new Decimal(123.456789).toSD().eq(new D3('123.456789').toSD()));
assert(true, new Decimal(123.456789).round().eq(new D3('123.456789').round()));
log('\n ' + passed + ' of ' + total + ' tests passed in ' + (+new Date() - start) + ' ms \n');
return [passed, total];

View File

@@ -38,10 +38,10 @@ console.log( '\n STARTING TESTS...\n' );
'toFixed',
'toFormat',
'toFraction',
'toInt',
'toNearest',
'toNumber',
'toPrecision',
'toSD',
'toStringEtc',
'trunc'
]

View File

@@ -48,8 +48,8 @@ var count = (function floor(Decimal) {
}
}
function T(expected, value, n) {
assert(String(expected), new Decimal(String(value)).floor(n).toString());
function T(expected, value) {
assert(String(expected), new Decimal(String(value)).floor().toString());
}
log('\n Testing floor...');
@@ -57,8 +57,8 @@ var count = (function floor(Decimal) {
Decimal.config({
precision: 20,
rounding: 4,
toExpNeg: -9e15,
toExpPos: 9e15,
toExpNeg: -1e3,
toExpPos: 1e3,
minE: -9e15,
maxE: 9e15,
errors: true
@@ -80,7 +80,8 @@ var count = (function floor(Decimal) {
T('70591', '70591.2244675522123484658978887');
T('4446128540401735117', '4446128540401735117.435836700611264749985822486641350492901');
T('-597273', '-597273');
T('729117', '729117');
T('729117', '729117.5');
T('-729118', '-729117.001');
T('4803729546823170064608098091', '4803729546823170064608098091');
T('-6581532150677269472830', '-6581532150677269472829.38194951340848938896000325718062365494');
T('2949426983040959', '2949426983040959.8911208825380208568451907');
@@ -88,7 +89,7 @@ var count = (function floor(Decimal) {
T('4560569286495', '4560569286495.98300685103599898554605198');
T('13', '13.763105480576616251068323541559825687');
T('9050999219306', '9050999219306.7846946346757664893036971777');
T('39900924', '39900924');
T('39900924', '39900924.00000005');
T('115911043168452445', '115911043168452445');
T('20962819101135667464733349383', '20962819101135667464733349383.8959025798517496777183');
T('4125789711001606948191', '4125789711001606948191.4707575965791242737346836');
@@ -99,6 +100,7 @@ var count = (function floor(Decimal) {
T('657', '657.0353902852');
T('0', '0.00000000000000000000000017921822306362413915');
T('1483059355427939255846407887', '1483059355427939255846407887.011361095342689876');
T('7.722e+999999999999999', '7.722e+999999999999999');
T('7722', '7722');
T('0', '0.00000005');
T('8551283060956479352', '8551283060956479352.5707396');
@@ -206,37 +208,8 @@ var count = (function floor(Decimal) {
// ------------------------------------------------------------------ v8 end
assertException(function () {new Decimal('0.6').floor(0)}, "floor(0)");
assertException(function () {new Decimal('1.6').floor(0)}, "floor(0)");
T('0.6', '0.66', 1);
T('1', '1.66', 1);
T('0.6666', '0.66666', 4);
T('1.666', '1.66666', 4);
T('0.6', '0.6', 1);
T('1', '1.6', 1);
T('0.66', '0.666', 2);
T('1.6', '1.66', 2);
T('0.66666', '0.666666', 5);
T('1.6666', '1.66666', 5);
assert('1', new Decimal('1.9999999999').floor().toString());
T('-1', '-1.0', 1);
T('-2', '-1.00001', 1);
T('-1.1', '-1.010203', 2);
T('-3', '-2.999', 3);
T('-0.0002', '-0.000123', 1);
T('-0.000123', '-0.000123', 4);
T('0.010203', '0.010203', 5);
T('0.010203', '0.01020301', 6);
T('-9.999', '-9.999', 6);
T('-9.99991', '-9.999900000001', 6);
T('0', '1e-9000000000000000');
T('-1', '-1e-9000000000000000');
T('0', '-9.9e-9000000000000001');

View File

@@ -320,7 +320,6 @@ var count = (function others(Decimal) {
assert(true, new Decimal(0).lessThanOrEqualTo('Infinity', 36));
assert(false, new Decimal(0).greaterThanOrEqualTo('Infinity', 36));
assert(false, new Decimal(10).lessThanOrEqualTo(20, 4));
assert(true, new Decimal(10).lessThanOrEqualTo(20, 5));
assert(false, new Decimal(10).greaterThanOrEqualTo(20, 6));
assert(false, new Decimal(1.23001e-2).lessThan(1.23e-2));

View File

@@ -152,6 +152,7 @@ var count = (function minMax(Decimal) {
T(-Infinity, 1, [1, '-1e+9000000000000001', -1e200]);
T(0, 1, [1, '1e-9000000000000001', 1e-200]);
T(0, 1, [1, '-1e-9000000000000001', 1e-200]);
T(-3, 3, [1, '2', 3, '-1', -2, '-3']);
log('\n ' + passed + ' of ' + total + ' tests passed in ' + (+new Date() - start) + ' ms \n');
return [passed, total];

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

20097
test/toSD.js Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -48,9 +48,9 @@ var count = (function trunc(Decimal) {
}
}
function T(expected, value, n) {
assert(String(expected), new Decimal(String(value)).trunc(n).toString());
//assert(String(expected), new Decimal(String(value)).truncated(n).toString());
function T(expected, value) {
assert(String(expected), new Decimal(String(value)).trunc().toString());
//assert(String(expected), new Decimal(String(value)).truncated().toString());
}
log('\n Testing trunc...');
@@ -58,8 +58,8 @@ var count = (function trunc(Decimal) {
Decimal.config({
precision: 20,
rounding: 4,
toExpNeg: -9e15,
toExpPos: 9e15,
toExpNeg: -1e3,
toExpPos: 1e3,
minE: -9e15,
maxE: 9e15,
errors: true
@@ -74,13 +74,11 @@ var count = (function trunc(Decimal) {
T('-3633239209', '-3633239209.654526163275621746013315304191073405508491056');
T('-23970335459820625362', '-23970335459820625362');
T('131869457416154038', '131869457416154038');
T('-2685', '-2685');
T('-4542227860', '-4542227860.9511298545226');
T('2416872281', '2416872281.963955669484225137349193306323379254936827');
T('-757684868752087594264588207655', '-757684868752087594264588207655.27838048392835556');
T('-438798503526', '-438798503526.2317623894721299587561697');
T('801625782231888715214665', '801625782231888715214665');
T('-91881984778675238', '-91881984778675238');
T('327765350218284325239839632046', '327765350218284325239839632046.91682741746683081459605386');
T('-7469045007691432294', '-7469045007691432294.362757245');
T('8365540212937142194319515218789', '8365540212937142194319515218789.4106658678537421977827');
@@ -96,14 +94,12 @@ var count = (function trunc(Decimal) {
T('4446128540401735117', '4446128540401735117.435836700611264749985822486641350492901');
T('-597273', '-597273');
T('729117', '729117');
T('504', '504');
T('-6581532150677269472829', '-6581532150677269472829.38194951340848938896000325718062365494');
T('-131279182164804751', '-131279182164804751.430589952021038264');
T('2949426983040959', '2949426983040959.8911208825380208568451907');
T('25166', '25166.125888418871654557352055849116604612621573251770362');
T('4560569286495', '4560569286495.98300685103599898554605198');
T('13', '13.763105480576616251068323541559825687');
T('176037174185746614410406167887', '176037174185746614410406167887.42317518');
T('9050999219306', '9050999219306.7846946346757664893036971777');
T('20962819101135667464733349383', '20962819101135667464733349383.8959025798517496777183');
T('4125789711001606948191', '4125789711001606948191.4707575965791242737346836');
@@ -131,7 +127,6 @@ var count = (function trunc(Decimal) {
T('0', '0.00000000000000000000004');
T('0', '0.000250427721966583680168028884692015623739');
T('0', '0.000000000001585613219016120158734661293405081934');
T('0', '0.00009');
T('0', '0.000000090358252973411013592234');
T('276312604693909858427', '276312604693909858427.21965306055697011390137926559');
T('0', '0.0000252');
@@ -176,38 +171,9 @@ var count = (function trunc(Decimal) {
T(Infinity, Infinity);
T(-Infinity, -Infinity);
assertException(function () {new Decimal('0.6').trunc(0)}, "trunc(0)");
assertException(function () {new Decimal('1.6').trunc(0)}, "trunc(0)");
T('0.6', '0.66', 1);
T('1', '1.66', 1);
T('0.6666', '0.66666', 4);
T('1.666', '1.66666', 4);
T('0.6', '0.6', 1);
T('1', '1.6', 1);
T('0.66', '0.666', 2);
T('1.6', '1.66', 2);
T('0.66666', '0.666666', 5);
T('1.6666', '1.66666', 5);
assert('1', new Decimal('1.9999999999').trunc().toString());
assert('-1', new Decimal('-1.9999999999').trunc().toString());
T('-1', '-1.0', 1);
T('-1', '-1.00001', 1);
T('-1', '-1.010203', 2);
T('-2.99', '-2.999', 3);
T('-0.0001', '-0.000123', 1);
T('-0.000123', '-0.000123', 4);
T('0.010203', '0.010203', 5);
T('0.010203', '0.01020301', 6);
T('-9.999', '-9.999', 6);
T('-9.9999', '-9.999900000001', 6);
log('\n ' + passed + ' of ' + total + ' tests passed in ' + (+new Date() - start) + ' ms \n');
return [passed, total];
})(this.Decimal);