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.fraction;
23
24 import java.io.Serializable;
25 import java.math.BigDecimal;
26 import java.math.BigInteger;
27 import java.math.RoundingMode;
28 import java.util.function.Function;
29 import java.util.stream.Stream;
30
31 import org.hipparchus.FieldElement;
32 import org.hipparchus.exception.LocalizedCoreFormats;
33 import org.hipparchus.exception.MathIllegalArgumentException;
34 import org.hipparchus.exception.MathIllegalStateException;
35 import org.hipparchus.exception.MathRuntimeException;
36 import org.hipparchus.exception.NullArgumentException;
37 import org.hipparchus.fraction.ConvergentsIterator.ConvergenceStep;
38 import org.hipparchus.util.ArithmeticUtils;
39 import org.hipparchus.util.FastMath;
40 import org.hipparchus.util.MathUtils;
41 import org.hipparchus.util.Pair;
42 import org.hipparchus.util.Precision;
43
44
45
46
47
48
49 public class BigFraction
50 extends Number
51 implements FieldElement<BigFraction>, Comparable<BigFraction>, Serializable {
52
53
54 public static final BigFraction TWO = new BigFraction(2);
55
56
57 public static final BigFraction ONE = new BigFraction(1);
58
59
60 public static final BigFraction ZERO = new BigFraction(0);
61
62
63 public static final BigFraction MINUS_ONE = new BigFraction(-1);
64
65
66 public static final BigFraction FOUR_FIFTHS = new BigFraction(4, 5);
67
68
69 public static final BigFraction ONE_FIFTH = new BigFraction(1, 5);
70
71
72 public static final BigFraction ONE_HALF = new BigFraction(1, 2);
73
74
75 public static final BigFraction ONE_QUARTER = new BigFraction(1, 4);
76
77
78 public static final BigFraction ONE_THIRD = new BigFraction(1, 3);
79
80
81 public static final BigFraction THREE_FIFTHS = new BigFraction(3, 5);
82
83
84 public static final BigFraction THREE_QUARTERS = new BigFraction(3, 4);
85
86
87 public static final BigFraction TWO_FIFTHS = new BigFraction(2, 5);
88
89
90 public static final BigFraction TWO_QUARTERS = new BigFraction(2, 4);
91
92
93 public static final BigFraction TWO_THIRDS = new BigFraction(2, 3);
94
95
96 private static final long serialVersionUID = -5630213147331578515L;
97
98
99 private static final BigInteger ONE_HUNDRED = BigInteger.valueOf(100);
100
101
102 private static final Function<ConvergenceStep, BigFraction> STEP_TO_FRACTION =
103 s -> new BigFraction(s.getNumerator(), s.getDenominator());
104
105
106 private final BigInteger numerator;
107
108
109 private final BigInteger denominator;
110
111
112
113
114
115
116
117
118
119
120 public BigFraction(final BigInteger num) {
121 this(num, BigInteger.ONE);
122 }
123
124
125
126
127
128
129
130
131
132
133 public BigFraction(BigInteger num, BigInteger den) {
134 MathUtils.checkNotNull(num, LocalizedCoreFormats.NUMERATOR);
135 MathUtils.checkNotNull(den, LocalizedCoreFormats.DENOMINATOR);
136 if (den.signum() == 0) {
137 throw new MathIllegalArgumentException(LocalizedCoreFormats.ZERO_DENOMINATOR);
138 }
139 if (num.signum() == 0) {
140 numerator = BigInteger.ZERO;
141 denominator = BigInteger.ONE;
142 } else {
143
144
145 final BigInteger gcd = num.gcd(den);
146 if (BigInteger.ONE.compareTo(gcd) < 0) {
147 num = num.divide(gcd);
148 den = den.divide(gcd);
149 }
150
151
152 if (den.signum() == -1) {
153 num = num.negate();
154 den = den.negate();
155 }
156
157
158 numerator = num;
159 denominator = den;
160
161 }
162 }
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185 public BigFraction(final double value) throws MathIllegalArgumentException {
186 if (Double.isNaN(value)) {
187 throw new MathIllegalArgumentException(LocalizedCoreFormats.NAN_VALUE_CONVERSION);
188 }
189 if (Double.isInfinite(value)) {
190 throw new MathIllegalArgumentException(LocalizedCoreFormats.INFINITE_VALUE_CONVERSION);
191 }
192
193
194 final long bits = Double.doubleToLongBits(value);
195 final long sign = bits & 0x8000000000000000L;
196 final long exponent = bits & 0x7ff0000000000000L;
197 long m = bits & 0x000fffffffffffffL;
198 if (exponent != 0) {
199
200 m |= 0x0010000000000000L;
201 }
202 if (sign != 0) {
203 m = -m;
204 }
205 int k = ((int) (exponent >> 52)) - 1075;
206 while (((m & 0x001ffffffffffffeL) != 0) && ((m & 0x1) == 0)) {
207 m >>= 1;
208 ++k;
209 }
210
211 if (k < 0) {
212 numerator = BigInteger.valueOf(m);
213 denominator = BigInteger.ZERO.flipBit(-k);
214 } else {
215 numerator = BigInteger.valueOf(m).multiply(BigInteger.ZERO.flipBit(k));
216 denominator = BigInteger.ONE;
217 }
218
219 }
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240 public BigFraction(final double value, final double epsilon,
241 final int maxIterations)
242 throws MathIllegalStateException {
243 ConvergenceStep converged = ConvergentsIterator.convergent(value, maxIterations, s -> {
244 final double quotient = s.getFractionValue();
245 return Precision.equals(quotient, value, 1) || FastMath.abs(quotient - value) < epsilon;
246 }).getKey();
247 if (FastMath.abs(converged.getFractionValue() - value) < epsilon) {
248 this.numerator = BigInteger.valueOf(converged.getNumerator());
249 this.denominator = BigInteger.valueOf(converged.getDenominator());
250 } else {
251 throw new MathIllegalStateException(LocalizedCoreFormats.FAILED_FRACTION_CONVERSION,
252 value, maxIterations);
253 }
254 }
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271 public BigFraction(final double value, final long maxDenominator)
272 throws MathIllegalStateException {
273 final int maxIterations = 100;
274 ConvergenceStep[] lastValid = new ConvergenceStep[1];
275 ConvergentsIterator.convergent(value, maxIterations, s -> {
276 if (s.getDenominator() < maxDenominator) {
277 lastValid[0] = s;
278 }
279 return Precision.equals(s.getFractionValue(), value, 1);
280 });
281 if (lastValid[0] != null) {
282 this.numerator = BigInteger.valueOf(lastValid[0].getNumerator());
283 this.denominator = BigInteger.valueOf(lastValid[0].getDenominator());
284 } else {
285 throw new MathIllegalStateException(LocalizedCoreFormats.FAILED_FRACTION_CONVERSION,
286 value, maxIterations);
287 }
288 }
289
290
291
292
293
294
295
296
297
298
299 public BigFraction(final int num) {
300 this(BigInteger.valueOf(num), BigInteger.ONE);
301 }
302
303
304
305
306
307
308
309
310
311
312
313
314 public BigFraction(final int num, final int den) {
315 this(BigInteger.valueOf(num), BigInteger.valueOf(den));
316 }
317
318
319
320
321
322
323
324
325
326 public BigFraction(final long num) {
327 this(BigInteger.valueOf(num), BigInteger.ONE);
328 }
329
330
331
332
333
334
335
336
337
338
339
340
341 public BigFraction(final long num, final long den) {
342 this(BigInteger.valueOf(num), BigInteger.valueOf(den));
343 }
344
345
346
347
348 @FunctionalInterface
349 public interface ConvergenceTest {
350
351
352
353
354
355
356
357
358 boolean test(long numerator, long denominator);
359 }
360
361
362
363
364
365
366
367 public static Stream<BigFraction> convergents(final double value, final int maxConvergents) {
368 return ConvergentsIterator.convergents(value, maxConvergents).map(STEP_TO_FRACTION);
369 }
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392 public static Pair<BigFraction, Boolean> convergent(double value, int maxConvergents,
393 ConvergenceTest convergenceTest) {
394 Pair<ConvergenceStep, Boolean> converged = ConvergentsIterator.convergent(value, maxConvergents,
395 s -> convergenceTest.test(s.getNumerator(), s.getDenominator()));
396 return Pair.create(STEP_TO_FRACTION.apply(converged.getKey()), converged.getValue());
397 }
398
399
400 @Override
401 public double getReal() {
402 return doubleValue();
403 }
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424 public static BigFraction getReducedFraction(final int numerator,
425 final int denominator) {
426 if (numerator == 0) {
427 return ZERO;
428 }
429
430 return new BigFraction(numerator, denominator);
431 }
432
433
434
435
436
437
438
439
440 public BigFraction abs() {
441 return (numerator.signum() == 1) ? this : negate();
442 }
443
444
445
446
447 public boolean isInteger() {
448 return denominator.equals(BigInteger.ONE);
449 }
450
451
452
453
454
455
456
457
458
459 public int signum() {
460 return numerator.signum();
461 }
462
463
464
465
466
467
468
469
470
471
472
473
474
475 public BigFraction add(final BigInteger bg) throws NullArgumentException {
476 MathUtils.checkNotNull(bg);
477
478 if (numerator.signum() == 0) {
479 return new BigFraction(bg);
480 }
481 if (bg.signum() == 0) {
482 return this;
483 }
484
485 return new BigFraction(numerator.add(denominator.multiply(bg)), denominator);
486 }
487
488
489
490
491
492
493
494
495
496
497
498 public BigFraction add(final int i) {
499 return add(BigInteger.valueOf(i));
500 }
501
502
503
504
505
506
507
508
509
510
511
512 public BigFraction add(final long l) {
513 return add(BigInteger.valueOf(l));
514 }
515
516
517
518
519
520
521
522
523
524
525
526
527 @Override
528 public BigFraction add(final BigFraction fraction) {
529 MathUtils.checkNotNull(fraction, LocalizedCoreFormats.FRACTION);
530 if (fraction.numerator.signum() == 0) {
531 return this;
532 }
533 if (numerator.signum() == 0) {
534 return fraction;
535 }
536
537 BigInteger num;
538 BigInteger den;
539 if (denominator.equals(fraction.denominator)) {
540 num = numerator.add(fraction.numerator);
541 den = denominator;
542 } else {
543 num = (numerator.multiply(fraction.denominator)).add((fraction.numerator).multiply(denominator));
544 den = denominator.multiply(fraction.denominator);
545 }
546
547 if (num.signum() == 0) {
548 return ZERO;
549 }
550
551 return new BigFraction(num, den);
552
553 }
554
555
556
557
558
559
560
561
562
563
564
565
566
567 public BigDecimal bigDecimalValue() {
568 return new BigDecimal(numerator).divide(new BigDecimal(denominator));
569 }
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586 public BigDecimal bigDecimalValue(final RoundingMode roundingMode) {
587 return new BigDecimal(numerator).divide(new BigDecimal(denominator), roundingMode);
588 }
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605 public BigDecimal bigDecimalValue(final int scale, final RoundingMode roundingMode) {
606 return new BigDecimal(numerator).divide(new BigDecimal(denominator), scale, roundingMode);
607 }
608
609
610
611
612
613
614
615
616
617
618
619
620 @Override
621 public int compareTo(final BigFraction object) {
622 int lhsSigNum = numerator.signum();
623 int rhsSigNum = object.numerator.signum();
624
625 if (lhsSigNum != rhsSigNum) {
626 return (lhsSigNum > rhsSigNum) ? 1 : -1;
627 }
628 if (lhsSigNum == 0) {
629 return 0;
630 }
631
632 BigInteger nOd = numerator.multiply(object.denominator);
633 BigInteger dOn = denominator.multiply(object.numerator);
634 return nOd.compareTo(dOn);
635 }
636
637
638
639
640
641
642
643
644
645
646
647
648 public BigFraction divide(final BigInteger bg) {
649 MathUtils.checkNotNull(bg);
650 if (bg.signum() == 0) {
651 throw new MathRuntimeException(LocalizedCoreFormats.ZERO_DENOMINATOR);
652 }
653 if (numerator.signum() == 0) {
654 return ZERO;
655 }
656 return new BigFraction(numerator, denominator.multiply(bg));
657 }
658
659
660
661
662
663
664
665
666
667
668
669 public BigFraction divide(final int i) {
670 return divide(BigInteger.valueOf(i));
671 }
672
673
674
675
676
677
678
679
680
681
682
683 public BigFraction divide(final long l) {
684 return divide(BigInteger.valueOf(l));
685 }
686
687
688
689
690
691
692
693
694
695
696
697
698 @Override
699 public BigFraction divide(final BigFraction fraction) {
700 MathUtils.checkNotNull(fraction, LocalizedCoreFormats.FRACTION);
701 if (fraction.numerator.signum() == 0) {
702 throw new MathRuntimeException(LocalizedCoreFormats.ZERO_DENOMINATOR);
703 }
704 if (numerator.signum() == 0) {
705 return ZERO;
706 }
707
708 return multiply(fraction.reciprocal());
709 }
710
711
712
713
714
715
716
717
718
719
720 @Override
721 public double doubleValue() {
722 double result = numerator.doubleValue() / denominator.doubleValue();
723 if (Double.isInfinite(result) || Double.isNaN(result)) {
724
725
726 int shift = FastMath.max(numerator.bitLength(),
727 denominator.bitLength()) - FastMath.getExponent(Double.MAX_VALUE);
728 result = numerator.shiftRight(shift).doubleValue() /
729 denominator.shiftRight(shift).doubleValue();
730 }
731 return result;
732 }
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749 @Override
750 public boolean equals(final Object other) {
751 boolean ret = false;
752
753 if (this == other) {
754 ret = true;
755 } else if (other instanceof BigFraction) {
756 BigFraction rhs = (BigFraction) other;
757 ret = numerator.equals(rhs.numerator) && denominator.equals(rhs.denominator);
758 }
759
760 return ret;
761 }
762
763
764
765
766
767
768
769
770
771
772 @Override
773 public float floatValue() {
774 float result = numerator.floatValue() / denominator.floatValue();
775 if (Double.isNaN(result)) {
776
777
778 int shift = FastMath.max(numerator.bitLength(),
779 denominator.bitLength()) - FastMath.getExponent(Float.MAX_VALUE);
780 result = numerator.shiftRight(shift).floatValue() /
781 denominator.shiftRight(shift).floatValue();
782 }
783 return result;
784 }
785
786
787
788
789
790
791
792
793
794 private static BigInteger lcm(final BigInteger i0, final BigInteger i1) {
795 if (i0.signum() == 0 && i1.signum() == 0) {
796 return BigInteger.ZERO;
797 }
798 BigInteger a = i0.abs();
799 BigInteger b = i1.abs();
800 BigInteger gcd = i0.gcd(b);
801 return (a.multiply(b)).divide(gcd);
802 }
803
804
805
806
807
808
809
810
811 public BigFraction gcd(BigFraction s) {
812 if (s.isZero()) {
813 return this;
814 }
815 if (this.isZero()) {
816 return s;
817 }
818 BigInteger p = numerator.gcd(s.numerator);
819 BigInteger q = lcm(denominator, s.denominator);
820 return new BigFraction(p, q);
821 }
822
823
824
825
826
827
828
829
830 public BigFraction lcm(BigFraction s) {
831 if (s.isZero()) {
832 return ZERO;
833 }
834 if (this.isZero()) {
835 return ZERO;
836 }
837 return new BigFraction(lcm(numerator, s.numerator), denominator.gcd(s.denominator));
838 }
839
840
841
842
843
844
845
846
847 public BigInteger getDenominator() {
848 return denominator;
849 }
850
851
852
853
854
855
856
857
858 public int getDenominatorAsInt() {
859 return denominator.intValue();
860 }
861
862
863
864
865
866
867
868
869 public long getDenominatorAsLong() {
870 return denominator.longValue();
871 }
872
873
874
875
876
877
878
879
880 public BigInteger getNumerator() {
881 return numerator;
882 }
883
884
885
886
887
888
889
890
891 public int getNumeratorAsInt() {
892 return numerator.intValue();
893 }
894
895
896
897
898
899
900
901
902 public long getNumeratorAsLong() {
903 return numerator.longValue();
904 }
905
906
907
908
909
910
911
912
913
914 @Override
915 public int hashCode() {
916 return 37 * (37 * 17 + numerator.hashCode()) + denominator.hashCode();
917 }
918
919
920
921
922
923
924
925
926
927
928 @Override
929 public int intValue() {
930 return numerator.divide(denominator).intValue();
931 }
932
933
934
935
936
937
938
939
940
941
942 @Override
943 public long longValue() {
944 return numerator.divide(denominator).longValue();
945 }
946
947
948
949
950
951
952
953
954
955
956
957 public BigFraction multiply(final BigInteger bg) {
958 MathUtils.checkNotNull(bg);
959 if (numerator.signum() == 0 || bg.signum() == 0) {
960 return ZERO;
961 }
962 return new BigFraction(bg.multiply(numerator), denominator);
963 }
964
965
966
967
968
969
970
971
972
973
974
975 @Override
976 public BigFraction multiply(final int i) {
977 if (i == 0 || numerator.signum() == 0) {
978 return ZERO;
979 }
980
981 return multiply(BigInteger.valueOf(i));
982 }
983
984
985
986
987
988
989
990
991
992
993
994 public BigFraction multiply(final long l) {
995 if (l == 0 || numerator.signum() == 0) {
996 return ZERO;
997 }
998
999 return multiply(BigInteger.valueOf(l));
1000 }
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012 @Override
1013 public BigFraction multiply(final BigFraction fraction) {
1014 MathUtils.checkNotNull(fraction, LocalizedCoreFormats.FRACTION);
1015 if (numerator.signum() == 0 ||
1016 fraction.numerator.signum() == 0) {
1017 return ZERO;
1018 }
1019 return new BigFraction(numerator.multiply(fraction.numerator),
1020 denominator.multiply(fraction.denominator));
1021 }
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031 @Override
1032 public BigFraction negate() {
1033 return new BigFraction(numerator.negate(), denominator);
1034 }
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044 public double percentageValue() {
1045 return multiply(ONE_HUNDRED).doubleValue();
1046 }
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059 public BigFraction pow(final int exponent) {
1060 if (exponent == 0) {
1061 return ONE;
1062 }
1063 if (numerator.signum() == 0) {
1064 return this;
1065 }
1066
1067 if (exponent < 0) {
1068 return new BigFraction(denominator.pow(-exponent), numerator.pow(-exponent));
1069 }
1070 return new BigFraction(numerator.pow(exponent), denominator.pow(exponent));
1071 }
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083 public BigFraction pow(final long exponent) {
1084 if (exponent == 0) {
1085 return ONE;
1086 }
1087 if (numerator.signum() == 0) {
1088 return this;
1089 }
1090
1091 if (exponent < 0) {
1092 return new BigFraction(ArithmeticUtils.pow(denominator, -exponent),
1093 ArithmeticUtils.pow(numerator, -exponent));
1094 }
1095 return new BigFraction(ArithmeticUtils.pow(numerator, exponent),
1096 ArithmeticUtils.pow(denominator, exponent));
1097 }
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109 public BigFraction pow(final BigInteger exponent) {
1110 if (exponent.signum() == 0) {
1111 return ONE;
1112 }
1113 if (numerator.signum() == 0) {
1114 return this;
1115 }
1116
1117 if (exponent.signum() == -1) {
1118 final BigInteger eNeg = exponent.negate();
1119 return new BigFraction(ArithmeticUtils.pow(denominator, eNeg),
1120 ArithmeticUtils.pow(numerator, eNeg));
1121 }
1122 return new BigFraction(ArithmeticUtils.pow(numerator, exponent),
1123 ArithmeticUtils.pow(denominator, exponent));
1124 }
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136 public double pow(final double exponent) {
1137 return FastMath.pow(numerator.doubleValue(), exponent) /
1138 FastMath.pow(denominator.doubleValue(), exponent);
1139 }
1140
1141
1142
1143
1144
1145
1146
1147
1148 @Override
1149 public BigFraction reciprocal() {
1150 return new BigFraction(denominator, numerator);
1151 }
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161 public BigFraction reduce() {
1162 final BigInteger gcd = numerator.gcd(denominator);
1163
1164 if (BigInteger.ONE.compareTo(gcd) < 0) {
1165 return new BigFraction(numerator.divide(gcd), denominator.divide(gcd));
1166 } else {
1167 return this;
1168 }
1169 }
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181 public BigFraction subtract(final BigInteger bg) {
1182 MathUtils.checkNotNull(bg);
1183 if (bg.signum() == 0) {
1184 return this;
1185 }
1186 if (numerator.signum() == 0) {
1187 return new BigFraction(bg.negate());
1188 }
1189
1190 return new BigFraction(numerator.subtract(denominator.multiply(bg)), denominator);
1191 }
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202 public BigFraction subtract(final int i) {
1203 return subtract(BigInteger.valueOf(i));
1204 }
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215 public BigFraction subtract(final long l) {
1216 return subtract(BigInteger.valueOf(l));
1217 }
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229 @Override
1230 public BigFraction subtract(final BigFraction fraction) {
1231 MathUtils.checkNotNull(fraction, LocalizedCoreFormats.FRACTION);
1232 if (fraction.numerator.signum() == 0) {
1233 return this;
1234 }
1235 if (numerator.signum() == 0) {
1236 return fraction.negate();
1237 }
1238
1239 BigInteger num;
1240 BigInteger den;
1241 if (denominator.equals(fraction.denominator)) {
1242 num = numerator.subtract(fraction.numerator);
1243 den = denominator;
1244 } else {
1245 num = (numerator.multiply(fraction.denominator)).subtract((fraction.numerator).multiply(denominator));
1246 den = denominator.multiply(fraction.denominator);
1247 }
1248 return new BigFraction(num, den);
1249
1250 }
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261 @Override
1262 public String toString() {
1263 if (BigInteger.ONE.equals(denominator)) {
1264 return numerator.toString();
1265 } else if (BigInteger.ZERO.equals(numerator)) {
1266 return "0";
1267 } else {
1268 return numerator + " / " + denominator;
1269 }
1270 }
1271
1272
1273 @Override
1274 public BigFractionField getField() {
1275 return BigFractionField.getInstance();
1276 }
1277
1278 }