1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.hipparchus.dfp;
24
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import org.hipparchus.CalculusFieldElementAbstractTest;
29 import org.hipparchus.util.Binary64Field;
30 import org.hipparchus.util.FastMath;
31 import org.hipparchus.util.Precision;
32 import org.junit.After;
33 import org.junit.Assert;
34 import org.junit.Before;
35 import org.junit.Test;
36
37 public class DfpTest extends CalculusFieldElementAbstractTest<Dfp> {
38
39 @Override
40 protected Dfp build(final double x) {
41 return field.newDfp(x);
42 }
43
44 private DfpField field;
45 private Dfp pinf;
46 private Dfp ninf;
47 private Dfp nan;
48 private Dfp snan;
49 private Dfp qnan;
50
51 @Before
52 public void setUp() {
53
54 field = new DfpField(20);
55 pinf = field.newDfp("1").divide(field.newDfp("0"));
56 ninf = field.newDfp("-1").divide(field.newDfp("0"));
57 nan = field.newDfp("0").divide(field.newDfp("0"));
58 snan = field.newDfp((byte)1, Dfp.SNAN);
59 qnan = field.newDfp((byte)1, Dfp.QNAN);
60 ninf.getField().clearIEEEFlags();
61 }
62
63 @After
64 public void tearDown() {
65 field = null;
66 pinf = null;
67 ninf = null;
68 nan = null;
69 snan = null;
70 qnan = null;
71 }
72
73
74
75
76 private void test(Dfp x, Dfp y, int flags, String desc)
77 {
78 boolean b = x.equals(y);
79
80 if (!x.equals(y) && !x.unequal(y))
81 b = (x.toString().equals(y.toString()));
82
83 if (x.equals(field.newDfp("0")))
84 b = (b && (x.toString().equals(y.toString())));
85
86 b = (b && x.getField().getIEEEFlags() == flags);
87
88 if (!b)
89 Assert.assertTrue("assersion failed "+desc+" x = "+x.toString()+" flags = "+x.getField().getIEEEFlags(), b);
90
91 x.getField().clearIEEEFlags();
92 }
93
94 @Test
95 public void testByteConstructor() {
96 Assert.assertEquals("0.", new Dfp(field, (byte) 0).toString());
97 Assert.assertEquals("1.", new Dfp(field, (byte) 1).toString());
98 Assert.assertEquals("-1.", new Dfp(field, (byte) -1).toString());
99 Assert.assertEquals("-128.", new Dfp(field, Byte.MIN_VALUE).toString());
100 Assert.assertEquals("127.", new Dfp(field, Byte.MAX_VALUE).toString());
101 }
102
103 @Test
104 public void testIntConstructor() {
105 Assert.assertEquals("0.", new Dfp(field, 0).toString());
106 Assert.assertEquals("1.", new Dfp(field, 1).toString());
107 Assert.assertEquals("-1.", new Dfp(field, -1).toString());
108 Assert.assertEquals("1234567890.", new Dfp(field, 1234567890).toString());
109 Assert.assertEquals("-1234567890.", new Dfp(field, -1234567890).toString());
110 Assert.assertEquals("-2147483648.", new Dfp(field, Integer.MIN_VALUE).toString());
111 Assert.assertEquals("2147483647.", new Dfp(field, Integer.MAX_VALUE).toString());
112 }
113
114 @Test
115 public void testLongConstructor() {
116 Assert.assertEquals("0.", new Dfp(field, 0l).toString());
117 Assert.assertEquals("1.", new Dfp(field, 1l).toString());
118 Assert.assertEquals("-1.", new Dfp(field, -1l).toString());
119 Assert.assertEquals("1234567890.", new Dfp(field, 1234567890l).toString());
120 Assert.assertEquals("-1234567890.", new Dfp(field, -1234567890l).toString());
121 Assert.assertEquals("-9223372036854775808.", new Dfp(field, Long.MIN_VALUE).toString());
122 Assert.assertEquals("9223372036854775807.", new Dfp(field, Long.MAX_VALUE).toString());
123 }
124
125
126
127
128 @Test
129 public void testAdd()
130 {
131 test(field.newDfp("1").add(field.newDfp("1")),
132 field.newDfp("2"),
133 0, "Add #1");
134
135 test(field.newDfp("1").add(field.newDfp("-1")),
136 field.newDfp("0"),
137 0, "Add #2");
138
139 test(field.newDfp("-1").add(field.newDfp("1")),
140 field.newDfp("0"),
141 0, "Add #3");
142
143 test(field.newDfp("-1").add(field.newDfp("-1")),
144 field.newDfp("-2"),
145 0, "Add #4");
146
147
148
149 test(field.newDfp("1").add(field.newDfp("1e-16")),
150 field.newDfp("1.0000000000000001"),
151 0, "Add #5");
152
153 test(field.newDfp("1").add(field.newDfp("1e-17")),
154 field.newDfp("1"),
155 DfpField.FLAG_INEXACT, "Add #6");
156
157 test(field.newDfp("0.90999999999999999999").add(field.newDfp("0.1")),
158 field.newDfp("1.01"),
159 DfpField.FLAG_INEXACT, "Add #7");
160
161 test(field.newDfp(".10000000000000005000").add(field.newDfp(".9")),
162 field.newDfp("1."),
163 DfpField.FLAG_INEXACT, "Add #8");
164
165 test(field.newDfp(".10000000000000015000").add(field.newDfp(".9")),
166 field.newDfp("1.0000000000000002"),
167 DfpField.FLAG_INEXACT, "Add #9");
168
169 test(field.newDfp(".10000000000000014999").add(field.newDfp(".9")),
170 field.newDfp("1.0000000000000001"),
171 DfpField.FLAG_INEXACT, "Add #10");
172
173 test(field.newDfp(".10000000000000015001").add(field.newDfp(".9")),
174 field.newDfp("1.0000000000000002"),
175 DfpField.FLAG_INEXACT, "Add #11");
176
177 test(field.newDfp(".11111111111111111111").add(field.newDfp("11.1111111111111111")),
178 field.newDfp("11.22222222222222222222"),
179 DfpField.FLAG_INEXACT, "Add #12");
180
181 test(field.newDfp(".11111111111111111111").add(field.newDfp("1111111111111111.1111")),
182 field.newDfp("1111111111111111.2222"),
183 DfpField.FLAG_INEXACT, "Add #13");
184
185 test(field.newDfp(".11111111111111111111").add(field.newDfp("11111111111111111111")),
186 field.newDfp("11111111111111111111"),
187 DfpField.FLAG_INEXACT, "Add #14");
188
189 test(field.newDfp("9.9999999999999999999e131071").add(field.newDfp("-1e131052")),
190 field.newDfp("9.9999999999999999998e131071"),
191 0, "Add #15");
192
193 test(field.newDfp("9.9999999999999999999e131071").add(field.newDfp("1e131052")),
194 pinf,
195 DfpField.FLAG_OVERFLOW, "Add #16");
196
197 test(field.newDfp("-9.9999999999999999999e131071").add(field.newDfp("-1e131052")),
198 ninf,
199 DfpField.FLAG_OVERFLOW, "Add #17");
200
201 test(field.newDfp("-9.9999999999999999999e131071").add(field.newDfp("1e131052")),
202 field.newDfp("-9.9999999999999999998e131071"),
203 0, "Add #18");
204
205 test(field.newDfp("1e-131072").add(field.newDfp("1e-131072")),
206 field.newDfp("2e-131072"),
207 0, "Add #19");
208
209 test(field.newDfp("1.0000000000000001e-131057").add(field.newDfp("-1e-131057")),
210 field.newDfp("1e-131073"),
211 DfpField.FLAG_UNDERFLOW, "Add #20");
212
213 test(field.newDfp("1.1e-131072").add(field.newDfp("-1e-131072")),
214 field.newDfp("1e-131073"),
215 DfpField.FLAG_UNDERFLOW, "Add #21");
216
217 test(field.newDfp("1.0000000000000001e-131072").add(field.newDfp("-1e-131072")),
218 field.newDfp("1e-131088"),
219 DfpField.FLAG_UNDERFLOW, "Add #22");
220
221 test(field.newDfp("1.0000000000000001e-131078").add(field.newDfp("-1e-131078")),
222 field.newDfp("0"),
223 DfpField.FLAG_UNDERFLOW, "Add #23");
224
225 test(field.newDfp("1.0").add(field.newDfp("-1e-20")),
226 field.newDfp("0.99999999999999999999"),
227 0, "Add #23.1");
228
229 test(field.newDfp("-0.99999999999999999999").add(field.newDfp("1")),
230 field.newDfp("0.00000000000000000001"),
231 0, "Add #23.2");
232
233 test(field.newDfp("1").add(field.newDfp("0")),
234 field.newDfp("1"),
235 0, "Add #24");
236
237 test(field.newDfp("0").add(field.newDfp("0")),
238 field.newDfp("0"),
239 0, "Add #25");
240
241 test(field.newDfp("-0").add(field.newDfp("0")),
242 field.newDfp("0"),
243 0, "Add #26");
244
245 test(field.newDfp("0").add(field.newDfp("-0")),
246 field.newDfp("0"),
247 0, "Add #27");
248
249 test(field.newDfp("-0").add(field.newDfp("-0")),
250 field.newDfp("-0"),
251 0, "Add #28");
252
253 test(field.newDfp("1e-20").add(field.newDfp("0")),
254 field.newDfp("1e-20"),
255 0, "Add #29");
256
257 test(field.newDfp("1e-40").add(field.newDfp("0")),
258 field.newDfp("1e-40"),
259 0, "Add #30");
260
261 test(pinf.add(ninf),
262 nan,
263 DfpField.FLAG_INVALID, "Add #31");
264
265 test(ninf.add(pinf),
266 nan,
267 DfpField.FLAG_INVALID, "Add #32");
268
269 test(ninf.add(ninf),
270 ninf,
271 0, "Add #33");
272
273 test(pinf.add(pinf),
274 pinf,
275 0, "Add #34");
276
277 test(pinf.add(field.newDfp("0")),
278 pinf,
279 0, "Add #35");
280
281 test(pinf.add(field.newDfp("-1e131071")),
282 pinf,
283 0, "Add #36");
284
285 test(pinf.add(field.newDfp("1e131071")),
286 pinf,
287 0, "Add #37");
288
289 test(field.newDfp("0").add(pinf),
290 pinf,
291 0, "Add #38");
292
293 test(field.newDfp("-1e131071").add(pinf),
294 pinf,
295 0, "Add #39");
296
297 test(field.newDfp("1e131071").add(pinf),
298 pinf,
299 0, "Add #40");
300
301 test(ninf.add(field.newDfp("0")),
302 ninf,
303 0, "Add #41");
304
305 test(ninf.add(field.newDfp("-1e131071")),
306 ninf,
307 0, "Add #42");
308
309 test(ninf.add(field.newDfp("1e131071")),
310 ninf,
311 0, "Add #43");
312
313 test(field.newDfp("0").add(ninf),
314 ninf,
315 0, "Add #44");
316
317 test(field.newDfp("-1e131071").add(ninf),
318 ninf,
319 0, "Add #45");
320
321 test(field.newDfp("1e131071").add(ninf),
322 ninf,
323 0, "Add #46");
324
325 test(field.newDfp("9.9999999999999999999e131071").add(field.newDfp("5e131051")),
326 pinf,
327 DfpField.FLAG_OVERFLOW, "Add #47");
328
329 test(field.newDfp("9.9999999999999999999e131071").add(field.newDfp("4.9999999999999999999e131051")),
330 field.newDfp("9.9999999999999999999e131071"),
331 DfpField.FLAG_INEXACT, "Add #48");
332
333 test(nan.add(field.newDfp("1")),
334 nan,
335 0, "Add #49");
336
337 test(field.newDfp("1").add(nan),
338 nan,
339 0, "Add #50");
340
341 test(field.newDfp("12345678123456781234").add(field.newDfp("0.12345678123456781234")),
342 field.newDfp("12345678123456781234"),
343 DfpField.FLAG_INEXACT, "Add #51");
344
345 test(field.newDfp("12345678123456781234").add(field.newDfp("123.45678123456781234")),
346 field.newDfp("12345678123456781357"),
347 DfpField.FLAG_INEXACT, "Add #52");
348
349 test(field.newDfp("123.45678123456781234").add(field.newDfp("12345678123456781234")),
350 field.newDfp("12345678123456781357"),
351 DfpField.FLAG_INEXACT, "Add #53");
352
353 test(field.newDfp("12345678123456781234").add(field.newDfp(".00001234567812345678")),
354 field.newDfp("12345678123456781234"),
355 DfpField.FLAG_INEXACT, "Add #54");
356
357 test(field.newDfp("12345678123456781234").add(field.newDfp(".00000000123456781234")),
358 field.newDfp("12345678123456781234"),
359 DfpField.FLAG_INEXACT, "Add #55");
360
361 test(field.newDfp("-0").add(field.newDfp("-0")),
362 field.newDfp("-0"),
363 0, "Add #56");
364
365 test(field.newDfp("0").add(field.newDfp("-0")),
366 field.newDfp("0"),
367 0, "Add #57");
368
369 test(field.newDfp("-0").add(field.newDfp("0")),
370 field.newDfp("0"),
371 0, "Add #58");
372
373 test(field.newDfp("0").add(field.newDfp("0")),
374 field.newDfp("0"),
375 0, "Add #59");
376 }
377
378
379
380
381
382
383 private void cmptst(Dfp a, Dfp b, String op, boolean result, double num)
384 {
385 if (op == "equal")
386 if (a.equals(b) != result)
387 Assert.fail("assersion failed. "+op+" compare #"+num);
388
389 if (op == "unequal")
390 if (a.unequal(b) != result)
391 Assert.fail("assersion failed. "+op+" compare #"+num);
392
393 if (op == "lessThan")
394 if (a.lessThan(b) != result)
395 Assert.fail("assersion failed. "+op+" compare #"+num);
396
397 if (op == "greaterThan")
398 if (a.greaterThan(b) != result)
399 Assert.fail("assersion failed. "+op+" compare #"+num);
400 }
401
402 @Test
403 public void testCompare()
404 {
405
406
407 field.clearIEEEFlags();
408
409 cmptst(field.newDfp("0"), field.newDfp("0"), "equal", true, 1);
410 cmptst(field.newDfp("0"), field.newDfp("-0"), "equal", true, 2);
411 cmptst(field.newDfp("-0"), field.newDfp("-0"), "equal", true, 3);
412 cmptst(field.newDfp("-0"), field.newDfp("0"), "equal", true, 4);
413
414
415
416 cmptst(field.newDfp("0"), field.newDfp("1"), "equal", false, 5);
417 cmptst(field.newDfp("1"), field.newDfp("0"), "equal", false, 6);
418 cmptst(field.newDfp("-1"), field.newDfp("0"), "equal", false, 7);
419 cmptst(field.newDfp("0"), field.newDfp("-1"), "equal", false, 8);
420 cmptst(field.newDfp("0"), field.newDfp("1e-131072"), "equal", false, 9);
421
422 if (field.getIEEEFlags() != 0)
423 Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags());
424
425 cmptst(field.newDfp("0"), field.newDfp("1e-131078"), "equal", false, 10);
426
427
428 if (field.getIEEEFlags() != DfpField.FLAG_UNDERFLOW)
429 Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags());
430
431 field.clearIEEEFlags();
432
433 cmptst(field.newDfp("0"), field.newDfp("1e+131071"), "equal", false, 11);
434
435
436
437 cmptst(field.newDfp("0"), pinf, "equal", false, 12);
438 cmptst(field.newDfp("0"), ninf, "equal", false, 13);
439 cmptst(field.newDfp("-0"), pinf, "equal", false, 14);
440 cmptst(field.newDfp("-0"), ninf, "equal", false, 15);
441 cmptst(pinf, field.newDfp("0"), "equal", false, 16);
442 cmptst(ninf, field.newDfp("0"), "equal", false, 17);
443 cmptst(pinf, field.newDfp("-0"), "equal", false, 18);
444 cmptst(ninf, field.newDfp("-0"), "equal", false, 19);
445 cmptst(ninf, pinf, "equal", false, 19.10);
446 cmptst(pinf, ninf, "equal", false, 19.11);
447 cmptst(pinf, pinf, "equal", true, 19.12);
448 cmptst(ninf, ninf, "equal", true, 19.13);
449
450
451 cmptst(field.newDfp("1"), field.newDfp("1"), "equal", true, 20);
452 cmptst(field.newDfp("1"), field.newDfp("-1"), "equal", false, 21);
453 cmptst(field.newDfp("-1"), field.newDfp("-1"), "equal", true, 22);
454 cmptst(field.newDfp("1"), field.newDfp("1.0000000000000001"), "equal", false, 23);
455
456
457
458 cmptst(field.newDfp("1e20"), field.newDfp("1.0000000000000001"), "equal", false, 24);
459 if (field.getIEEEFlags() != 0)
460 Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags());
461
462 cmptst(field.newDfp("0.000001"), field.newDfp("1e-6"), "equal", true, 25);
463
464
465
466 cmptst(snan, snan, "equal", false, 27);
467 cmptst(qnan, qnan, "equal", false, 28);
468 cmptst(snan, qnan, "equal", false, 29);
469 cmptst(qnan, snan, "equal", false, 30);
470 cmptst(qnan, field.newDfp("0"), "equal", false, 31);
471 cmptst(snan, field.newDfp("0"), "equal", false, 32);
472 cmptst(field.newDfp("0"), snan, "equal", false, 33);
473 cmptst(field.newDfp("0"), qnan, "equal", false, 34);
474 cmptst(qnan, pinf, "equal", false, 35);
475 cmptst(snan, pinf, "equal", false, 36);
476 cmptst(pinf, snan, "equal", false, 37);
477 cmptst(pinf, qnan, "equal", false, 38);
478 cmptst(qnan, ninf, "equal", false, 39);
479 cmptst(snan, ninf, "equal", false, 40);
480 cmptst(ninf, snan, "equal", false, 41);
481 cmptst(ninf, qnan, "equal", false, 42);
482 cmptst(qnan, field.newDfp("-1"), "equal", false, 43);
483 cmptst(snan, field.newDfp("-1"), "equal", false, 44);
484 cmptst(field.newDfp("-1"), snan, "equal", false, 45);
485 cmptst(field.newDfp("-1"), qnan, "equal", false, 46);
486 cmptst(qnan, field.newDfp("1"), "equal", false, 47);
487 cmptst(snan, field.newDfp("1"), "equal", false, 48);
488 cmptst(field.newDfp("1"), snan, "equal", false, 49);
489 cmptst(field.newDfp("1"), qnan, "equal", false, 50);
490 cmptst(snan.negate(), snan, "equal", false, 51);
491 cmptst(qnan.negate(), qnan, "equal", false, 52);
492
493
494
495
496
497 cmptst(field.newDfp("0"), field.newDfp("0"), "unequal", false, 1);
498 cmptst(field.newDfp("0"), field.newDfp("-0"), "unequal", false, 2);
499 cmptst(field.newDfp("-0"), field.newDfp("-0"), "unequal", false, 3);
500 cmptst(field.newDfp("-0"), field.newDfp("0"), "unequal", false, 4);
501
502
503
504 cmptst(field.newDfp("0"), field.newDfp("1"), "unequal", true, 5);
505 cmptst(field.newDfp("1"), field.newDfp("0"), "unequal", true, 6);
506 cmptst(field.newDfp("-1"), field.newDfp("0"), "unequal", true, 7);
507 cmptst(field.newDfp("0"), field.newDfp("-1"), "unequal", true, 8);
508 cmptst(field.newDfp("0"), field.newDfp("1e-131072"), "unequal", true, 9);
509
510 if (field.getIEEEFlags() != 0)
511 Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags());
512
513 cmptst(field.newDfp("0"), field.newDfp("1e-131078"), "unequal", true, 10);
514
515
516 if (field.getIEEEFlags() != DfpField.FLAG_UNDERFLOW)
517 Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags());
518
519 field.clearIEEEFlags();
520
521 cmptst(field.newDfp("0"), field.newDfp("1e+131071"), "unequal", true, 11);
522
523
524
525 cmptst(field.newDfp("0"), pinf, "unequal", true, 12);
526 cmptst(field.newDfp("0"), ninf, "unequal", true, 13);
527 cmptst(field.newDfp("-0"), pinf, "unequal", true, 14);
528 cmptst(field.newDfp("-0"), ninf, "unequal", true, 15);
529 cmptst(pinf, field.newDfp("0"), "unequal", true, 16);
530 cmptst(ninf, field.newDfp("0"), "unequal", true, 17);
531 cmptst(pinf, field.newDfp("-0"), "unequal", true, 18);
532 cmptst(ninf, field.newDfp("-0"), "unequal", true, 19);
533 cmptst(ninf, pinf, "unequal", true, 19.10);
534 cmptst(pinf, ninf, "unequal", true, 19.11);
535 cmptst(pinf, pinf, "unequal", false, 19.12);
536 cmptst(ninf, ninf, "unequal", false, 19.13);
537
538
539 cmptst(field.newDfp("1"), field.newDfp("1"), "unequal", false, 20);
540 cmptst(field.newDfp("1"), field.newDfp("-1"), "unequal", true, 21);
541 cmptst(field.newDfp("-1"), field.newDfp("-1"), "unequal", false, 22);
542 cmptst(field.newDfp("1"), field.newDfp("1.0000000000000001"), "unequal", true, 23);
543
544
545
546 cmptst(field.newDfp("1e20"), field.newDfp("1.0000000000000001"), "unequal", true, 24);
547 if (field.getIEEEFlags() != 0)
548 Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags());
549
550 cmptst(field.newDfp("0.000001"), field.newDfp("1e-6"), "unequal", false, 25);
551
552
553
554 cmptst(snan, snan, "unequal", false, 27);
555 cmptst(qnan, qnan, "unequal", false, 28);
556 cmptst(snan, qnan, "unequal", false, 29);
557 cmptst(qnan, snan, "unequal", false, 30);
558 cmptst(qnan, field.newDfp("0"), "unequal", false, 31);
559 cmptst(snan, field.newDfp("0"), "unequal", false, 32);
560 cmptst(field.newDfp("0"), snan, "unequal", false, 33);
561 cmptst(field.newDfp("0"), qnan, "unequal", false, 34);
562 cmptst(qnan, pinf, "unequal", false, 35);
563 cmptst(snan, pinf, "unequal", false, 36);
564 cmptst(pinf, snan, "unequal", false, 37);
565 cmptst(pinf, qnan, "unequal", false, 38);
566 cmptst(qnan, ninf, "unequal", false, 39);
567 cmptst(snan, ninf, "unequal", false, 40);
568 cmptst(ninf, snan, "unequal", false, 41);
569 cmptst(ninf, qnan, "unequal", false, 42);
570 cmptst(qnan, field.newDfp("-1"), "unequal", false, 43);
571 cmptst(snan, field.newDfp("-1"), "unequal", false, 44);
572 cmptst(field.newDfp("-1"), snan, "unequal", false, 45);
573 cmptst(field.newDfp("-1"), qnan, "unequal", false, 46);
574 cmptst(qnan, field.newDfp("1"), "unequal", false, 47);
575 cmptst(snan, field.newDfp("1"), "unequal", false, 48);
576 cmptst(field.newDfp("1"), snan, "unequal", false, 49);
577 cmptst(field.newDfp("1"), qnan, "unequal", false, 50);
578 cmptst(snan.negate(), snan, "unequal", false, 51);
579 cmptst(qnan.negate(), qnan, "unequal", false, 52);
580
581 if (field.getIEEEFlags() != 0)
582 Assert.fail("assersion failed. compare unequal flags = "+field.getIEEEFlags());
583
584
585
586
587
588 cmptst(field.newDfp("0"), field.newDfp("0"), "lessThan", false, 1);
589 cmptst(field.newDfp("0"), field.newDfp("-0"), "lessThan", false, 2);
590 cmptst(field.newDfp("-0"), field.newDfp("-0"), "lessThan", false, 3);
591 cmptst(field.newDfp("-0"), field.newDfp("0"), "lessThan", false, 4);
592
593
594
595 cmptst(field.newDfp("0"), field.newDfp("1"), "lessThan", true, 5);
596 cmptst(field.newDfp("1"), field.newDfp("0"), "lessThan", false, 6);
597 cmptst(field.newDfp("-1"), field.newDfp("0"), "lessThan", true, 7);
598 cmptst(field.newDfp("0"), field.newDfp("-1"), "lessThan", false, 8);
599 cmptst(field.newDfp("0"), field.newDfp("1e-131072"), "lessThan", true, 9);
600
601 if (field.getIEEEFlags() != 0)
602 Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags());
603
604 cmptst(field.newDfp("0"), field.newDfp("1e-131078"), "lessThan", true, 10);
605
606
607 if (field.getIEEEFlags() != DfpField.FLAG_UNDERFLOW)
608 Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags());
609 field.clearIEEEFlags();
610
611 cmptst(field.newDfp("0"), field.newDfp("1e+131071"), "lessThan", true, 11);
612
613
614
615 cmptst(field.newDfp("0"), pinf, "lessThan", true, 12);
616 cmptst(field.newDfp("0"), ninf, "lessThan", false, 13);
617 cmptst(field.newDfp("-0"), pinf, "lessThan", true, 14);
618 cmptst(field.newDfp("-0"), ninf, "lessThan", false, 15);
619 cmptst(pinf, field.newDfp("0"), "lessThan", false, 16);
620 cmptst(ninf, field.newDfp("0"), "lessThan", true, 17);
621 cmptst(pinf, field.newDfp("-0"), "lessThan", false, 18);
622 cmptst(ninf, field.newDfp("-0"), "lessThan", true, 19);
623 cmptst(ninf, pinf, "lessThan", true, 19.10);
624 cmptst(pinf, ninf, "lessThan", false, 19.11);
625 cmptst(pinf, pinf, "lessThan", false, 19.12);
626 cmptst(ninf, ninf, "lessThan", false, 19.13);
627
628
629 cmptst(field.newDfp("1"), field.newDfp("1"), "lessThan", false, 20);
630 cmptst(field.newDfp("1"), field.newDfp("-1"), "lessThan", false, 21);
631 cmptst(field.newDfp("-1"), field.newDfp("-1"), "lessThan", false, 22);
632 cmptst(field.newDfp("1"), field.newDfp("1.0000000000000001"), "lessThan", true, 23);
633
634
635
636 cmptst(field.newDfp("1e20"), field.newDfp("1.0000000000000001"), "lessThan", false, 24);
637 if (field.getIEEEFlags() != 0)
638 Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags());
639
640 cmptst(field.newDfp("0.000001"), field.newDfp("1e-6"), "lessThan", false, 25);
641
642
643 cmptst(snan, snan, "lessThan", false, 27);
644 cmptst(qnan, qnan, "lessThan", false, 28);
645 cmptst(snan, qnan, "lessThan", false, 29);
646 cmptst(qnan, snan, "lessThan", false, 30);
647 cmptst(qnan, field.newDfp("0"), "lessThan", false, 31);
648 cmptst(snan, field.newDfp("0"), "lessThan", false, 32);
649 cmptst(field.newDfp("0"), snan, "lessThan", false, 33);
650 cmptst(field.newDfp("0"), qnan, "lessThan", false, 34);
651 cmptst(qnan, pinf, "lessThan", false, 35);
652 cmptst(snan, pinf, "lessThan", false, 36);
653 cmptst(pinf, snan, "lessThan", false, 37);
654 cmptst(pinf, qnan, "lessThan", false, 38);
655 cmptst(qnan, ninf, "lessThan", false, 39);
656 cmptst(snan, ninf, "lessThan", false, 40);
657 cmptst(ninf, snan, "lessThan", false, 41);
658 cmptst(ninf, qnan, "lessThan", false, 42);
659 cmptst(qnan, field.newDfp("-1"), "lessThan", false, 43);
660 cmptst(snan, field.newDfp("-1"), "lessThan", false, 44);
661 cmptst(field.newDfp("-1"), snan, "lessThan", false, 45);
662 cmptst(field.newDfp("-1"), qnan, "lessThan", false, 46);
663 cmptst(qnan, field.newDfp("1"), "lessThan", false, 47);
664 cmptst(snan, field.newDfp("1"), "lessThan", false, 48);
665 cmptst(field.newDfp("1"), snan, "lessThan", false, 49);
666 cmptst(field.newDfp("1"), qnan, "lessThan", false, 50);
667 cmptst(snan.negate(), snan, "lessThan", false, 51);
668 cmptst(qnan.negate(), qnan, "lessThan", false, 52);
669
670
671 if (field.getIEEEFlags() != DfpField.FLAG_INVALID)
672 Assert.fail("assersion failed. compare lessThan flags = "+field.getIEEEFlags());
673 field.clearIEEEFlags();
674
675
676
677
678
679 cmptst(field.newDfp("0"), field.newDfp("0"), "greaterThan", false, 1);
680 cmptst(field.newDfp("0"), field.newDfp("-0"), "greaterThan", false, 2);
681 cmptst(field.newDfp("-0"), field.newDfp("-0"), "greaterThan", false, 3);
682 cmptst(field.newDfp("-0"), field.newDfp("0"), "greaterThan", false, 4);
683
684
685
686 cmptst(field.newDfp("0"), field.newDfp("1"), "greaterThan", false, 5);
687 cmptst(field.newDfp("1"), field.newDfp("0"), "greaterThan", true, 6);
688 cmptst(field.newDfp("-1"), field.newDfp("0"), "greaterThan", false, 7);
689 cmptst(field.newDfp("0"), field.newDfp("-1"), "greaterThan", true, 8);
690 cmptst(field.newDfp("0"), field.newDfp("1e-131072"), "greaterThan", false, 9);
691
692 if (field.getIEEEFlags() != 0)
693 Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags());
694
695 cmptst(field.newDfp("0"), field.newDfp("1e-131078"), "greaterThan", false, 10);
696
697
698 if (field.getIEEEFlags() != DfpField.FLAG_UNDERFLOW)
699 Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags());
700 field.clearIEEEFlags();
701
702 cmptst(field.newDfp("0"), field.newDfp("1e+131071"), "greaterThan", false, 11);
703
704
705
706 cmptst(field.newDfp("0"), pinf, "greaterThan", false, 12);
707 cmptst(field.newDfp("0"), ninf, "greaterThan", true, 13);
708 cmptst(field.newDfp("-0"), pinf, "greaterThan", false, 14);
709 cmptst(field.newDfp("-0"), ninf, "greaterThan", true, 15);
710 cmptst(pinf, field.newDfp("0"), "greaterThan", true, 16);
711 cmptst(ninf, field.newDfp("0"), "greaterThan", false, 17);
712 cmptst(pinf, field.newDfp("-0"), "greaterThan", true, 18);
713 cmptst(ninf, field.newDfp("-0"), "greaterThan", false, 19);
714 cmptst(ninf, pinf, "greaterThan", false, 19.10);
715 cmptst(pinf, ninf, "greaterThan", true, 19.11);
716 cmptst(pinf, pinf, "greaterThan", false, 19.12);
717 cmptst(ninf, ninf, "greaterThan", false, 19.13);
718
719
720 cmptst(field.newDfp("1"), field.newDfp("1"), "greaterThan", false, 20);
721 cmptst(field.newDfp("1"), field.newDfp("-1"), "greaterThan", true, 21);
722 cmptst(field.newDfp("-1"), field.newDfp("-1"), "greaterThan", false, 22);
723 cmptst(field.newDfp("1"), field.newDfp("1.0000000000000001"), "greaterThan", false, 23);
724
725
726
727 cmptst(field.newDfp("1e20"), field.newDfp("1.0000000000000001"), "greaterThan", true, 24);
728 if (field.getIEEEFlags() != 0)
729 Assert.fail("assersion failed. compare flags = "+field.getIEEEFlags());
730
731 cmptst(field.newDfp("0.000001"), field.newDfp("1e-6"), "greaterThan", false, 25);
732
733
734 cmptst(snan, snan, "greaterThan", false, 27);
735 cmptst(qnan, qnan, "greaterThan", false, 28);
736 cmptst(snan, qnan, "greaterThan", false, 29);
737 cmptst(qnan, snan, "greaterThan", false, 30);
738 cmptst(qnan, field.newDfp("0"), "greaterThan", false, 31);
739 cmptst(snan, field.newDfp("0"), "greaterThan", false, 32);
740 cmptst(field.newDfp("0"), snan, "greaterThan", false, 33);
741 cmptst(field.newDfp("0"), qnan, "greaterThan", false, 34);
742 cmptst(qnan, pinf, "greaterThan", false, 35);
743 cmptst(snan, pinf, "greaterThan", false, 36);
744 cmptst(pinf, snan, "greaterThan", false, 37);
745 cmptst(pinf, qnan, "greaterThan", false, 38);
746 cmptst(qnan, ninf, "greaterThan", false, 39);
747 cmptst(snan, ninf, "greaterThan", false, 40);
748 cmptst(ninf, snan, "greaterThan", false, 41);
749 cmptst(ninf, qnan, "greaterThan", false, 42);
750 cmptst(qnan, field.newDfp("-1"), "greaterThan", false, 43);
751 cmptst(snan, field.newDfp("-1"), "greaterThan", false, 44);
752 cmptst(field.newDfp("-1"), snan, "greaterThan", false, 45);
753 cmptst(field.newDfp("-1"), qnan, "greaterThan", false, 46);
754 cmptst(qnan, field.newDfp("1"), "greaterThan", false, 47);
755 cmptst(snan, field.newDfp("1"), "greaterThan", false, 48);
756 cmptst(field.newDfp("1"), snan, "greaterThan", false, 49);
757 cmptst(field.newDfp("1"), qnan, "greaterThan", false, 50);
758 cmptst(snan.negate(), snan, "greaterThan", false, 51);
759 cmptst(qnan.negate(), qnan, "greaterThan", false, 52);
760
761
762 if (field.getIEEEFlags() != DfpField.FLAG_INVALID)
763 Assert.fail("assersion failed. compare greaterThan flags = "+field.getIEEEFlags());
764 field.clearIEEEFlags();
765 }
766
767
768
769
770 @Test
771 public void testMultiply()
772 {
773 test(field.newDfp("1").multiply(field.newDfp("1")),
774 field.newDfp("1"),
775 0, "Multiply #1");
776
777 test(field.newDfp("1").multiply(1),
778 field.newDfp("1"),
779 0, "Multiply #2");
780
781 test(field.newDfp("-1").multiply(field.newDfp("1")),
782 field.newDfp("-1"),
783 0, "Multiply #3");
784
785 test(field.newDfp("-1").multiply(1),
786 field.newDfp("-1"),
787 0, "Multiply #4");
788
789
790 test(field.newDfp("2").multiply(field.newDfp("3")),
791 field.newDfp("6"),
792 0, "Multiply #5");
793
794 test(field.newDfp("2").multiply(3),
795 field.newDfp("6"),
796 0, "Multiply #6");
797
798 test(field.newDfp("-2").multiply(field.newDfp("3")),
799 field.newDfp("-6"),
800 0, "Multiply #7");
801
802 test(field.newDfp("-2").multiply(3),
803 field.newDfp("-6"),
804 0, "Multiply #8");
805
806 test(field.newDfp("2").multiply(field.newDfp("-3")),
807 field.newDfp("-6"),
808 0, "Multiply #9");
809
810 test(field.newDfp("-2").multiply(field.newDfp("-3")),
811 field.newDfp("6"),
812 0, "Multiply #10");
813
814
815
816 test(field.newDfp("-2").multiply(field.newDfp("0")),
817 field.newDfp("-0"),
818 0, "Multiply #11");
819
820 test(field.newDfp("-2").multiply(0),
821 field.newDfp("-0"),
822 0, "Multiply #12");
823
824 test(field.newDfp("2").multiply(field.newDfp("0")),
825 field.newDfp("0"),
826 0, "Multiply #13");
827
828 test(field.newDfp("2").multiply(0),
829 field.newDfp("0"),
830 0, "Multiply #14");
831
832 test(field.newDfp("2").multiply(pinf),
833 pinf,
834 0, "Multiply #15");
835
836 test(field.newDfp("2").multiply(ninf),
837 ninf,
838 0, "Multiply #16");
839
840 test(field.newDfp("-2").multiply(pinf),
841 ninf,
842 0, "Multiply #17");
843
844 test(field.newDfp("-2").multiply(ninf),
845 pinf,
846 0, "Multiply #18");
847
848 test(ninf.multiply(field.newDfp("-2")),
849 pinf,
850 0, "Multiply #18.1");
851
852 test(field.newDfp("5e131071").multiply(2),
853 pinf,
854 DfpField.FLAG_OVERFLOW, "Multiply #19");
855
856 test(field.newDfp("5e131071").multiply(field.newDfp("1.999999999999999")),
857 field.newDfp("9.9999999999999950000e131071"),
858 0, "Multiply #20");
859
860 test(field.newDfp("-5e131071").multiply(2),
861 ninf,
862 DfpField.FLAG_OVERFLOW, "Multiply #22");
863
864 test(field.newDfp("-5e131071").multiply(field.newDfp("1.999999999999999")),
865 field.newDfp("-9.9999999999999950000e131071"),
866 0, "Multiply #23");
867
868 test(field.newDfp("1e-65539").multiply(field.newDfp("1e-65539")),
869 field.newDfp("1e-131078"),
870 DfpField.FLAG_UNDERFLOW, "Multiply #24");
871
872 test(field.newDfp("1").multiply(nan),
873 nan,
874 0, "Multiply #25");
875
876 test(nan.multiply(field.newDfp("1")),
877 nan,
878 0, "Multiply #26");
879
880 test(nan.multiply(pinf),
881 nan,
882 0, "Multiply #27");
883
884 test(pinf.multiply(nan),
885 nan,
886 0, "Multiply #27");
887
888 test(pinf.multiply(field.newDfp("0")),
889 nan,
890 DfpField.FLAG_INVALID, "Multiply #28");
891
892 test(field.newDfp("0").multiply(pinf),
893 nan,
894 DfpField.FLAG_INVALID, "Multiply #29");
895
896 test(pinf.multiply(pinf),
897 pinf,
898 0, "Multiply #30");
899
900 test(ninf.multiply(pinf),
901 ninf,
902 0, "Multiply #31");
903
904 test(pinf.multiply(ninf),
905 ninf,
906 0, "Multiply #32");
907
908 test(ninf.multiply(ninf),
909 pinf,
910 0, "Multiply #33");
911
912 test(pinf.multiply(1),
913 pinf,
914 0, "Multiply #34");
915
916 test(pinf.multiply(0),
917 nan,
918 DfpField.FLAG_INVALID, "Multiply #35");
919
920 test(nan.multiply(1),
921 nan,
922 0, "Multiply #36");
923
924 test(field.newDfp("1").multiply(10000),
925 field.newDfp("10000"),
926 0, "Multiply #37");
927
928 test(field.newDfp("2").multiply(1000000),
929 field.newDfp("2000000"),
930 0, "Multiply #38");
931
932 test(field.newDfp("1").multiply(-1),
933 field.newDfp("-1"),
934 0, "Multiply #39");
935 }
936
937 @Test
938 public void testDivide()
939 {
940 test(field.newDfp("1").divide(nan),
941 nan,
942 0, "Divide #1");
943
944 test(nan.divide(field.newDfp("1")),
945 nan,
946 0, "Divide #2");
947
948 test(pinf.divide(field.newDfp("1")),
949 pinf,
950 0, "Divide #3");
951
952 test(pinf.divide(field.newDfp("-1")),
953 ninf,
954 0, "Divide #4");
955
956 test(pinf.divide(pinf),
957 nan,
958 DfpField.FLAG_INVALID, "Divide #5");
959
960 test(ninf.divide(pinf),
961 nan,
962 DfpField.FLAG_INVALID, "Divide #6");
963
964 test(pinf.divide(ninf),
965 nan,
966 DfpField.FLAG_INVALID, "Divide #7");
967
968 test(ninf.divide(ninf),
969 nan,
970 DfpField.FLAG_INVALID, "Divide #8");
971
972 test(field.newDfp("0").divide(field.newDfp("0")),
973 nan,
974 DfpField.FLAG_DIV_ZERO, "Divide #9");
975
976 test(field.newDfp("1").divide(field.newDfp("0")),
977 pinf,
978 DfpField.FLAG_DIV_ZERO, "Divide #10");
979
980 test(field.newDfp("1").divide(field.newDfp("-0")),
981 ninf,
982 DfpField.FLAG_DIV_ZERO, "Divide #11");
983
984 test(field.newDfp("-1").divide(field.newDfp("0")),
985 ninf,
986 DfpField.FLAG_DIV_ZERO, "Divide #12");
987
988 test(field.newDfp("-1").divide(field.newDfp("-0")),
989 pinf,
990 DfpField.FLAG_DIV_ZERO, "Divide #13");
991
992 test(field.newDfp("1").divide(field.newDfp("3")),
993 field.newDfp("0.33333333333333333333"),
994 DfpField.FLAG_INEXACT, "Divide #14");
995
996 test(field.newDfp("1").divide(field.newDfp("6")),
997 field.newDfp("0.16666666666666666667"),
998 DfpField.FLAG_INEXACT, "Divide #15");
999
1000 test(field.newDfp("10").divide(field.newDfp("6")),
1001 field.newDfp("1.6666666666666667"),
1002 DfpField.FLAG_INEXACT, "Divide #16");
1003
1004 test(field.newDfp("100").divide(field.newDfp("6")),
1005 field.newDfp("16.6666666666666667"),
1006 DfpField.FLAG_INEXACT, "Divide #17");
1007
1008 test(field.newDfp("1000").divide(field.newDfp("6")),
1009 field.newDfp("166.6666666666666667"),
1010 DfpField.FLAG_INEXACT, "Divide #18");
1011
1012 test(field.newDfp("10000").divide(field.newDfp("6")),
1013 field.newDfp("1666.6666666666666667"),
1014 DfpField.FLAG_INEXACT, "Divide #19");
1015
1016 test(field.newDfp("1").divide(field.newDfp("1")),
1017 field.newDfp("1"),
1018 0, "Divide #20");
1019
1020 test(field.newDfp("1").divide(field.newDfp("-1")),
1021 field.newDfp("-1"),
1022 0, "Divide #21");
1023
1024 test(field.newDfp("-1").divide(field.newDfp("1")),
1025 field.newDfp("-1"),
1026 0, "Divide #22");
1027
1028 test(field.newDfp("-1").divide(field.newDfp("-1")),
1029 field.newDfp("1"),
1030 0, "Divide #23");
1031
1032 test(field.newDfp("1e-65539").divide(field.newDfp("1e65539")),
1033 field.newDfp("1e-131078"),
1034 DfpField.FLAG_UNDERFLOW, "Divide #24");
1035
1036 test(field.newDfp("1e65539").divide(field.newDfp("1e-65539")),
1037 pinf,
1038 DfpField.FLAG_OVERFLOW, "Divide #24");
1039
1040 test(field.newDfp("2").divide(field.newDfp("1.5")),
1041 field.newDfp("1.3333333333333333"),
1042 DfpField.FLAG_INEXACT, "Divide #25");
1043
1044 test(field.newDfp("2").divide(pinf),
1045 field.newDfp("0"),
1046 0, "Divide #26");
1047
1048 test(field.newDfp("2").divide(ninf),
1049 field.newDfp("-0"),
1050 0, "Divide #27");
1051
1052 test(field.newDfp("0").divide(field.newDfp("1")),
1053 field.newDfp("0"),
1054 0, "Divide #28");
1055 }
1056
1057 @Test
1058 public void testReciprocal()
1059 {
1060 test(nan.reciprocal(),
1061 nan,
1062 0, "Reciprocal #1");
1063
1064 test(field.newDfp("0").reciprocal(),
1065 pinf,
1066 DfpField.FLAG_DIV_ZERO, "Reciprocal #2");
1067
1068 test(field.newDfp("-0").reciprocal(),
1069 ninf,
1070 DfpField.FLAG_DIV_ZERO, "Reciprocal #3");
1071
1072 test(field.newDfp("3").reciprocal(),
1073 field.newDfp("0.33333333333333333333"),
1074 DfpField.FLAG_INEXACT, "Reciprocal #4");
1075
1076 test(field.newDfp("6").reciprocal(),
1077 field.newDfp("0.16666666666666666667"),
1078 DfpField.FLAG_INEXACT, "Reciprocal #5");
1079
1080 test(field.newDfp("1").reciprocal(),
1081 field.newDfp("1"),
1082 0, "Reciprocal #6");
1083
1084 test(field.newDfp("-1").reciprocal(),
1085 field.newDfp("-1"),
1086 0, "Reciprocal #7");
1087
1088 test(pinf.reciprocal(),
1089 field.newDfp("0"),
1090 0, "Reciprocal #8");
1091
1092 test(ninf.reciprocal(),
1093 field.newDfp("-0"),
1094 0, "Reciprocal #9");
1095 }
1096
1097 @Test
1098 public void testDivideInt()
1099 {
1100 test(nan.divide(1),
1101 nan,
1102 0, "DivideInt #1");
1103
1104 test(pinf.divide(1),
1105 pinf,
1106 0, "DivideInt #2");
1107
1108 test(field.newDfp("0").divide(0),
1109 nan,
1110 DfpField.FLAG_DIV_ZERO, "DivideInt #3");
1111
1112 test(field.newDfp("1").divide(0),
1113 pinf,
1114 DfpField.FLAG_DIV_ZERO, "DivideInt #4");
1115
1116 test(field.newDfp("-1").divide(0),
1117 ninf,
1118 DfpField.FLAG_DIV_ZERO, "DivideInt #5");
1119
1120 test(field.newDfp("1").divide(3),
1121 field.newDfp("0.33333333333333333333"),
1122 DfpField.FLAG_INEXACT, "DivideInt #6");
1123
1124 test(field.newDfp("1").divide(6),
1125 field.newDfp("0.16666666666666666667"),
1126 DfpField.FLAG_INEXACT, "DivideInt #7");
1127
1128 test(field.newDfp("10").divide(6),
1129 field.newDfp("1.6666666666666667"),
1130 DfpField.FLAG_INEXACT, "DivideInt #8");
1131
1132 test(field.newDfp("100").divide(6),
1133 field.newDfp("16.6666666666666667"),
1134 DfpField.FLAG_INEXACT, "DivideInt #9");
1135
1136 test(field.newDfp("1000").divide(6),
1137 field.newDfp("166.6666666666666667"),
1138 DfpField.FLAG_INEXACT, "DivideInt #10");
1139
1140 test(field.newDfp("10000").divide(6),
1141 field.newDfp("1666.6666666666666667"),
1142 DfpField.FLAG_INEXACT, "DivideInt #20");
1143
1144 test(field.newDfp("1").divide(1),
1145 field.newDfp("1"),
1146 0, "DivideInt #21");
1147
1148 test(field.newDfp("1e-131077").divide(10),
1149 field.newDfp("1e-131078"),
1150 DfpField.FLAG_UNDERFLOW, "DivideInt #22");
1151
1152 test(field.newDfp("0").divide(1),
1153 field.newDfp("0"),
1154 0, "DivideInt #23");
1155
1156 test(field.newDfp("1").divide(10000),
1157 nan,
1158 DfpField.FLAG_INVALID, "DivideInt #24");
1159
1160 test(field.newDfp("1").divide(-1),
1161 nan,
1162 DfpField.FLAG_INVALID, "DivideInt #25");
1163 }
1164
1165 @Test
1166 public void testNextAfter()
1167 {
1168 test(field.newDfp("1").nextAfter(pinf),
1169 field.newDfp("1.0000000000000001"),
1170 0, "NextAfter #1");
1171
1172 test(field.newDfp("1.0000000000000001").nextAfter(ninf),
1173 field.newDfp("1"),
1174 0, "NextAfter #1.5");
1175
1176 test(field.newDfp("1").nextAfter(ninf),
1177 field.newDfp("0.99999999999999999999"),
1178 0, "NextAfter #2");
1179
1180 test(field.newDfp("0.99999999999999999999").nextAfter(field.newDfp("2")),
1181 field.newDfp("1"),
1182 0, "NextAfter #3");
1183
1184 test(field.newDfp("-1").nextAfter(ninf),
1185 field.newDfp("-1.0000000000000001"),
1186 0, "NextAfter #4");
1187
1188 test(field.newDfp("-1").nextAfter(pinf),
1189 field.newDfp("-0.99999999999999999999"),
1190 0, "NextAfter #5");
1191
1192 test(field.newDfp("-0.99999999999999999999").nextAfter(field.newDfp("-2")),
1193 field.newDfp("-1"),
1194 0, "NextAfter #6");
1195
1196 test(field.newDfp("2").nextAfter(field.newDfp("2")),
1197 field.newDfp("2"),
1198 0, "NextAfter #7");
1199
1200 test(field.newDfp("0").nextAfter(field.newDfp("0")),
1201 field.newDfp("0"),
1202 0, "NextAfter #8");
1203
1204 test(field.newDfp("-2").nextAfter(field.newDfp("-2")),
1205 field.newDfp("-2"),
1206 0, "NextAfter #9");
1207
1208 test(field.newDfp("0").nextAfter(field.newDfp("1")),
1209 field.newDfp("1e-131092"),
1210 DfpField.FLAG_UNDERFLOW, "NextAfter #10");
1211
1212 test(field.newDfp("0").nextAfter(field.newDfp("-1")),
1213 field.newDfp("-1e-131092"),
1214 DfpField.FLAG_UNDERFLOW, "NextAfter #11");
1215
1216 test(field.newDfp("-1e-131092").nextAfter(pinf),
1217 field.newDfp("-0"),
1218 DfpField.FLAG_UNDERFLOW|DfpField.FLAG_INEXACT, "Next After #12");
1219
1220 test(field.newDfp("1e-131092").nextAfter(ninf),
1221 field.newDfp("0"),
1222 DfpField.FLAG_UNDERFLOW|DfpField.FLAG_INEXACT, "Next After #13");
1223
1224 test(field.newDfp("9.9999999999999999999e131078").nextAfter(pinf),
1225 pinf,
1226 DfpField.FLAG_OVERFLOW|DfpField.FLAG_INEXACT, "Next After #14");
1227 }
1228
1229 @Test
1230 public void testToString()
1231 {
1232 Assert.assertEquals("toString #1", "Infinity", pinf.toString());
1233 Assert.assertEquals("toString #2", "-Infinity", ninf.toString());
1234 Assert.assertEquals("toString #3", "NaN", nan.toString());
1235 Assert.assertEquals("toString #4", "NaN", field.newDfp((byte) 1, Dfp.QNAN).toString());
1236 Assert.assertEquals("toString #5", "NaN", field.newDfp((byte) 1, Dfp.SNAN).toString());
1237 Assert.assertEquals("toString #6", "1.2300000000000000e100", field.newDfp("1.23e100").toString());
1238 Assert.assertEquals("toString #7", "-1.2300000000000000e100", field.newDfp("-1.23e100").toString());
1239 Assert.assertEquals("toString #8", "12345678.1234", field.newDfp("12345678.1234").toString());
1240 Assert.assertEquals("toString #9", "0.00001234", field.newDfp("0.00001234").toString());
1241 }
1242
1243 @Override
1244 @Test
1245 public void testRound()
1246 {
1247 field.setRoundingMode(DfpField.RoundingMode.ROUND_DOWN);
1248
1249
1250 test(field.newDfp("12345678901234567890").add(field.newDfp("0.9")),
1251 field.newDfp("12345678901234567890"),
1252 DfpField.FLAG_INEXACT, "Round #1");
1253
1254 test(field.newDfp("12345678901234567890").add(field.newDfp("0.99999999")),
1255 field.newDfp("12345678901234567890"),
1256 DfpField.FLAG_INEXACT, "Round #2");
1257
1258 test(field.newDfp("-12345678901234567890").add(field.newDfp("-0.99999999")),
1259 field.newDfp("-12345678901234567890"),
1260 DfpField.FLAG_INEXACT, "Round #3");
1261
1262 field.setRoundingMode(DfpField.RoundingMode.ROUND_UP);
1263
1264
1265 test(field.newDfp("12345678901234567890").add(field.newDfp("0.1")),
1266 field.newDfp("12345678901234567891"),
1267 DfpField.FLAG_INEXACT, "Round #4");
1268
1269 test(field.newDfp("12345678901234567890").add(field.newDfp("0.0001")),
1270 field.newDfp("12345678901234567891"),
1271 DfpField.FLAG_INEXACT, "Round #5");
1272
1273 test(field.newDfp("-12345678901234567890").add(field.newDfp("-0.1")),
1274 field.newDfp("-12345678901234567891"),
1275 DfpField.FLAG_INEXACT, "Round #6");
1276
1277 test(field.newDfp("-12345678901234567890").add(field.newDfp("-0.0001")),
1278 field.newDfp("-12345678901234567891"),
1279 DfpField.FLAG_INEXACT, "Round #7");
1280
1281 field.setRoundingMode(DfpField.RoundingMode.ROUND_HALF_UP);
1282
1283
1284 test(field.newDfp("12345678901234567890").add(field.newDfp("0.4999")),
1285 field.newDfp("12345678901234567890"),
1286 DfpField.FLAG_INEXACT, "Round #8");
1287
1288 test(field.newDfp("12345678901234567890").add(field.newDfp("0.5000")),
1289 field.newDfp("12345678901234567891"),
1290 DfpField.FLAG_INEXACT, "Round #9");
1291
1292 test(field.newDfp("-12345678901234567890").add(field.newDfp("-0.4999")),
1293 field.newDfp("-12345678901234567890"),
1294 DfpField.FLAG_INEXACT, "Round #10");
1295
1296 test(field.newDfp("-12345678901234567890").add(field.newDfp("-0.5000")),
1297 field.newDfp("-12345678901234567891"),
1298 DfpField.FLAG_INEXACT, "Round #11");
1299
1300 field.setRoundingMode(DfpField.RoundingMode.ROUND_HALF_DOWN);
1301
1302
1303 test(field.newDfp("12345678901234567890").add(field.newDfp("0.5001")),
1304 field.newDfp("12345678901234567891"),
1305 DfpField.FLAG_INEXACT, "Round #12");
1306
1307 test(field.newDfp("12345678901234567890").add(field.newDfp("0.5000")),
1308 field.newDfp("12345678901234567890"),
1309 DfpField.FLAG_INEXACT, "Round #13");
1310
1311 test(field.newDfp("-12345678901234567890").add(field.newDfp("-0.5001")),
1312 field.newDfp("-12345678901234567891"),
1313 DfpField.FLAG_INEXACT, "Round #14");
1314
1315 test(field.newDfp("-12345678901234567890").add(field.newDfp("-0.5000")),
1316 field.newDfp("-12345678901234567890"),
1317 DfpField.FLAG_INEXACT, "Round #15");
1318
1319 field.setRoundingMode(DfpField.RoundingMode.ROUND_HALF_ODD);
1320
1321
1322 test(field.newDfp("12345678901234567890").add(field.newDfp("0.5000")),
1323 field.newDfp("12345678901234567891"),
1324 DfpField.FLAG_INEXACT, "Round #16");
1325
1326 test(field.newDfp("12345678901234567891").add(field.newDfp("0.5000")),
1327 field.newDfp("12345678901234567891"),
1328 DfpField.FLAG_INEXACT, "Round #17");
1329
1330 test(field.newDfp("-12345678901234567890").add(field.newDfp("-0.5000")),
1331 field.newDfp("-12345678901234567891"),
1332 DfpField.FLAG_INEXACT, "Round #18");
1333
1334 test(field.newDfp("-12345678901234567891").add(field.newDfp("-0.5000")),
1335 field.newDfp("-12345678901234567891"),
1336 DfpField.FLAG_INEXACT, "Round #19");
1337
1338 field.setRoundingMode(DfpField.RoundingMode.ROUND_CEIL);
1339
1340
1341 test(field.newDfp("12345678901234567890").add(field.newDfp("0.0001")),
1342 field.newDfp("12345678901234567891"),
1343 DfpField.FLAG_INEXACT, "Round #20");
1344
1345 test(field.newDfp("-12345678901234567890").add(field.newDfp("-0.9999")),
1346 field.newDfp("-12345678901234567890"),
1347 DfpField.FLAG_INEXACT, "Round #21");
1348
1349 field.setRoundingMode(DfpField.RoundingMode.ROUND_FLOOR);
1350
1351
1352 test(field.newDfp("12345678901234567890").add(field.newDfp("0.9999")),
1353 field.newDfp("12345678901234567890"),
1354 DfpField.FLAG_INEXACT, "Round #22");
1355
1356 test(field.newDfp("-12345678901234567890").add(field.newDfp("-0.0001")),
1357 field.newDfp("-12345678901234567891"),
1358 DfpField.FLAG_INEXACT, "Round #23");
1359
1360 field.setRoundingMode(DfpField.RoundingMode.ROUND_HALF_EVEN);
1361 }
1362
1363 @Override
1364 @Test
1365 public void testCeil()
1366 {
1367 test(field.newDfp("1234.0000000000000001").ceil(),
1368 field.newDfp("1235"),
1369 DfpField.FLAG_INEXACT, "Ceil #1");
1370 }
1371
1372 @Test
1373 public void testCeilSmallNegative()
1374 {
1375 test(field.newDfp("-0.00009999").ceil(),
1376 field.newDfp("0"),
1377 DfpField.FLAG_INEXACT, "Ceil small positive");
1378 }
1379
1380 @Test
1381 public void testCeilSmallPositive()
1382 {
1383 test(field.newDfp("+0.00009999").ceil(),
1384 field.newDfp("+1"),
1385 DfpField.FLAG_INEXACT, "Ceil small positive");
1386 }
1387
1388 @Override
1389 @Test
1390 public void testFloor()
1391 {
1392 test(field.newDfp("1234.9999999999999999").floor(),
1393 field.newDfp("1234"),
1394 DfpField.FLAG_INEXACT, "Floor #1");
1395 }
1396
1397 @Test
1398 public void testFloorSmallNegative()
1399 {
1400 test(field.newDfp("-0.00009999").floor(),
1401 field.newDfp("-1"),
1402 DfpField.FLAG_INEXACT, "Floor small negative");
1403 }
1404
1405 @Test
1406 public void testFloorSmallPositive()
1407 {
1408 test(field.newDfp("+0.00009999").floor(),
1409 field.newDfp("0"),
1410 DfpField.FLAG_INEXACT, "Floor small positive");
1411 }
1412
1413 @Override
1414 @Test
1415 public void testRint()
1416 {
1417 test(field.newDfp("1234.50000000001").rint(),
1418 field.newDfp("1235"),
1419 DfpField.FLAG_INEXACT, "Rint #1");
1420
1421 test(field.newDfp("1234.5000").rint(),
1422 field.newDfp("1234"),
1423 DfpField.FLAG_INEXACT, "Rint #2");
1424
1425 test(field.newDfp("1235.5000").rint(),
1426 field.newDfp("1236"),
1427 DfpField.FLAG_INEXACT, "Rint #3");
1428 }
1429
1430 @Test
1431 public void testCopySign()
1432 {
1433 test(Dfp.copysign(field.newDfp("1234."), field.newDfp("-1")),
1434 field.newDfp("-1234"),
1435 0, "CopySign #1");
1436
1437 test(Dfp.copysign(field.newDfp("-1234."), field.newDfp("-1")),
1438 field.newDfp("-1234"),
1439 0, "CopySign #2");
1440
1441 test(Dfp.copysign(field.newDfp("-1234."), field.newDfp("1")),
1442 field.newDfp("1234"),
1443 0, "CopySign #3");
1444
1445 test(Dfp.copysign(field.newDfp("1234."), field.newDfp("1")),
1446 field.newDfp("1234"),
1447 0, "CopySign #4");
1448 }
1449
1450 @Test
1451 public void testIntValue()
1452 {
1453 Assert.assertEquals("intValue #1", 1234, field.newDfp("1234").intValue());
1454 Assert.assertEquals("intValue #2", -1234, field.newDfp("-1234").intValue());
1455 Assert.assertEquals("intValue #3", 1234, field.newDfp("1234.5").intValue());
1456 Assert.assertEquals("intValue #4", 1235, field.newDfp("1234.500001").intValue());
1457 Assert.assertEquals("intValue #5", 2147483647, field.newDfp("1e1000").intValue());
1458 Assert.assertEquals("intValue #6", -2147483648, field.newDfp("-1e1000").intValue());
1459 }
1460
1461 @Test
1462 public void testLog10K()
1463 {
1464 Assert.assertEquals("log10K #1", 1, field.newDfp("123456").log10K());
1465 Assert.assertEquals("log10K #2", 2, field.newDfp("123456789").log10K());
1466 Assert.assertEquals("log10K #3", 0, field.newDfp("2").log10K());
1467 Assert.assertEquals("log10K #3", 0, field.newDfp("1").log10K());
1468 Assert.assertEquals("log10K #4", -1, field.newDfp("0.1").log10K());
1469 }
1470
1471 @Test
1472 public void testPower10K()
1473 {
1474 Dfp d = field.newDfp();
1475
1476 test(d.power10K(0), field.newDfp("1"), 0, "Power10 #1");
1477 test(d.power10K(1), field.newDfp("10000"), 0, "Power10 #2");
1478 test(d.power10K(2), field.newDfp("100000000"), 0, "Power10 #3");
1479
1480 test(d.power10K(-1), field.newDfp("0.0001"), 0, "Power10 #4");
1481 test(d.power10K(-2), field.newDfp("0.00000001"), 0, "Power10 #5");
1482 test(d.power10K(-3), field.newDfp("0.000000000001"), 0, "Power10 #6");
1483 }
1484
1485 @Test
1486 public void testLog10()
1487 {
1488
1489 Assert.assertEquals("log10 #1", 1, field.newDfp("12").intLog10());
1490 Assert.assertEquals("log10 #2", 2, field.newDfp("123").intLog10());
1491 Assert.assertEquals("log10 #3", 3, field.newDfp("1234").intLog10());
1492 Assert.assertEquals("log10 #4", 4, field.newDfp("12345").intLog10());
1493 Assert.assertEquals("log10 #5", 5, field.newDfp("123456").intLog10());
1494 Assert.assertEquals("log10 #6", 6, field.newDfp("1234567").intLog10());
1495 Assert.assertEquals("log10 #6", 7, field.newDfp("12345678").intLog10());
1496 Assert.assertEquals("log10 #7", 8, field.newDfp("123456789").intLog10());
1497 Assert.assertEquals("log10 #8", 9, field.newDfp("1234567890").intLog10());
1498 Assert.assertEquals("log10 #9", 10, field.newDfp("12345678901").intLog10());
1499 Assert.assertEquals("log10 #10", 11, field.newDfp("123456789012").intLog10());
1500 Assert.assertEquals("log10 #11", 12, field.newDfp("1234567890123").intLog10());
1501
1502 Assert.assertEquals("log10 #12", 0, field.newDfp("2").intLog10());
1503 Assert.assertEquals("log10 #13", 0, field.newDfp("1").intLog10());
1504 Assert.assertEquals("log10 #14", -1, field.newDfp("0.12").intLog10());
1505 Assert.assertEquals("log10 #15", -2, field.newDfp("0.012").intLog10());
1506 }
1507
1508 @Test
1509 public void testPower10()
1510 {
1511 Dfp d = field.newDfp();
1512
1513 test(d.power10(0), field.newDfp("1"), 0, "Power10 #1");
1514 test(d.power10(1), field.newDfp("10"), 0, "Power10 #2");
1515 test(d.power10(2), field.newDfp("100"), 0, "Power10 #3");
1516 test(d.power10(3), field.newDfp("1000"), 0, "Power10 #4");
1517 test(d.power10(4), field.newDfp("10000"), 0, "Power10 #5");
1518 test(d.power10(5), field.newDfp("100000"), 0, "Power10 #6");
1519 test(d.power10(6), field.newDfp("1000000"), 0, "Power10 #7");
1520 test(d.power10(7), field.newDfp("10000000"), 0, "Power10 #8");
1521 test(d.power10(8), field.newDfp("100000000"), 0, "Power10 #9");
1522 test(d.power10(9), field.newDfp("1000000000"), 0, "Power10 #10");
1523
1524 test(d.power10(-1), field.newDfp(".1"), 0, "Power10 #11");
1525 test(d.power10(-2), field.newDfp(".01"), 0, "Power10 #12");
1526 test(d.power10(-3), field.newDfp(".001"), 0, "Power10 #13");
1527 test(d.power10(-4), field.newDfp(".0001"), 0, "Power10 #14");
1528 test(d.power10(-5), field.newDfp(".00001"), 0, "Power10 #15");
1529 test(d.power10(-6), field.newDfp(".000001"), 0, "Power10 #16");
1530 test(d.power10(-7), field.newDfp(".0000001"), 0, "Power10 #17");
1531 test(d.power10(-8), field.newDfp(".00000001"), 0, "Power10 #18");
1532 test(d.power10(-9), field.newDfp(".000000001"), 0, "Power10 #19");
1533 test(d.power10(-10), field.newDfp(".0000000001"), 0, "Power10 #20");
1534 }
1535
1536 @Test
1537 public void testRemainder()
1538 {
1539 test(field.newDfp("10").remainder(field.newDfp("3")),
1540 field.newDfp("1"),
1541 DfpField.FLAG_INEXACT, "Remainder #1");
1542
1543 test(field.newDfp("9").remainder(field.newDfp("3")),
1544 field.newDfp("0"),
1545 0, "Remainder #2");
1546
1547 test(field.newDfp("-9").remainder(field.newDfp("3")),
1548 field.newDfp("-0"),
1549 0, "Remainder #3");
1550 }
1551
1552 @Override
1553 @Test
1554 public void testSqrt()
1555 {
1556 test(field.newDfp("0").sqrt(),
1557 field.newDfp("0"),
1558 0, "Sqrt #1");
1559
1560 test(field.newDfp("-0").sqrt(),
1561 field.newDfp("-0"),
1562 0, "Sqrt #2");
1563
1564 test(field.newDfp("1").sqrt(),
1565 field.newDfp("1"),
1566 0, "Sqrt #3");
1567
1568 test(field.newDfp("2").sqrt(),
1569 field.newDfp("1.4142135623730950"),
1570 DfpField.FLAG_INEXACT, "Sqrt #4");
1571
1572 test(field.newDfp("3").sqrt(),
1573 field.newDfp("1.7320508075688773"),
1574 DfpField.FLAG_INEXACT, "Sqrt #5");
1575
1576 test(field.newDfp("5").sqrt(),
1577 field.newDfp("2.2360679774997897"),
1578 DfpField.FLAG_INEXACT, "Sqrt #6");
1579
1580 test(field.newDfp("500").sqrt(),
1581 field.newDfp("22.3606797749978970"),
1582 DfpField.FLAG_INEXACT, "Sqrt #6.2");
1583
1584 test(field.newDfp("50000").sqrt(),
1585 field.newDfp("223.6067977499789696"),
1586 DfpField.FLAG_INEXACT, "Sqrt #6.3");
1587
1588 test(field.newDfp("-1").sqrt(),
1589 nan,
1590 DfpField.FLAG_INVALID, "Sqrt #7");
1591
1592 test(pinf.sqrt(),
1593 pinf,
1594 0, "Sqrt #8");
1595
1596 test(field.newDfp((byte) 1, Dfp.QNAN).sqrt(),
1597 nan,
1598 0, "Sqrt #9");
1599
1600 test(field.newDfp((byte) 1, Dfp.SNAN).sqrt(),
1601 nan,
1602 DfpField.FLAG_INVALID, "Sqrt #9");
1603 }
1604
1605 @Test
1606 public void testIssue567() {
1607 DfpField field = new DfpField(100);
1608 Assert.assertEquals(0.0, field.getZero().toDouble(), Precision.SAFE_MIN);
1609 Assert.assertEquals(0.0, field.newDfp(0.0).toDouble(), Precision.SAFE_MIN);
1610 Assert.assertEquals(-1, FastMath.copySign(1, field.newDfp(-0.0).toDouble()), Precision.EPSILON);
1611 Assert.assertEquals(+1, FastMath.copySign(1, field.newDfp(+0.0).toDouble()), Precision.EPSILON);
1612 }
1613
1614 @Test
1615 public void testIsZero() {
1616 Assert.assertTrue(field.getZero().isZero());
1617 Assert.assertTrue(field.getZero().negate().isZero());
1618 Assert.assertTrue(field.newDfp(+0.0).isZero());
1619 Assert.assertTrue(field.newDfp(-0.0).isZero());
1620 Assert.assertFalse(field.newDfp(1.0e-90).isZero());
1621 Assert.assertFalse(nan.isZero());
1622 Assert.assertFalse(nan.negate().isZero());
1623 Assert.assertFalse(pinf.isZero());
1624 Assert.assertFalse(pinf.negate().isZero());
1625 Assert.assertFalse(ninf.isZero());
1626 Assert.assertFalse(ninf.negate().isZero());
1627 }
1628
1629 @Test
1630 public void testSignPredicates() {
1631
1632 Assert.assertTrue(field.getZero().negativeOrNull());
1633 Assert.assertTrue(field.getZero().positiveOrNull());
1634 Assert.assertFalse(field.getZero().strictlyNegative());
1635 Assert.assertFalse(field.getZero().strictlyPositive());
1636
1637 Assert.assertTrue(field.getZero().negate().negativeOrNull());
1638 Assert.assertTrue(field.getZero().negate().positiveOrNull());
1639 Assert.assertFalse(field.getZero().negate().strictlyNegative());
1640 Assert.assertFalse(field.getZero().negate().strictlyPositive());
1641
1642 Assert.assertFalse(field.getOne().negativeOrNull());
1643 Assert.assertTrue(field.getOne().positiveOrNull());
1644 Assert.assertFalse(field.getOne().strictlyNegative());
1645 Assert.assertTrue(field.getOne().strictlyPositive());
1646
1647 Assert.assertTrue(field.getOne().negate().negativeOrNull());
1648 Assert.assertFalse(field.getOne().negate().positiveOrNull());
1649 Assert.assertTrue(field.getOne().negate().strictlyNegative());
1650 Assert.assertFalse(field.getOne().negate().strictlyPositive());
1651
1652 Assert.assertFalse(nan.negativeOrNull());
1653 Assert.assertFalse(nan.positiveOrNull());
1654 Assert.assertFalse(nan.strictlyNegative());
1655 Assert.assertFalse(nan.strictlyPositive());
1656
1657 Assert.assertFalse(nan.negate().negativeOrNull());
1658 Assert.assertFalse(nan.negate().positiveOrNull());
1659 Assert.assertFalse(nan.negate().strictlyNegative());
1660 Assert.assertFalse(nan.negate().strictlyPositive());
1661
1662 Assert.assertFalse(pinf.negativeOrNull());
1663 Assert.assertTrue(pinf.positiveOrNull());
1664 Assert.assertFalse(pinf.strictlyNegative());
1665 Assert.assertTrue(pinf.strictlyPositive());
1666
1667 Assert.assertTrue(pinf.negate().negativeOrNull());
1668 Assert.assertFalse(pinf.negate().positiveOrNull());
1669 Assert.assertTrue(pinf.negate().strictlyNegative());
1670 Assert.assertFalse(pinf.negate().strictlyPositive());
1671
1672 Assert.assertTrue(ninf.negativeOrNull());
1673 Assert.assertFalse(ninf.positiveOrNull());
1674 Assert.assertTrue(ninf.strictlyNegative());
1675 Assert.assertFalse(ninf.strictlyPositive());
1676
1677 Assert.assertFalse(ninf.negate().negativeOrNull());
1678 Assert.assertTrue(ninf.negate().positiveOrNull());
1679 Assert.assertFalse(ninf.negate().strictlyNegative());
1680 Assert.assertTrue(ninf.negate().strictlyPositive());
1681
1682 }
1683
1684 @Test
1685 public void testSpecialConstructors() {
1686 Assert.assertEquals(ninf, field.newDfp(Double.NEGATIVE_INFINITY));
1687 Assert.assertEquals(ninf, field.newDfp("-Infinity"));
1688 Assert.assertEquals(pinf, field.newDfp(Double.POSITIVE_INFINITY));
1689 Assert.assertEquals(pinf, field.newDfp("Infinity"));
1690 Assert.assertTrue(field.newDfp(Double.NaN).isNaN());
1691 Assert.assertTrue(field.newDfp("NaN").isNaN());
1692 }
1693
1694 @Test
1695 public void testHypotNoOverflow() {
1696 Dfp x = field.newDfp(+3);
1697 Dfp y = field.newDfp(-4);
1698 Dfp h = field.newDfp(+5);
1699 for (int i = 0; i < 70000; ++i) {
1700 x = x.multiply(10);
1701 y = y.multiply(10);
1702 h = h.multiply(10);
1703 }
1704 Assert.assertEquals(h, x.hypot(y));
1705 }
1706
1707 @Test
1708 public void testGetExponentVsDouble() {
1709 for (int i = -1000; i < 1000; ++i) {
1710 final double x = FastMath.scalb(1.0, i);
1711 final double xMinus = 0.99 * x;
1712 final double xPlus = 1.01 * x;
1713 final Dfp dfpMinus = field.newDfp(xMinus);
1714 final Dfp dfpPlus = field.newDfp(xPlus);
1715 Assert.assertEquals(FastMath.getExponent(xMinus), dfpMinus.getExponent());
1716 Assert.assertEquals(FastMath.getExponent(xPlus), dfpPlus.getExponent());
1717 }
1718 }
1719
1720 @Test
1721 public void testGetExponentSpecialCases() {
1722 Assert.assertEquals(-435412, field.newDfp(0).getExponent());
1723 Assert.assertEquals(0, field.newDfp(1).getExponent());
1724 Assert.assertEquals(1, field.newDfp(2).getExponent());
1725 Assert.assertEquals(435411, field.newDfp(Double.NaN).getExponent());
1726 Assert.assertEquals(435411, field.newDfp(Double.POSITIVE_INFINITY).getExponent());
1727 Assert.assertEquals(435411, field.newDfp(Double.NEGATIVE_INFINITY).getExponent());
1728 }
1729
1730 @Test
1731 public void testGetExponentAutonomous() {
1732 for (int i = -435411; i < 435411; i += 217) {
1733 final Dfp x = field.newDfp(2).pow(i).multiply(1.1);
1734 Assert.assertEquals(i, x.getExponent());
1735 }
1736 }
1737
1738 @Test
1739 public void testEqualsHashcodeContract() {
1740 DfpField var1 = new DfpField(1);
1741 Dfp var6 = var1.newDfp(-0.0d);
1742 Dfp var5 = var1.newDfp(0L);
1743
1744
1745 Assert.assertTrue(var5.equals(var6) ? var5.hashCode() == var6.hashCode() : true);
1746 }
1747
1748 @Test
1749 public void testZero() {
1750 Dfp zero = new DfpField(15).getZero();
1751 Assert.assertEquals(0.0, zero.toDouble(), 1.0e-15);
1752 }
1753
1754 @Test
1755 public void testOne() {
1756 Dfp one = new DfpField(15).getOne();
1757 Assert.assertEquals(1.0, one.toDouble(), 1.0e-15);
1758 }
1759
1760 @Test
1761 public void testToDegreesDefinition() {
1762 double epsilon = 1.0e-14;
1763 for (int maxOrder = 0; maxOrder < 6; ++maxOrder) {
1764 for (double x = 0.1; x < 1.0; x += 0.001) {
1765 Dfp value = new Dfp(field, x);
1766 Assert.assertEquals(FastMath.toDegrees(x), value.toDegrees().getReal(), epsilon);
1767 }
1768 }
1769 }
1770
1771 @Test
1772 public void testToRadiansDefinition() {
1773 double epsilon = 1.0e-15;
1774 for (int maxOrder = 0; maxOrder < 6; ++maxOrder) {
1775 for (double x = 0.1; x < 1.0; x += 0.001) {
1776 Dfp value = new Dfp(field, x);
1777 Assert.assertEquals(FastMath.toRadians(x), value.toRadians().getReal(), epsilon);
1778 }
1779 }
1780 }
1781
1782 @Test
1783 public void testDegRad() {
1784 for (double x = 0.1; x < 1.2; x += 0.001) {
1785 Dfp value = field.newDfp("x");
1786 Dfp rebuilt = value.toDegrees().toRadians();
1787 Dfp zero = rebuilt.subtract(value);
1788 Assert.assertEquals(zero.getReal(), 0, 3.0e-16);
1789 }
1790 }
1791
1792 @SuppressWarnings("unlikely-arg-type")
1793 @Test
1794 public void testMap() {
1795 int[] decimalDigits = new int[] { 10, 50, 100 };
1796 Map<DfpField, Integer> map = new HashMap<>();
1797 for (int i = 0; i < 1000; ++i) {
1798
1799 map.put(new DfpField(decimalDigits[i % decimalDigits.length]), 0);
1800 }
1801
1802
1803
1804 Assert.assertEquals(decimalDigits.length, map.size());
1805 DfpField first = map.entrySet().iterator().next().getKey();
1806 Assert.assertTrue(first.equals(first));
1807 Assert.assertFalse(first.equals(Binary64Field.getInstance()));
1808
1809 }
1810
1811 @Test
1812 public void testPi() {
1813 Assert.assertEquals("3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117",
1814 new DfpField(100).newDfp(1.0).getPi().toString());
1815 }
1816
1817 @Test
1818 public void testEquals() {
1819 DfpField f10A = new DfpField(10);
1820 DfpField f10B = new DfpField(10);
1821 DfpField f50 = new DfpField(50);
1822 Assert.assertNotEquals(f10A, f50);
1823 Assert.assertEquals(f10A, f10B);
1824 f10B.setRoundingMode(DfpField.RoundingMode.ROUND_DOWN);
1825 Assert.assertNotEquals(f10A, f10B);
1826 f10B.setRoundingMode(DfpField.RoundingMode.ROUND_HALF_EVEN);
1827 Assert.assertEquals(f10A, f10B);
1828 f10B.setIEEEFlags(DfpField.FLAG_UNDERFLOW | DfpField.FLAG_OVERFLOW);
1829 Assert.assertNotEquals(f10A.getIEEEFlags(), f10B.getIEEEFlags());
1830 Assert.assertEquals(f10A, f10B);
1831 }
1832
1833 @Test
1834 public void testRunTimeClass() {
1835 DfpField field = new DfpField(15);
1836 Assert.assertEquals(Dfp.class, field.getRuntimeClass());
1837 }
1838
1839 @Override
1840 @Test
1841 public void testLinearCombinationReference() {
1842 final DfpField field25 = new DfpField(25);
1843 doTestLinearCombinationReference(x -> field25.newDfp(x), 4.15e-9, 4.21e-9);
1844 }
1845
1846 @Test
1847 public void testConvertToSameAccuracy() {
1848 DfpField field13 = new DfpField(13);
1849 DfpField field16 = new DfpField(16);
1850 Dfp dfp = field13.newDfp(1.25);
1851 Assert.assertSame(dfp, dfp.newInstance(field16, DfpField.RoundingMode.ROUND_HALF_EVEN));
1852 }
1853
1854 @Test
1855 public void testConvertToHigherAccuracy() {
1856
1857 DfpField field16 = new DfpField(16);
1858 DfpField field24 = new DfpField(24);
1859
1860 checkConvert(field16, "1.25", field24, "1.25", null);
1861
1862 Assert.assertTrue(field16.newDfp(-1).sqrt().newInstance(field24, null).isNaN());
1863 Assert.assertTrue(field16.newDfp().reciprocal().newInstance(field24, null).isInfinite());
1864 }
1865
1866 @Test
1867 public void testUlpdDfpA() {
1868 Assert.assertEquals(1.0e-36, new DfpField(40).getOne().ulp().getReal(), 1.0e-51);
1869 Assert.assertEquals(1.0e-40, new DfpField(41).getOne().ulp().getReal(), 1.0e-55);
1870 }
1871
1872 @Test
1873 public void testUlpdDfpB() {
1874 DfpField field = new DfpField(41);
1875 Dfp one = field.getOne();
1876 Dfp ulp = one.ulp();
1877 Assert.assertTrue(one.add(ulp).greaterThan(one));
1878 Assert.assertFalse(one.add(ulp.divide(2)).greaterThan(one));
1879 }
1880
1881 @Test
1882 public void testConvertToLowerAccuracy() {
1883 DfpField field16 = new DfpField(16);
1884 DfpField field24 = new DfpField(24);
1885
1886 checkConvert(field24, "1234.56789012345678901234", field16, "1234.567890123456", DfpField.RoundingMode.ROUND_DOWN);
1887 checkConvert(field24, "1234.56789012345678901234", field16, "1234.567890123457", DfpField.RoundingMode.ROUND_UP);
1888 checkConvert(field24, "1234.56789012345678901234", field16, "1234.567890123457", DfpField.RoundingMode.ROUND_HALF_UP);
1889 checkConvert(field24, "1234.56789012345678901234", field16, "1234.567890123457", DfpField.RoundingMode.ROUND_HALF_DOWN);
1890 checkConvert(field24, "1234.56789012345678901234", field16, "1234.567890123457", DfpField.RoundingMode.ROUND_HALF_EVEN);
1891 checkConvert(field24, "1234.56789012345678901234", field16, "1234.567890123457", DfpField.RoundingMode.ROUND_HALF_ODD);
1892 checkConvert(field24, "1234.56789012345678901234", field16, "1234.567890123457", DfpField.RoundingMode.ROUND_CEIL);
1893 checkConvert(field24, "1234.56789012345678901234", field16, "1234.567890123456", DfpField.RoundingMode.ROUND_FLOOR);
1894
1895 checkConvert(field24, "1234.56789012345600000001", field16, "1234.567890123456", DfpField.RoundingMode.ROUND_DOWN);
1896 checkConvert(field24, "1234.56789012345600000001", field16, "1234.567890123457", DfpField.RoundingMode.ROUND_UP);
1897 checkConvert(field24, "1234.56789012345600000001", field16, "1234.567890123456", DfpField.RoundingMode.ROUND_HALF_UP);
1898 checkConvert(field24, "1234.56789012345600000001", field16, "1234.567890123456", DfpField.RoundingMode.ROUND_HALF_DOWN);
1899 checkConvert(field24, "1234.56789012345600000001", field16, "1234.567890123456", DfpField.RoundingMode.ROUND_HALF_EVEN);
1900 checkConvert(field24, "1234.56789012345600000001", field16, "1234.567890123456", DfpField.RoundingMode.ROUND_HALF_ODD);
1901 checkConvert(field24, "1234.56789012345600000001", field16, "1234.567890123457", DfpField.RoundingMode.ROUND_CEIL);
1902 checkConvert(field24, "1234.56789012345600000001", field16, "1234.567890123456", DfpField.RoundingMode.ROUND_FLOOR);
1903
1904 checkConvert(field24, "1234.56789012345650000000", field16, "1234.567890123456", DfpField.RoundingMode.ROUND_DOWN);
1905 checkConvert(field24, "1234.56789012345650000000", field16, "1234.567890123457", DfpField.RoundingMode.ROUND_UP);
1906 checkConvert(field24, "1234.56789012345650000000", field16, "1234.567890123457", DfpField.RoundingMode.ROUND_HALF_UP);
1907 checkConvert(field24, "1234.56789012345650000000", field16, "1234.567890123456", DfpField.RoundingMode.ROUND_HALF_DOWN);
1908 checkConvert(field24, "1234.56789012345650000000", field16, "1234.567890123456", DfpField.RoundingMode.ROUND_HALF_EVEN);
1909 checkConvert(field24, "1234.56789012345650000000", field16, "1234.567890123457", DfpField.RoundingMode.ROUND_HALF_ODD);
1910 checkConvert(field24, "1234.56789012345650000000", field16, "1234.567890123457", DfpField.RoundingMode.ROUND_CEIL);
1911 checkConvert(field24, "1234.56789012345650000000", field16, "1234.567890123456", DfpField.RoundingMode.ROUND_FLOOR);
1912
1913 checkConvert(field24, "1234.56789012345650000001", field16, "1234.567890123456", DfpField.RoundingMode.ROUND_DOWN);
1914 checkConvert(field24, "1234.56789012345650000001", field16, "1234.567890123457", DfpField.RoundingMode.ROUND_UP);
1915 checkConvert(field24, "1234.56789012345650000001", field16, "1234.567890123457", DfpField.RoundingMode.ROUND_HALF_UP);
1916 checkConvert(field24, "1234.56789012345650000001", field16, "1234.567890123457", DfpField.RoundingMode.ROUND_HALF_DOWN);
1917 checkConvert(field24, "1234.56789012345650000001", field16, "1234.567890123457", DfpField.RoundingMode.ROUND_HALF_EVEN);
1918 checkConvert(field24, "1234.56789012345650000001", field16, "1234.567890123457", DfpField.RoundingMode.ROUND_HALF_ODD);
1919 checkConvert(field24, "1234.56789012345650000001", field16, "1234.567890123457", DfpField.RoundingMode.ROUND_CEIL);
1920 checkConvert(field24, "1234.56789012345650000001", field16, "1234.567890123456", DfpField.RoundingMode.ROUND_FLOOR);
1921
1922 checkConvert(field24, "1234.56789012345750000000", field16, "1234.567890123457", DfpField.RoundingMode.ROUND_DOWN);
1923 checkConvert(field24, "1234.56789012345750000000", field16, "1234.567890123458", DfpField.RoundingMode.ROUND_UP);
1924 checkConvert(field24, "1234.56789012345750000000", field16, "1234.567890123458", DfpField.RoundingMode.ROUND_HALF_UP);
1925 checkConvert(field24, "1234.56789012345750000000", field16, "1234.567890123457", DfpField.RoundingMode.ROUND_HALF_DOWN);
1926 checkConvert(field24, "1234.56789012345750000000", field16, "1234.567890123458", DfpField.RoundingMode.ROUND_HALF_EVEN);
1927 checkConvert(field24, "1234.56789012345750000000", field16, "1234.567890123457", DfpField.RoundingMode.ROUND_HALF_ODD);
1928 checkConvert(field24, "1234.56789012345750000000", field16, "1234.567890123458", DfpField.RoundingMode.ROUND_CEIL);
1929 checkConvert(field24, "1234.56789012345750000000", field16, "1234.567890123457", DfpField.RoundingMode.ROUND_FLOOR);
1930
1931 checkConvert(field24, "1234.56789012345750000001", field16, "1234.567890123457", DfpField.RoundingMode.ROUND_DOWN);
1932 checkConvert(field24, "1234.56789012345750000001", field16, "1234.567890123458", DfpField.RoundingMode.ROUND_UP);
1933 checkConvert(field24, "1234.56789012345750000001", field16, "1234.567890123458", DfpField.RoundingMode.ROUND_HALF_UP);
1934 checkConvert(field24, "1234.56789012345750000001", field16, "1234.567890123458", DfpField.RoundingMode.ROUND_HALF_DOWN);
1935 checkConvert(field24, "1234.56789012345750000001", field16, "1234.567890123458", DfpField.RoundingMode.ROUND_HALF_EVEN);
1936 checkConvert(field24, "1234.56789012345750000001", field16, "1234.567890123458", DfpField.RoundingMode.ROUND_HALF_ODD);
1937 checkConvert(field24, "1234.56789012345750000001", field16, "1234.567890123458", DfpField.RoundingMode.ROUND_CEIL);
1938 checkConvert(field24, "1234.56789012345750000001", field16, "1234.567890123457", DfpField.RoundingMode.ROUND_FLOOR);
1939
1940 checkConvert(field24, "-1234.56789012345678901234", field16, "-1234.567890123456", DfpField.RoundingMode.ROUND_DOWN);
1941 checkConvert(field24, "-1234.56789012345678901234", field16, "-1234.567890123457", DfpField.RoundingMode.ROUND_UP);
1942 checkConvert(field24, "-1234.56789012345678901234", field16, "-1234.567890123457", DfpField.RoundingMode.ROUND_HALF_UP);
1943 checkConvert(field24, "-1234.56789012345678901234", field16, "-1234.567890123457", DfpField.RoundingMode.ROUND_HALF_DOWN);
1944 checkConvert(field24, "-1234.56789012345678901234", field16, "-1234.567890123457", DfpField.RoundingMode.ROUND_HALF_EVEN);
1945 checkConvert(field24, "-1234.56789012345678901234", field16, "-1234.567890123457", DfpField.RoundingMode.ROUND_HALF_ODD);
1946 checkConvert(field24, "-1234.56789012345678901234", field16, "-1234.567890123456", DfpField.RoundingMode.ROUND_CEIL);
1947 checkConvert(field24, "-1234.56789012345678901234", field16, "-1234.567890123457", DfpField.RoundingMode.ROUND_FLOOR);
1948
1949 checkConvert(field24, "-1234.56789012345600000001", field16, "-1234.567890123456", DfpField.RoundingMode.ROUND_DOWN);
1950 checkConvert(field24, "-1234.56789012345600000001", field16, "-1234.567890123457", DfpField.RoundingMode.ROUND_UP);
1951 checkConvert(field24, "-1234.56789012345600000001", field16, "-1234.567890123456", DfpField.RoundingMode.ROUND_HALF_UP);
1952 checkConvert(field24, "-1234.56789012345600000001", field16, "-1234.567890123456", DfpField.RoundingMode.ROUND_HALF_DOWN);
1953 checkConvert(field24, "-1234.56789012345600000001", field16, "-1234.567890123456", DfpField.RoundingMode.ROUND_HALF_EVEN);
1954 checkConvert(field24, "-1234.56789012345600000001", field16, "-1234.567890123456", DfpField.RoundingMode.ROUND_HALF_ODD);
1955 checkConvert(field24, "-1234.56789012345600000001", field16, "-1234.567890123456", DfpField.RoundingMode.ROUND_CEIL);
1956 checkConvert(field24, "-1234.56789012345600000001", field16, "-1234.567890123457", DfpField.RoundingMode.ROUND_FLOOR);
1957
1958 checkConvert(field24, "-1234.56789012345650000000", field16, "-1234.567890123456", DfpField.RoundingMode.ROUND_DOWN);
1959 checkConvert(field24, "-1234.56789012345650000000", field16, "-1234.567890123457", DfpField.RoundingMode.ROUND_UP);
1960 checkConvert(field24, "-1234.56789012345650000000", field16, "-1234.567890123457", DfpField.RoundingMode.ROUND_HALF_UP);
1961 checkConvert(field24, "-1234.56789012345650000000", field16, "-1234.567890123456", DfpField.RoundingMode.ROUND_HALF_DOWN);
1962 checkConvert(field24, "-1234.56789012345650000000", field16, "-1234.567890123456", DfpField.RoundingMode.ROUND_HALF_EVEN);
1963 checkConvert(field24, "-1234.56789012345650000000", field16, "-1234.567890123457", DfpField.RoundingMode.ROUND_HALF_ODD);
1964 checkConvert(field24, "-1234.56789012345650000000", field16, "-1234.567890123456", DfpField.RoundingMode.ROUND_CEIL);
1965 checkConvert(field24, "-1234.56789012345650000000", field16, "-1234.567890123457", DfpField.RoundingMode.ROUND_FLOOR);
1966
1967 checkConvert(field24, "-1234.56789012345650000001", field16, "-1234.567890123456", DfpField.RoundingMode.ROUND_DOWN);
1968 checkConvert(field24, "-1234.56789012345650000001", field16, "-1234.567890123457", DfpField.RoundingMode.ROUND_UP);
1969 checkConvert(field24, "-1234.56789012345650000001", field16, "-1234.567890123457", DfpField.RoundingMode.ROUND_HALF_UP);
1970 checkConvert(field24, "-1234.56789012345650000001", field16, "-1234.567890123457", DfpField.RoundingMode.ROUND_HALF_DOWN);
1971 checkConvert(field24, "-1234.56789012345650000001", field16, "-1234.567890123457", DfpField.RoundingMode.ROUND_HALF_EVEN);
1972 checkConvert(field24, "-1234.56789012345650000001", field16, "-1234.567890123457", DfpField.RoundingMode.ROUND_HALF_ODD);
1973 checkConvert(field24, "-1234.56789012345650000001", field16, "-1234.567890123456", DfpField.RoundingMode.ROUND_CEIL);
1974 checkConvert(field24, "-1234.56789012345650000001", field16, "-1234.567890123457", DfpField.RoundingMode.ROUND_FLOOR);
1975
1976 checkConvert(field24, "-1234.56789012345750000000", field16, "-1234.567890123457", DfpField.RoundingMode.ROUND_DOWN);
1977 checkConvert(field24, "-1234.56789012345750000000", field16, "-1234.567890123458", DfpField.RoundingMode.ROUND_UP);
1978 checkConvert(field24, "-1234.56789012345750000000", field16, "-1234.567890123458", DfpField.RoundingMode.ROUND_HALF_UP);
1979 checkConvert(field24, "-1234.56789012345750000000", field16, "-1234.567890123457", DfpField.RoundingMode.ROUND_HALF_DOWN);
1980 checkConvert(field24, "-1234.56789012345750000000", field16, "-1234.567890123458", DfpField.RoundingMode.ROUND_HALF_EVEN);
1981 checkConvert(field24, "-1234.56789012345750000000", field16, "-1234.567890123457", DfpField.RoundingMode.ROUND_HALF_ODD);
1982 checkConvert(field24, "-1234.56789012345750000000", field16, "-1234.567890123457", DfpField.RoundingMode.ROUND_CEIL);
1983 checkConvert(field24, "-1234.56789012345750000000", field16, "-1234.567890123458", DfpField.RoundingMode.ROUND_FLOOR);
1984
1985 checkConvert(field24, "-1234.56789012345750000001", field16, "-1234.567890123457", DfpField.RoundingMode.ROUND_DOWN);
1986 checkConvert(field24, "-1234.56789012345750000001", field16, "-1234.567890123458", DfpField.RoundingMode.ROUND_UP);
1987 checkConvert(field24, "-1234.56789012345750000001", field16, "-1234.567890123458", DfpField.RoundingMode.ROUND_HALF_UP);
1988 checkConvert(field24, "-1234.56789012345750000001", field16, "-1234.567890123458", DfpField.RoundingMode.ROUND_HALF_DOWN);
1989 checkConvert(field24, "-1234.56789012345750000001", field16, "-1234.567890123458", DfpField.RoundingMode.ROUND_HALF_EVEN);
1990 checkConvert(field24, "-1234.56789012345750000001", field16, "-1234.567890123458", DfpField.RoundingMode.ROUND_HALF_ODD);
1991 checkConvert(field24, "-1234.56789012345750000001", field16, "-1234.567890123457", DfpField.RoundingMode.ROUND_CEIL);
1992 checkConvert(field24, "-1234.56789012345750000001", field16, "-1234.567890123458", DfpField.RoundingMode.ROUND_FLOOR);
1993
1994 checkConvert(field24, "1234.56789012345650000001", field16, "1234.567890123457", DfpField.RoundingMode.ROUND_HALF_EVEN);
1995 checkConvert(field24, "1234.56789012345600000000", field16, "1234.567890123456", DfpField.RoundingMode.ROUND_HALF_EVEN);
1996 checkConvert(field24, "9999.99999999999950000000", field16, "10000.", DfpField.RoundingMode.ROUND_HALF_EVEN);
1997 checkConvert(field24, "9999.99999999999950000000", field16, "9999.999999999999", DfpField.RoundingMode.ROUND_HALF_ODD);
1998 checkConvert(field24, "9999.99999999999950000001", field16, "10000.", DfpField.RoundingMode.ROUND_HALF_ODD);
1999
2000 Assert.assertTrue(field24.newDfp(-1).sqrt().newInstance(field16, DfpField.RoundingMode.ROUND_HALF_EVEN).isNaN());
2001 Assert.assertTrue(field24.newDfp().reciprocal().newInstance(field16, DfpField.RoundingMode.ROUND_HALF_EVEN).isInfinite());
2002
2003 }
2004
2005 private void checkConvert(DfpField originalField, String originalValue,
2006 DfpField targetField, String targetValue,
2007 DfpField.RoundingMode rmode) {
2008 Dfp original = originalField.newDfp(originalValue);
2009 Dfp converted = original.newInstance(targetField, rmode);
2010 Assert.assertEquals(targetValue, converted.toString());
2011 }
2012
2013 }