• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1if (typeof T === 'undefined') require('../setup');
2
3T('toNearest', function () {
4
5  function isMinusZero(n) {
6    return n.isZero() && n.isNegative();
7  }
8
9  Decimal.config({
10    precision: 20,
11    rounding: 4,
12    toExpNeg: -9e15,
13    toExpPos: 9e15,
14    minE: -9e15,
15    maxE: 9e15
16  });
17
18  var t = function (actual) {
19    T.assert(actual);
20  }
21
22  t(!isMinusZero(new Decimal(0).toNearest(0)));
23  t( isMinusZero(new Decimal(-1).toNearest(0)));
24  t( isMinusZero(new Decimal(-0).toNearest(0)));
25  t(!isMinusZero(new Decimal(1).toNearest(0)));
26  t(!isMinusZero(new Decimal(1).toNearest(-0)));
27  t(!isMinusZero(new Decimal(1).toNearest(-3)));
28  t( isMinusZero(new Decimal(-1).toNearest(-3)));
29
30  t = function (expected, n, v, rm) {
31    T.assertEqual(expected, new Decimal(n).toNearest(v, rm).valueOf());
32  }
33
34  t('Infinity', Infinity);
35  t('-Infinity', -Infinity);
36  t('NaN', NaN);
37  t('NaN', NaN, NaN);
38  t('NaN', NaN, Infinity);
39  t('NaN', NaN, -Infinity);
40  t('NaN', NaN, 0);
41  t('NaN', NaN, -0);
42
43  t('Infinity', '9.999e+9000000000000000', '1e+9000000000000001');
44  t('Infinity', '9.999e+9000000000000000', '-1e+9000000000000001');
45  t('-Infinity', '-9.999e+9000000000000000', '1e+9000000000000001');
46  t('-Infinity', '-9.999e+9000000000000000', '-1e+9000000000000001');
47  t('9.999e+9000000000000000', '9.999e+9000000000000000');
48  t('-9.999e+9000000000000000', '-9.999e+9000000000000000');
49
50  t('NaN', 123.456, NaN);
51  t('Infinity', 123.456, Infinity);
52  t('Infinity', 123.456, -Infinity);
53  t('0', 123.456, 0);
54  t('0', 123.456, '-0');
55
56  t('NaN', -123.456, NaN);
57  t('-Infinity', -123.456, Infinity);
58  t('-Infinity', -123.456, -Infinity);
59  t('-0', -123.456, '-0');
60
61  t('0', 0, 0);
62  t('Infinity', 0, Infinity);
63  t('Infinity', 0, -Infinity);
64  t('-Infinity', -0, Infinity);
65  t('-Infinity', -0, -Infinity);
66
67  t('0', 1, -3);
68  t('-0', -1, -3);
69  t('3', 1.5, -3, 0);
70  t('-0', -1.5, -3, 1);
71  t('-3', -1.5, -3, 2);
72
73  t('123', 123.456);
74  t('123', 123.456, 1);
75  t('123.5', 123.456, 0.1);
76  t('123.46', 123.456, 0.01);
77  t('123.456', 123.456, 0.001);
78
79  t('123', 123.456, -1);
80  t('123.5', 123.456, -0.1);
81  t('123.46', 123.456, -0.01);
82  t('123.456', 123.456, -0.001);
83
84  t('124', 123.456, '-2');
85  t('123.4', 123.456, '-0.2');
86  t('123.46', 123.456, '-0.02');
87  t('123.456', 123.456, '-0.002');
88
89  t('83105511540', '83105511539.5', 1, 4);
90  t('83105511539', '83105511539.499999999999999999999999999999', 1, 4);
91  t('83105511539', '83105511539.5', '1', 5);
92  t('83105511540', '83105511539.5000000000000000000001', 1, 5);
93
94  Decimal.precision = 3;
95
96  t('83105511540', '83105511539.5', new Decimal(1), 4);
97  t('83105511539', '83105511539.499999999999999999999999999999', 1, 4);
98  t('83105511539', '83105511539.5', new Decimal('1'), 5);
99  t('83105511540', '83105511539.5000000000000000000001', 1, 5);
100
101  Decimal.precision = 20;
102
103  t('83105511540', '83105511539.5', -1, 4);
104  t('83105511539', '83105511539.499999999999999999999999999999', -1, 4);
105  t('83105511539', '83105511539.5', '-1', 5);
106  t('83105511540', '83105511539.5000000000000000000001', -1, 5);
107
108  t('-83105511540', '-83105511539.5', new Decimal(-1), 4);
109  t('-83105511539', '-83105511539.499999999999999999999999999999', 1, 4);
110  t('-83105511539', '-83105511539.5', new Decimal('-1'), 5);
111  t('-83105511540', '-83105511539.5000000000000000000001', -1, 5);
112
113  t('83105511540', '83105511539.5', 1, 0);
114  t('83105511539', '83105511539.5', 1, 1);
115  t('83105511540', '83105511539.5', 1, 2);
116  t('83105511539', '83105511539.5', 1, 3);
117  t('83105511540', '83105511539.5', 1, 4);
118  t('83105511539', '83105511539.5', 1, 5);
119  t('83105511540', '83105511539.5', 1, 6);
120  t('83105511540', '83105511539.5', 1, 7);
121  t('83105511539', '83105511539.5', 1, 8);
122  t('83105511539', '83105511539.499999999999999999999999999999', void 0, 0);
123  t('83105511539', '83105511539.499999999999999999999999999999', 1, 1);
124  t('83105511539', '83105511539.499999999999999999999999999999', void 0, 2);
125  t('83105511539', '83105511539.499999999999999999999999999999', 1, 3);
126  t('83105511539', '83105511539.499999999999999999999999999999', void 0, 4);
127  t('83105511539', '83105511539.499999999999999999999999999999', 1, 5);
128  t('83105511539', '83105511539.499999999999999999999999999999', void 0, 6);
129  t('83105511539', '83105511539.499999999999999999999999999999', 1, 7);
130  t('83105511539', '83105511539.499999999999999999999999999999', void 0, 8);
131  t('83105511540', '83105511539.5000000000000000000001', void 0, 0);
132  t('83105511539', '83105511539.5000000000000000000001', 1, 1);
133  t('83105511540', '83105511539.5000000000000000000001', void 0, 2);
134  t('83105511539', '83105511539.5000000000000000000001', 1, 3);
135  t('83105511540', '83105511539.5000000000000000000001', void 0, 4);
136  t('83105511540', '83105511539.5000000000000000000001', 1, 5);
137  t('83105511540', '83105511539.5000000000000000000001', void 0, 6);
138  t('83105511540', '83105511539.5000000000000000000001', 1, 7);
139  t('83105511540', '83105511539.5000000000000000000001', void 0, 8);
140
141  Decimal.rounding = 0;
142  t('83105511540', '83105511539.5');
143
144  Decimal.rounding = 1;
145  t('83105511539', '83105511539.5');
146
147  t('3847570', '3847561.00000749', 10, 0);
148  t('42840000000000000', '42835000000000001', '1e+13', 0);
149  t('42830000000000000', '42835000000000001', '1e+13', 1);
150  t('42840000000000000', '42835000000000000.0002', '1e+13', 0);
151  t('42830000000000000', '42835000000000000.0002', '1e+13', 1);
152
153  t('500', '449.999', 100, 0);
154  t('400', '449.999', 100, 1);
155  t('500', '449.999', 100, 2);
156  t('400', '449.999', 100, 3);
157  t('400', '449.999', 100, 4);
158  t('400', '449.999', 100, 5);
159  t('400', '449.999', 100, 6);
160  t('400', '449.999', 100, 7);
161  t('400', '449.999', 100, 8);
162
163  t('-500', '-449.999', 100, 0);
164  t('-400', '-449.999', 100, 1);
165  t('-400', '-449.999', 100, 2);
166  t('-500', '-449.999', 100, 3);
167  t('-400', '-449.999', 100, 4);
168  t('-400', '-449.999', 100, 5);
169  t('-400', '-449.999', 100, 6);
170  t('-400', '-449.999', 100, 7);
171  t('-400', '-449.999', 100, 8);
172
173  t('500', '450', 100, 0);
174  t('400', '450', 100, 1);
175  t('500', '450', 100, 2);
176  t('400', '450', 100, 3);
177  t('500', '450', 100, 4);
178  t('400', '450', 100, 5);
179  t('400', '450', 100, 6);
180  t('500', '450', 100, 7);
181  t('400', '450', 100, 8);
182
183  t('-500', '-450', 100, 0);
184  t('-400', '-450', 100, 1);
185  t('-400', '-450', 100, 2);
186  t('-500', '-450', 100, 3);
187  t('-500', '-450', 100, 4);
188  t('-400', '-450', 100, 5);
189  t('-400', '-450', 100, 6);
190  t('-400', '-450', 100, 7);
191  t('-500', '-450', 100, 8);
192
193  Decimal.rounding = 0;
194  t('500', '450.001', 100);
195  Decimal.rounding = 1;
196  t('400', '450.001', 100);
197  Decimal.rounding = 2;
198  t('500', '450.001', 100);
199  Decimal.rounding = 3;
200  t('400', '450.001', 100);
201  Decimal.rounding = 4;
202  t('500', '450.001', 100);
203  Decimal.rounding = 5;
204  t('500', '450.001', 100);
205  Decimal.rounding = 6;
206  t('500', '450.001', 100);
207  Decimal.rounding = 7;
208  t('500', '450.001', 100);
209  Decimal.rounding = 8;
210  t('500', '450.001', 100);
211
212  Decimal.rounding = 0;
213  t('-500', '-450.001', 100);
214  Decimal.rounding = 1;
215  t('-400', '-450.001', 100);
216  Decimal.rounding = 2;
217  t('-400', '-450.001', 100);
218  Decimal.rounding = 3;
219  t('-500', '-450.001', 100);
220  Decimal.rounding = 4;
221  t('-500', '-450.001', 100);
222  Decimal.rounding = 5;
223  t('-500', '-450.001', 100);
224  Decimal.rounding = 6;
225  t('-500', '-450.001', 100);
226  Decimal.rounding = 7;
227  t('-500', '-450.001', 100);
228  Decimal.rounding = 8;
229  t('-500', '-450.001', 100);
230});
231