• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1if (typeof T === 'undefined') require('../setup');
2
3T('decimalPlaces and precision', function () {
4
5  function t(n, dp, sd, zs) {
6    T.assertEqual(dp, new Decimal(n).dp());
7    T.assertEqual(dp, new Decimal(n).decimalPlaces());
8    T.assertEqual(sd, new Decimal(n).sd(zs));
9    T.assertEqual(sd, new Decimal(n).precision(zs));
10  }
11
12  function tx(fn, msg) {
13    T.assertException(fn, msg);
14  }
15
16  Decimal.config({
17    precision: 20,
18    rounding: 4,
19    toExpNeg: -7,
20    toExpPos: 21,
21    minE: -9e15,
22    maxE: 9e15
23  });
24
25  t(0, 0, 1);
26  t(-0, 0, 1);
27  t(NaN, NaN, NaN);
28  t(Infinity, NaN, NaN);
29  t(-Infinity, NaN, NaN);
30  t(1, 0, 1);
31  t(-1, 0, 1);
32
33  t(100, 0, 1);
34  t(100, 0, 1, 0);
35  t(100, 0, 1, false);
36  t(100, 0, 3, 1);
37  t(100, 0, 3, true);
38
39  t('0.0012345689', 10, 8);
40  t('0.0012345689', 10, 8, 0);
41  t('0.0012345689', 10, 8, false);
42  t('0.0012345689', 10, 8, 1);
43  t('0.0012345689', 10, 8, true);
44
45  t('987654321000000.0012345689000001', 16, 31, 0);
46  t('987654321000000.0012345689000001', 16, 31, 1);
47
48  t('1e+123', 0, 1);
49  t('1e+123', 0, 124, 1);
50  t('1e-123', 123, 1);
51  t('1e-123', 123, 1, 1);
52
53  t('9.9999e+9000000000000000', 0, 5, false);
54  t('9.9999e+9000000000000000', 0, 9000000000000001, true);
55  t('-9.9999e+9000000000000000', 0, 5, false);
56  t('-9.9999e+9000000000000000', 0, 9000000000000001, true);
57
58  t('1e-9000000000000000', 9e15, 1, false);
59  t('1e-9000000000000000', 9e15, 1, true);
60  t('-1e-9000000000000000', 9e15, 1, false);
61  t('-1e-9000000000000000', 9e15, 1, true);
62
63  t('55325252050000000000000000000000.000000004534500000001', 21, 53);
64
65  tx(function () {new Decimal(1).precision(null)}, "new Decimal(1).precision(null)");
66  tx(function () {new Decimal(1).sd(null)}, "new Decimal(1).sd(null)");
67  tx(function () {new Decimal(1).sd(2)}, "new Decimal(1).sd(2)");
68  tx(function () {new Decimal(1).sd('3')}, "new Decimal(1).sd('3')");
69  tx(function () {new Decimal(1).sd({})}, "new Decimal(1).sd({})");
70});
71