mirror of
https://github.com/MikeMcl/decimal.js.git
synced 2024-10-27 20:34:12 +00:00
187 lines
7.4 KiB
JavaScript
187 lines
7.4 KiB
JavaScript
var count = (function toNearest(Decimal) {
|
|
var start = +new Date(),
|
|
log,
|
|
error,
|
|
u,
|
|
passed = 0,
|
|
total = 0;
|
|
|
|
if (typeof window === 'undefined') {
|
|
log = console.log;
|
|
error = console.error;
|
|
} else {
|
|
log = function (str) { document.body.innerHTML += str.replace('\n', '<br>') };
|
|
error = function (str) { document.body.innerHTML += '<div style="color: red">' +
|
|
str.replace('\n', '<br>') + '</div>' };
|
|
}
|
|
|
|
if (!Decimal && typeof require === 'function') Decimal = require('../decimal');
|
|
|
|
function assert(expected, actual) {
|
|
total++;
|
|
|
|
if (expected !== actual) {
|
|
error('\n Test number: ' + total + ' failed');
|
|
error(' Expected: ' + expected);
|
|
error(' Actual: ' + actual);
|
|
//process.exit();
|
|
} else {
|
|
passed++;
|
|
//log('\n Expected and actual: ' + actual);
|
|
}
|
|
}
|
|
|
|
function isMinusZero(n) {
|
|
return n.toString() === '0' && n.s == -1;
|
|
}
|
|
|
|
function T(expected, value, n, pr, rm) {
|
|
Decimal.precision = pr;
|
|
assert(String(expected), String(new Decimal(value).toNearest(n, rm)));
|
|
}
|
|
|
|
log('\n Testing toNearest...');
|
|
|
|
Decimal.config({
|
|
precision: 20,
|
|
rounding: 4,
|
|
toExpNeg: -9e15,
|
|
toExpPos: 9e15,
|
|
minE: -9e15,
|
|
maxE: 9e15,
|
|
errors: true
|
|
});
|
|
|
|
T('Infinity', Infinity, u, 20, u);
|
|
T('-Infinity', -Infinity, u, 20, u);
|
|
T('NaN', NaN, u, 20, u);
|
|
T('NaN', NaN, NaN, 20, u);
|
|
T('NaN', NaN, Infinity, 20, u);
|
|
T('NaN', NaN, -Infinity, 20, u);
|
|
T('NaN', NaN, 0, 20, u);
|
|
T('NaN', NaN, -0, 20, u);
|
|
|
|
T('Infinity', '9.999e+9000000000000000', '1e+9000000000000001', 20, u);
|
|
T('Infinity', '9.999e+9000000000000000', '-1e+9000000000000001', 20, u);
|
|
T('-Infinity', '-9.999e+9000000000000000', '1e+9000000000000001', 20, u);
|
|
T('-Infinity', '-9.999e+9000000000000000', '-1e+9000000000000001', 20, u);
|
|
T('9.999e+9000000000000000', '9.999e+9000000000000000', u, 20, u);
|
|
T('-9.999e+9000000000000000', '-9.999e+9000000000000000', u, 20, u);
|
|
|
|
T('NaN', 123.456, NaN, 20, u);
|
|
T('Infinity', 123.456, Infinity, 20, u);
|
|
T('Infinity', 123.456, -Infinity, 20, u);
|
|
T('0', 123.456, 0, 20, u);
|
|
T('0', 123.456, '-0', 20, u);
|
|
|
|
T('NaN', -123.456, NaN, 20, u);
|
|
T('-Infinity', -123.456, Infinity, 20, u);
|
|
T('-Infinity', -123.456, -Infinity, 20, u);
|
|
T('0', -123.456, '-0', 20, u);
|
|
|
|
T('0', 0, 0, 20, u);
|
|
T('Infinity', 0, Infinity, 20, u);
|
|
T('Infinity', 0, -Infinity, 20, u);
|
|
T('-Infinity', -0, Infinity, 20, u);
|
|
T('-Infinity', -0, -Infinity, 20, u);
|
|
|
|
T('0', 1, -3, 20, u);
|
|
T('0', -1, -3, 20, u);
|
|
T('3', 1.5, -3, 20, 0);
|
|
T('0', -1.5, -3, 20, 1);
|
|
T('-3', -1.5, -3, 20, 2);
|
|
|
|
assert(false, isMinusZero(new Decimal(0).toNearest(0)));
|
|
assert(true, isMinusZero(new Decimal(-1).toNearest(0)));
|
|
assert(true, isMinusZero(new Decimal(-0).toNearest(0)));
|
|
assert(false, isMinusZero(new Decimal(1).toNearest(0)));
|
|
assert(false, isMinusZero(new Decimal(1).toNearest(-0)));
|
|
assert(false, isMinusZero(new Decimal(1).toNearest(-3)));
|
|
assert(true, isMinusZero(new Decimal(-1).toNearest(-3)));
|
|
|
|
T('123', 123.456, u, 20, u);
|
|
T('123', 123.456, 1, 20, u);
|
|
T('123.5', 123.456, 0.1, 20, u);
|
|
T('123.46', 123.456, 0.01, 20, u);
|
|
T('123.456', 123.456, 0.001, 20, u);
|
|
|
|
T('123', 123.456, -1, 20, u);
|
|
T('123.5', 123.456, -0.1, 20, u);
|
|
T('123.46', 123.456, -0.01, 20, u);
|
|
T('123.456', 123.456, -0.001, 20, u);
|
|
|
|
T('124', 123.456, '-2', 20, u);
|
|
T('123.4', 123.456, '-0.2', 20, u);
|
|
T('123.46', 123.456, '-0.02', 20, u);
|
|
T('123.456', 123.456, '-0.002', 20, u);
|
|
|
|
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.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', u, 30, 0);
|
|
T('83105511539', '83105511539.499999999999999999999999999999', 1, 30, 1);
|
|
T('83105511539', '83105511539.499999999999999999999999999999', u, 30, 2);
|
|
T('83105511539', '83105511539.499999999999999999999999999999', 1, 30, 3);
|
|
T('83105511539', '83105511539.499999999999999999999999999999', u, 30, 4);
|
|
T('83105511539', '83105511539.499999999999999999999999999999', 1, 30, 5);
|
|
T('83105511539', '83105511539.499999999999999999999999999999', u, 30, 6);
|
|
T('83105511539', '83105511539.499999999999999999999999999999', 1, 30, 7);
|
|
T('83105511539', '83105511539.499999999999999999999999999999', u, 30, 8);
|
|
T('83105511540', '83105511539.5000000000000000000001', u, 30, 0);
|
|
T('83105511540', '83105511539.5000000000000000000001', 1, 30, 1);
|
|
T('83105511540', '83105511539.5000000000000000000001', u, 30, 2);
|
|
T('83105511540', '83105511539.5000000000000000000001', 1, 30, 3);
|
|
T('83105511540', '83105511539.5000000000000000000001', u, 30, 4);
|
|
T('83105511540', '83105511539.5000000000000000000001', 1, 30, 5);
|
|
T('83105511540', '83105511539.5000000000000000000001', u, 30, 6);
|
|
T('83105511540', '83105511539.5000000000000000000001', 1, 30, 7);
|
|
T('83105511540', '83105511539.5000000000000000000001', u, 30, 8);
|
|
|
|
Decimal.rounding = 0;
|
|
T('83105511540', '83105511539.5', u, 11, u);
|
|
Decimal.rounding = 1;
|
|
T('83105511539', '83105511539.5', u, 11, u);
|
|
|
|
T('3847560', '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);
|
|
|
|
|
|
log('\n ' + passed + ' of ' + total + ' tests passed in ' + (+new Date() - start) + ' ms \n');
|
|
return [passed, total];
|
|
})(this.Decimal);
|
|
if (typeof module !== 'undefined' && module.exports) module.exports = count;
|