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

71 lines
1.9 KiB
JavaScript
Raw Normal View History

2016-01-25 00:11:32 +00:00
if (typeof T === 'undefined') require('../setup');
2016-02-06 17:51:29 +00:00
T('decimalPlaces and precision', function () {
2016-01-25 00:11:32 +00:00
2016-02-04 23:52:10 +00:00
function t(n, dp, sd, zs) {
T.assertEqual(dp, new Decimal(n).dp());
T.assertEqual(dp, new Decimal(n).decimalPlaces());
T.assertEqual(sd, new Decimal(n).sd(zs));
T.assertEqual(sd, new Decimal(n).precision(zs));
}
2016-01-25 00:11:32 +00:00
2016-02-04 23:52:10 +00:00
function tx(fn, msg) {
T.assertException(fn, msg);
}
2016-01-25 00:11:32 +00:00
2016-02-04 23:52:10 +00:00
Decimal.config({
precision: 20,
rounding: 4,
toExpNeg: -7,
toExpPos: 21,
minE: -9e15,
maxE: 9e15
});
2016-01-25 00:11:32 +00:00
2016-02-04 23:52:10 +00:00
t(0, 0, 1);
t(-0, 0, 1);
t(NaN, NaN, NaN);
t(Infinity, NaN, NaN);
t(-Infinity, NaN, NaN);
t(1, 0, 1);
t(-1, 0, 1);
2016-01-25 00:11:32 +00:00
2016-02-04 23:52:10 +00:00
t(100, 0, 1);
t(100, 0, 1, 0);
t(100, 0, 1, false);
t(100, 0, 3, 1);
t(100, 0, 3, true);
2016-01-25 00:11:32 +00:00
2016-02-04 23:52:10 +00:00
t('0.0012345689', 10, 8);
t('0.0012345689', 10, 8, 0);
t('0.0012345689', 10, 8, false);
t('0.0012345689', 10, 8, 1);
t('0.0012345689', 10, 8, true);
2016-01-25 00:11:32 +00:00
2016-02-04 23:52:10 +00:00
t('987654321000000.0012345689000001', 16, 31, 0);
t('987654321000000.0012345689000001', 16, 31, 1);
2016-01-25 00:11:32 +00:00
2016-02-04 23:52:10 +00:00
t('1e+123', 0, 1);
t('1e+123', 0, 124, 1);
t('1e-123', 123, 1);
t('1e-123', 123, 1, 1);
2016-01-25 00:11:32 +00:00
2016-02-04 23:52:10 +00:00
t('9.9999e+9000000000000000', 0, 5, false);
t('9.9999e+9000000000000000', 0, 9000000000000001, true);
t('-9.9999e+9000000000000000', 0, 5, false);
t('-9.9999e+9000000000000000', 0, 9000000000000001, true);
2016-01-25 00:11:32 +00:00
2016-02-04 23:52:10 +00:00
t('1e-9000000000000000', 9e15, 1, false);
t('1e-9000000000000000', 9e15, 1, true);
t('-1e-9000000000000000', 9e15, 1, false);
t('-1e-9000000000000000', 9e15, 1, true);
2016-01-25 00:11:32 +00:00
2016-02-04 23:52:10 +00:00
t('55325252050000000000000000000000.000000004534500000001', 21, 53);
2016-01-25 00:11:32 +00:00
2016-02-04 23:52:10 +00:00
tx(function () {new Decimal(1).precision(null)}, "new Decimal(1).precision(null)");
tx(function () {new Decimal(1).sd(null)}, "new Decimal(1).sd(null)");
tx(function () {new Decimal(1).sd(2)}, "new Decimal(1).sd(2)");
tx(function () {new Decimal(1).sd('3')}, "new Decimal(1).sd('3')");
tx(function () {new Decimal(1).sd({})}, "new Decimal(1).sd({})");
2016-02-06 17:51:29 +00:00
});