1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.util;
23
24 import org.hipparchus.CalculusFieldElement;
25 import org.hipparchus.Field;
26 import org.hipparchus.exception.MathIllegalArgumentException;
27
28
29
30
31
32
33 public class Binary64 extends Number implements CalculusFieldElement<Binary64>, Comparable<Binary64> {
34
35
36 public static final Binary64 ZERO;
37
38
39 public static final Binary64 ONE;
40
41
42 public static final Binary64 PI;
43
44
45
46
47
48 public static final Binary64 NEGATIVE_INFINITY;
49
50
51
52
53
54 public static final Binary64 POSITIVE_INFINITY;
55
56
57 public static final Binary64 NAN;
58
59
60 private static final long serialVersionUID = 20120227L;
61
62 static {
63 ZERO = new Binary64(0d);
64 ONE = new Binary64(1d);
65 PI = new Binary64(FastMath.PI);
66 NEGATIVE_INFINITY = new Binary64(Double.NEGATIVE_INFINITY);
67 POSITIVE_INFINITY = new Binary64(Double.POSITIVE_INFINITY);
68 NAN = new Binary64(Double.NaN);
69 }
70
71
72 private final double value;
73
74
75
76
77
78
79 public Binary64(final double x) {
80 this.value = x;
81 }
82
83
84
85
86
87
88 @Override
89 public Binary64 newInstance(final double v) {
90 return new Binary64(v);
91 }
92
93
94 @Override
95 public Field<Binary64> getField() {
96 return Binary64Field.getInstance();
97 }
98
99
100
101
102
103
104
105
106 @Override
107 public Binary64 add(final Binary64 a) {
108 return new Binary64(this.value + a.value);
109 }
110
111
112
113
114
115
116
117
118 @Override
119 public Binary64 subtract(final Binary64 a) {
120 return new Binary64(this.value - a.value);
121 }
122
123
124
125
126
127
128
129 @Override
130 public Binary64 negate() {
131 return new Binary64(-this.value);
132 }
133
134 @Override
135 public Binary64 square() {
136 return multiply(this);
137 }
138
139
140
141
142
143
144
145
146 @Override
147 public Binary64 multiply(final Binary64 a) {
148 return new Binary64(this.value * a.value);
149 }
150
151
152
153
154
155
156
157 @Override
158 public Binary64 multiply(final int n) {
159 return new Binary64(n * this.value);
160 }
161
162
163
164
165
166
167
168
169
170 @Override
171 public Binary64 divide(final Binary64 a) {
172 return new Binary64(this.value / a.value);
173 }
174
175
176
177
178
179
180
181
182 @Override
183 public Binary64 reciprocal() {
184 return new Binary64(1.0 / this.value);
185 }
186
187
188
189
190
191
192
193
194
195
196 @Override
197 public byte byteValue() {
198 return (byte) value;
199 }
200
201
202
203
204
205
206 @Override
207 public short shortValue() {
208 return (short) value;
209 }
210
211
212
213
214
215
216 @Override
217 public int intValue() {
218 return (int) value;
219 }
220
221
222
223
224
225
226 @Override
227 public long longValue() {
228 return (long) value;
229 }
230
231
232
233
234
235
236 @Override
237 public float floatValue() {
238 return (float) value;
239 }
240
241
242 @Override
243 public double doubleValue() {
244 return value;
245 }
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260 @Override
261 public int compareTo(final Binary64 o) {
262 return Double.compare(this.value, o.value);
263 }
264
265
266
267
268
269
270 @Override
271 public boolean equals(final Object obj) {
272 if (obj instanceof Binary64) {
273 final Binary64 that = (Binary64) obj;
274 return Double.doubleToLongBits(this.value) == Double
275 .doubleToLongBits(that.value);
276 }
277 return false;
278 }
279
280
281
282
283
284
285
286 @Override
287 public boolean isZero() {
288 return value == 0.0;
289 }
290
291
292
293
294
295
296
297
298
299 @Override
300 public int hashCode() {
301 long v = Double.doubleToLongBits(value);
302 return (int) (v ^ (v >>> 32));
303 }
304
305
306
307
308
309
310
311
312
313 @Override
314 public String toString() {
315 return Double.toString(value);
316 }
317
318
319
320
321
322
323
324
325
326
327
328 @Override
329 public boolean isInfinite() {
330 return Double.isInfinite(value);
331 }
332
333
334
335
336
337
338
339 @Override
340 public boolean isNaN() {
341 return Double.isNaN(value);
342 }
343
344
345 @Override
346 public double getReal() {
347 return value;
348 }
349
350
351 @Override
352 public Binary64 add(final double a) {
353 return new Binary64(value + a);
354 }
355
356
357 @Override
358 public Binary64 subtract(final double a) {
359 return new Binary64(value - a);
360 }
361
362
363 @Override
364 public Binary64 multiply(final double a) {
365 return new Binary64(value * a);
366 }
367
368
369 @Override
370 public Binary64 divide(final double a) {
371 return new Binary64(value / a);
372 }
373
374
375 @Override
376 public Binary64 remainder(final double a) {
377 return new Binary64(FastMath.IEEEremainder(value, a));
378 }
379
380
381 @Override
382 public Binary64 remainder(final Binary64 a) {
383 return new Binary64(FastMath.IEEEremainder(value, a.value));
384 }
385
386
387 @Override
388 public Binary64 abs() {
389 return new Binary64(FastMath.abs(value));
390 }
391
392
393 @Override
394 public Binary64 ceil() {
395 return new Binary64(FastMath.ceil(value));
396 }
397
398
399 @Override
400 public Binary64 floor() {
401 return new Binary64(FastMath.floor(value));
402 }
403
404
405 @Override
406 public Binary64 rint() {
407 return new Binary64(FastMath.rint(value));
408 }
409
410
411 @Override
412 public Binary64 sign() {
413 return new Binary64(FastMath.signum(value));
414 }
415
416
417 @Override
418 public Binary64 copySign(final Binary64 sign) {
419 return new Binary64(FastMath.copySign(value, sign.value));
420 }
421
422
423 @Override
424 public Binary64 copySign(final double sign) {
425 return new Binary64(FastMath.copySign(value, sign));
426 }
427
428
429 @Override
430 public Binary64 scalb(final int n) {
431 return new Binary64(FastMath.scalb(value, n));
432 }
433
434
435 @Override
436 public Binary64 ulp() {
437 return new Binary64(FastMath.ulp(value));
438 }
439
440
441 @Override
442 public Binary64 hypot(final Binary64 y) {
443 return new Binary64(FastMath.hypot(value, y.value));
444 }
445
446
447 @Override
448 public Binary64 sqrt() {
449 return new Binary64(FastMath.sqrt(value));
450 }
451
452
453 @Override
454 public Binary64 cbrt() {
455 return new Binary64(FastMath.cbrt(value));
456 }
457
458
459 @Override
460 public Binary64 rootN(final int n) {
461 if (value < 0) {
462 return (n % 2 == 0) ? Binary64.NAN : new Binary64(-FastMath.pow(-value, 1.0 / n));
463 } else {
464 return new Binary64(FastMath.pow(value, 1.0 / n));
465 }
466 }
467
468
469 @Override
470 public Binary64 pow(final double p) {
471 return new Binary64(FastMath.pow(value, p));
472 }
473
474
475 @Override
476 public Binary64 pow(final int n) {
477 return new Binary64(FastMath.pow(value, n));
478 }
479
480
481 @Override
482 public Binary64 pow(final Binary64 e) {
483 return new Binary64(FastMath.pow(value, e.value));
484 }
485
486
487 @Override
488 public Binary64 exp() {
489 return new Binary64(FastMath.exp(value));
490 }
491
492
493 @Override
494 public Binary64 expm1() {
495 return new Binary64(FastMath.expm1(value));
496 }
497
498
499 @Override
500 public Binary64 log() {
501 return new Binary64(FastMath.log(value));
502 }
503
504
505 @Override
506 public Binary64 log1p() {
507 return new Binary64(FastMath.log1p(value));
508 }
509
510
511
512
513 @Override
514 public Binary64 log10() {
515 return new Binary64(FastMath.log10(value));
516 }
517
518
519 @Override
520 public Binary64 cos() {
521 return new Binary64(FastMath.cos(value));
522 }
523
524
525 @Override
526 public Binary64 sin() {
527 return new Binary64(FastMath.sin(value));
528 }
529
530
531 @Override
532 public FieldSinCos<Binary64> sinCos() {
533 final SinCos sc = FastMath.sinCos(value);
534 return new FieldSinCos<>(new Binary64(sc.sin()), new Binary64(sc.cos()));
535 }
536
537
538 @Override
539 public Binary64 tan() {
540 return new Binary64(FastMath.tan(value));
541 }
542
543
544 @Override
545 public Binary64 acos() {
546 return new Binary64(FastMath.acos(value));
547 }
548
549
550 @Override
551 public Binary64 asin() {
552 return new Binary64(FastMath.asin(value));
553 }
554
555
556 @Override
557 public Binary64 atan() {
558 return new Binary64(FastMath.atan(value));
559 }
560
561
562 @Override
563 public Binary64 atan2(final Binary64 x) {
564 return new Binary64(FastMath.atan2(value, x.value));
565 }
566
567
568 @Override
569 public Binary64 cosh() {
570 return new Binary64(FastMath.cosh(value));
571 }
572
573
574 @Override
575 public Binary64 sinh() {
576 return new Binary64(FastMath.sinh(value));
577 }
578
579
580 @Override
581 public FieldSinhCosh<Binary64> sinhCosh() {
582 final SinhCosh sch = FastMath.sinhCosh(value);
583 return new FieldSinhCosh<>(new Binary64(sch.sinh()), new Binary64(sch.cosh()));
584 }
585
586
587 @Override
588 public Binary64 tanh() {
589 return new Binary64(FastMath.tanh(value));
590 }
591
592
593 @Override
594 public Binary64 acosh() {
595 return new Binary64(FastMath.acosh(value));
596 }
597
598
599 @Override
600 public Binary64 asinh() {
601 return new Binary64(FastMath.asinh(value));
602 }
603
604
605 @Override
606 public Binary64 atanh() {
607 return new Binary64(FastMath.atanh(value));
608 }
609
610
611 @Override
612 public Binary64 toDegrees() {
613 return new Binary64(FastMath.toDegrees(value));
614 }
615
616
617 @Override
618 public Binary64 toRadians() {
619 return new Binary64(FastMath.toRadians(value));
620 }
621
622
623 @Override
624 public Binary64 linearCombination(final Binary64[] a, final Binary64[] b)
625 throws MathIllegalArgumentException {
626 MathUtils.checkDimension(a.length, b.length);
627 final double[] aDouble = new double[a.length];
628 final double[] bDouble = new double[b.length];
629 for (int i = 0; i < a.length; ++i) {
630 aDouble[i] = a[i].value;
631 bDouble[i] = b[i].value;
632 }
633 return new Binary64(MathArrays.linearCombination(aDouble, bDouble));
634 }
635
636
637 @Override
638 public Binary64 linearCombination(final double[] a, final Binary64[] b)
639 throws MathIllegalArgumentException {
640 MathUtils.checkDimension(a.length, b.length);
641 final double[] bDouble = new double[b.length];
642 for (int i = 0; i < a.length; ++i) {
643 bDouble[i] = b[i].value;
644 }
645 return new Binary64(MathArrays.linearCombination(a, bDouble));
646 }
647
648
649 @Override
650 public Binary64 linearCombination(final Binary64 a1, final Binary64 b1,
651 final Binary64 a2, final Binary64 b2) {
652 return new Binary64(MathArrays.linearCombination(a1.value, b1.value,
653 a2.value, b2.value));
654 }
655
656
657 @Override
658 public Binary64 linearCombination(final double a1, final Binary64 b1,
659 final double a2, final Binary64 b2) {
660 return new Binary64(MathArrays.linearCombination(a1, b1.value,
661 a2, b2.value));
662 }
663
664
665 @Override
666 public Binary64 linearCombination(final Binary64 a1, final Binary64 b1,
667 final Binary64 a2, final Binary64 b2,
668 final Binary64 a3, final Binary64 b3) {
669 return new Binary64(MathArrays.linearCombination(a1.value, b1.value,
670 a2.value, b2.value,
671 a3.value, b3.value));
672 }
673
674
675 @Override
676 public Binary64 linearCombination(final double a1, final Binary64 b1,
677 final double a2, final Binary64 b2,
678 final double a3, final Binary64 b3) {
679 return new Binary64(MathArrays.linearCombination(a1, b1.value,
680 a2, b2.value,
681 a3, b3.value));
682 }
683
684
685 @Override
686 public Binary64 linearCombination(final Binary64 a1, final Binary64 b1,
687 final Binary64 a2, final Binary64 b2,
688 final Binary64 a3, final Binary64 b3,
689 final Binary64 a4, final Binary64 b4) {
690 return new Binary64(MathArrays.linearCombination(a1.value, b1.value,
691 a2.value, b2.value,
692 a3.value, b3.value,
693 a4.value, b4.value));
694 }
695
696
697 @Override
698 public Binary64 linearCombination(final double a1, final Binary64 b1,
699 final double a2, final Binary64 b2,
700 final double a3, final Binary64 b3,
701 final double a4, final Binary64 b4) {
702 return new Binary64(MathArrays.linearCombination(a1, b1.value,
703 a2, b2.value,
704 a3, b3.value,
705 a4, b4.value));
706 }
707
708
709 @Override
710 public Binary64 getPi() {
711 return PI;
712 }
713
714 }