1
0
mirror of https://github.com/MikeMcl/decimal.js.git synced 2024-10-27 20:34:12 +00:00
MikeMcl_decimal.js/test/toNumber.js

95 lines
2.8 KiB
JavaScript
Raw Normal View History

2014-04-02 15:28:08 +00:00
var count = (function toNumber(Decimal) {
var start = +new Date(),
log,
error,
undefined,
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++;
2014-05-08 18:27:25 +00:00
if (expected === actual || isNaN(expected) && isNaN(actual)) {
passed++;
//log('\n Expected and actual: ' + actual);
} else {
2014-04-02 15:28:08 +00:00
error('\n Test number: ' + total + ' failed');
error(' Expected: ' + expected);
error(' Actual: ' + actual);
//process.exit();
}
}
function isMinusZero(n) {
return 1 / n === -Infinity;
}
function T(value, n) {
2014-05-08 18:27:25 +00:00
assert(n, new Decimal(value).toNumber());
2014-04-02 15:28:08 +00:00
}
log('\n Testing toNumber...');
Decimal.config({
precision: 20,
rounding: 4,
toExpNeg: -7,
toExpPos: 21,
minE: -9e15,
maxE: 9e15,
errors: true
});
assert(false, isMinusZero(new Decimal('0').toNumber()));
assert(false, isMinusZero(new Decimal('0.0').toNumber()));
assert(false, isMinusZero(new Decimal('0.000000000000').toNumber()));
assert(false, isMinusZero(new Decimal('0e+0').toNumber()));
assert(false, isMinusZero(new Decimal('0e-0').toNumber()));
assert(false, isMinusZero(new Decimal('1e-9000000000000000').toNumber()));
assert(true, isMinusZero(new Decimal('-0').toNumber()));
assert(true, isMinusZero(new Decimal('-0.0').toNumber()));
assert(true, isMinusZero(new Decimal('-0.000000000000').toNumber()));
assert(true, isMinusZero(new Decimal('-0e+0').toNumber()));
assert(true, isMinusZero(new Decimal('-0e-0').toNumber()));
assert(true, isMinusZero(new Decimal('-1e-9000000000000000').toNumber()));
T(1, 1);
T('1', 1);
T('1.0', 1);
T('1e+0', 1);
T('1e-0', 1);
T(-1, -1);
T('-1', -1);
T('-1.0', -1);
T('-1e+0', -1);
T('-1e-0', -1);
T(Infinity, 1 / 0);
T('Infinity', 1 / 0);
T(-Infinity, -1 / 0);
T('-Infinity', -1 / 0);
2014-05-08 18:27:25 +00:00
T(NaN, NaN);
T('NaN', NaN);
2014-04-02 15:28:08 +00:00
T('9.999999e+9000000000000000', 1 / 0);
T('-9.999999e+9000000000000000', -1 / 0);
T('1e-9000000000000000', 0);
T('-1e-9000000000000000', -0);
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;