Fraction.java
14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
package org.apache.commons.lang3.math;
import java.math.BigInteger;
public final class Fraction
extends Number
implements Comparable<Fraction>
{
public static final Fraction FOUR_FIFTHS = new Fraction(4, 5);
public static final Fraction ONE;
public static final Fraction ONE_FIFTH;
public static final Fraction ONE_HALF;
public static final Fraction ONE_QUARTER;
public static final Fraction ONE_THIRD;
public static final Fraction THREE_FIFTHS;
public static final Fraction THREE_QUARTERS;
public static final Fraction TWO_FIFTHS;
public static final Fraction TWO_QUARTERS;
public static final Fraction TWO_THIRDS;
public static final Fraction ZERO = new Fraction(0, 1);
private static final long serialVersionUID = 65382027393090L;
private final int a;
private final int b;
private transient int c = 0;
private transient String d = null;
private transient String e = null;
static
{
ONE = new Fraction(1, 1);
ONE_HALF = new Fraction(1, 2);
ONE_THIRD = new Fraction(1, 3);
TWO_THIRDS = new Fraction(2, 3);
ONE_QUARTER = new Fraction(1, 4);
TWO_QUARTERS = new Fraction(2, 4);
THREE_QUARTERS = new Fraction(3, 4);
ONE_FIFTH = new Fraction(1, 5);
TWO_FIFTHS = new Fraction(2, 5);
THREE_FIFTHS = new Fraction(3, 5);
}
private Fraction(int paramInt1, int paramInt2)
{
this.a = paramInt1;
this.b = paramInt2;
}
private static int a(int paramInt1, int paramInt2)
{
if ((paramInt1 == 0) || (paramInt2 == 0))
{
if ((paramInt1 == Integer.MIN_VALUE) || (paramInt2 == Integer.MIN_VALUE)) {
throw new ArithmeticException("overflow: gcd is 2^31");
}
return Math.abs(paramInt1) + Math.abs(paramInt2);
}
if ((Math.abs(paramInt1) == 1) || (Math.abs(paramInt2) == 1)) {
return 1;
}
if (paramInt1 > 0) {
paramInt1 = -paramInt1;
}
for (;;)
{
int i = paramInt2;
if (paramInt2 > 0) {
i = -paramInt2;
}
int j = 0;
paramInt2 = paramInt1;
paramInt1 = i;
while (((paramInt2 & 0x1) == 0) && ((paramInt1 & 0x1) == 0) && (j < 31))
{
paramInt2 /= 2;
paramInt1 /= 2;
j += 1;
}
if (j == 31) {
throw new ArithmeticException("overflow: gcd is 2^31");
}
if ((paramInt2 & 0x1) == 1) {
i = paramInt1;
}
while ((paramInt1 & 0x1) == 0)
{
paramInt1 /= 2;
continue;
k = -(paramInt2 / 2);
i = paramInt1;
paramInt1 = k;
}
if (paramInt1 > 0) {}
for (int k = -paramInt1;; k = paramInt2)
{
int m = (i - k) / 2;
paramInt1 = m;
paramInt2 = k;
if (m != 0) {
break;
}
return -k * (1 << j);
i = paramInt1;
}
}
}
private Fraction a(Fraction paramFraction, boolean paramBoolean)
{
if (paramFraction == null) {
throw new IllegalArgumentException("The fraction must not be null");
}
if (this.a == 0)
{
if (paramBoolean) {
return paramFraction;
}
return paramFraction.negate();
}
if (paramFraction.a == 0) {
return this;
}
int j = a(this.b, paramFraction.b);
if (j == 1)
{
i = b(this.a, paramFraction.b);
j = b(paramFraction.a, this.b);
long l;
if (paramBoolean)
{
l = i;
l = j + l;
if ((l < -2147483648L) || (l > 2147483647L)) {
throw new ArithmeticException("overflow: add");
}
}
for (i = (int)l;; i = (int)l)
{
return new Fraction(i, c(this.b, paramFraction.b));
l = i - j;
if ((l < -2147483648L) || (l > 2147483647L)) {
throw new ArithmeticException("overflow: add");
}
}
}
BigInteger localBigInteger1 = BigInteger.valueOf(this.a).multiply(BigInteger.valueOf(paramFraction.b / j));
BigInteger localBigInteger2 = BigInteger.valueOf(paramFraction.a).multiply(BigInteger.valueOf(this.b / j));
if (paramBoolean)
{
localBigInteger1 = localBigInteger1.add(localBigInteger2);
i = localBigInteger1.mod(BigInteger.valueOf(j)).intValue();
if (i != 0) {
break label323;
}
}
label323:
for (int i = j;; i = a(i, j))
{
localBigInteger1 = localBigInteger1.divide(BigInteger.valueOf(i));
if (localBigInteger1.bitLength() <= 31) {
break label333;
}
throw new ArithmeticException("overflow: numerator too large after multiply");
localBigInteger1 = localBigInteger1.subtract(localBigInteger2);
break;
}
label333:
return new Fraction(localBigInteger1.intValue(), c(this.b / j, paramFraction.b / i));
}
private static int b(int paramInt1, int paramInt2)
{
long l = paramInt1 * paramInt2;
if ((l < -2147483648L) || (l > 2147483647L)) {
throw new ArithmeticException("overflow: mul");
}
return (int)l;
}
private static int c(int paramInt1, int paramInt2)
{
long l = paramInt1 * paramInt2;
if (l > 2147483647L) {
throw new ArithmeticException("overflow: mulPos");
}
return (int)l;
}
public static Fraction getFraction(double paramDouble)
{
if (paramDouble < 0.0D) {}
for (int k = -1;; k = 1)
{
paramDouble = Math.abs(paramDouble);
if ((paramDouble <= 2.147483647E9D) && (!Double.isNaN(paramDouble))) {
break;
}
throw new ArithmeticException("The value must not be greater than Integer.MAX_VALUE or NaN");
}
int i5 = (int)paramDouble;
double d4 = paramDouble - i5;
int i1 = (int)d4;
paramDouble = i1;
double d2 = Double.MAX_VALUE;
int m = 1;
int n = 1;
int i2 = 0;
int j = 0;
int i = 1;
double d1 = 1.0D;
paramDouble = d4 - paramDouble;
for (;;)
{
double d3 = d1;
int i4 = (int)(d3 / paramDouble);
double d5 = i4;
int i3 = i2 + i1 * i;
i1 = i1 * j + n;
d1 = Math.abs(d4 - i3 / i1);
m += 1;
if ((d2 <= d1) || (i1 > 10000) || (i1 <= 0) || (m >= 25))
{
if (m == 25) {
throw new ArithmeticException("Unable to convert double to fraction");
}
return getReducedFraction(k * (i5 * j + i), j);
}
d2 = d1;
n = j;
d1 = paramDouble;
j = i1;
i1 = i4;
paramDouble = d3 - d5 * paramDouble;
i2 = i;
i = i3;
}
}
public static Fraction getFraction(int paramInt1, int paramInt2)
{
if (paramInt2 == 0) {
throw new ArithmeticException("The denominator must not be zero");
}
int j = paramInt1;
int i = paramInt2;
if (paramInt2 < 0)
{
if ((paramInt1 == Integer.MIN_VALUE) || (paramInt2 == Integer.MIN_VALUE)) {
throw new ArithmeticException("overflow: can't negate");
}
j = -paramInt1;
i = -paramInt2;
}
return new Fraction(j, i);
}
public static Fraction getFraction(int paramInt1, int paramInt2, int paramInt3)
{
if (paramInt3 == 0) {
throw new ArithmeticException("The denominator must not be zero");
}
if (paramInt3 < 0) {
throw new ArithmeticException("The denominator must not be negative");
}
if (paramInt2 < 0) {
throw new ArithmeticException("The numerator must not be negative");
}
if (paramInt1 < 0) {}
for (long l = paramInt1 * paramInt3 - paramInt2; (l < -2147483648L) || (l > 2147483647L); l = paramInt1 * paramInt3 + paramInt2) {
throw new ArithmeticException("Numerator too large to represent as an Integer.");
}
return new Fraction((int)l, paramInt3);
}
public static Fraction getFraction(String paramString)
{
if (paramString == null) {
throw new IllegalArgumentException("The string must not be null");
}
if (paramString.indexOf('.') >= 0) {
return getFraction(Double.parseDouble(paramString));
}
int j = paramString.indexOf(' ');
if (j > 0)
{
i = Integer.parseInt(paramString.substring(0, j));
paramString = paramString.substring(j + 1);
j = paramString.indexOf('/');
if (j < 0) {
throw new NumberFormatException("The fraction could not be parsed as the format X Y/Z");
}
return getFraction(i, Integer.parseInt(paramString.substring(0, j)), Integer.parseInt(paramString.substring(j + 1)));
}
int i = paramString.indexOf('/');
if (i < 0) {
return getFraction(Integer.parseInt(paramString), 1);
}
return getFraction(Integer.parseInt(paramString.substring(0, i)), Integer.parseInt(paramString.substring(i + 1)));
}
public static Fraction getReducedFraction(int paramInt1, int paramInt2)
{
if (paramInt2 == 0) {
throw new ArithmeticException("The denominator must not be zero");
}
if (paramInt1 == 0) {
return ZERO;
}
int i;
if ((paramInt2 == Integer.MIN_VALUE) && ((paramInt1 & 0x1) == 0))
{
i = paramInt1 / 2;
paramInt1 = paramInt2 / 2;
}
for (paramInt2 = i;; paramInt2 = i)
{
int j = paramInt1;
i = paramInt2;
if (paramInt1 < 0)
{
if ((paramInt2 == Integer.MIN_VALUE) || (paramInt1 == Integer.MIN_VALUE)) {
throw new ArithmeticException("overflow: can't negate");
}
i = -paramInt2;
j = -paramInt1;
}
paramInt1 = a(i, j);
return new Fraction(i / paramInt1, j / paramInt1);
i = paramInt1;
paramInt1 = paramInt2;
}
}
public final Fraction abs()
{
if (this.a >= 0) {
return this;
}
return negate();
}
public final Fraction add(Fraction paramFraction)
{
return a(paramFraction, true);
}
public final int compareTo(Fraction paramFraction)
{
if (this == paramFraction) {}
long l1;
long l2;
do
{
do
{
return 0;
} while ((this.a == paramFraction.a) && (this.b == paramFraction.b));
l1 = this.a * paramFraction.b;
l2 = paramFraction.a * this.b;
} while (l1 == l2);
if (l1 < l2) {
return -1;
}
return 1;
}
public final Fraction divideBy(Fraction paramFraction)
{
if (paramFraction == null) {
throw new IllegalArgumentException("The fraction must not be null");
}
if (paramFraction.a == 0) {
throw new ArithmeticException("The fraction to divide by must not be zero");
}
return multiplyBy(paramFraction.invert());
}
public final double doubleValue()
{
return this.a / this.b;
}
public final boolean equals(Object paramObject)
{
if (paramObject == this) {}
do
{
return true;
if (!(paramObject instanceof Fraction)) {
return false;
}
paramObject = (Fraction)paramObject;
} while ((getNumerator() == ((Fraction)paramObject).getNumerator()) && (getDenominator() == ((Fraction)paramObject).getDenominator()));
return false;
}
public final float floatValue()
{
return this.a / this.b;
}
public final int getDenominator()
{
return this.b;
}
public final int getNumerator()
{
return this.a;
}
public final int getProperNumerator()
{
return Math.abs(this.a % this.b);
}
public final int getProperWhole()
{
return this.a / this.b;
}
public final int hashCode()
{
if (this.c == 0) {
this.c = ((getNumerator() + 629) * 37 + getDenominator());
}
return this.c;
}
public final int intValue()
{
return this.a / this.b;
}
public final Fraction invert()
{
if (this.a == 0) {
throw new ArithmeticException("Unable to invert zero.");
}
if (this.a == Integer.MIN_VALUE) {
throw new ArithmeticException("overflow: can't negate numerator");
}
if (this.a < 0) {
return new Fraction(-this.b, -this.a);
}
return new Fraction(this.b, this.a);
}
public final long longValue()
{
return this.a / this.b;
}
public final Fraction multiplyBy(Fraction paramFraction)
{
if (paramFraction == null) {
throw new IllegalArgumentException("The fraction must not be null");
}
if ((this.a == 0) || (paramFraction.a == 0)) {
return ZERO;
}
int i = a(this.a, paramFraction.b);
int j = a(paramFraction.a, this.b);
return getReducedFraction(b(this.a / i, paramFraction.a / j), c(this.b / j, paramFraction.b / i));
}
public final Fraction negate()
{
if (this.a == Integer.MIN_VALUE) {
throw new ArithmeticException("overflow: too large to negate");
}
return new Fraction(-this.a, this.b);
}
public final Fraction pow(int paramInt)
{
Object localObject = this;
Fraction localFraction;
for (;;)
{
if (paramInt == 1) {
return (Fraction)localObject;
}
if (paramInt == 0) {
return ONE;
}
if (paramInt < 0)
{
if (paramInt == Integer.MIN_VALUE)
{
localObject = ((Fraction)localObject).invert().pow(2);
paramInt = -(paramInt / 2);
}
else
{
localObject = ((Fraction)localObject).invert();
paramInt = -paramInt;
}
}
else
{
localFraction = ((Fraction)localObject).multiplyBy((Fraction)localObject);
if (paramInt % 2 != 0) {
break;
}
paramInt /= 2;
localObject = localFraction;
}
}
return localFraction.pow(paramInt / 2).multiplyBy((Fraction)localObject);
}
public final Fraction reduce()
{
if (this.a == 0) {
if (!equals(ZERO)) {}
}
int i;
do
{
return this;
return ZERO;
i = a(Math.abs(this.a), this.b);
} while (i == 1);
return getFraction(this.a / i, this.b / i);
}
public final Fraction subtract(Fraction paramFraction)
{
return a(paramFraction, false);
}
public final String toProperString()
{
if (this.e == null)
{
if (this.a != 0) {
break label26;
}
this.e = "0";
}
for (;;)
{
return this.e;
label26:
if (this.a == this.b)
{
this.e = "1";
}
else if (this.a == this.b * -1)
{
this.e = "-1";
}
else
{
int i;
if (this.a > 0) {
i = -this.a;
}
for (;;)
{
if (i < -this.b)
{
i = getProperNumerator();
if (i == 0)
{
this.e = Integer.toString(getProperWhole());
break;
i = this.a;
continue;
}
this.e = (getProperWhole() + " " + i + "/" + getDenominator());
break;
}
}
this.e = (getNumerator() + "/" + getDenominator());
}
}
}
public final String toString()
{
if (this.d == null) {
this.d = (getNumerator() + "/" + getDenominator());
}
return this.d;
}
}
/* Location: /home/merong/decompile/hackery-dex2jar.jar!/org/apache/commons/lang3/math/Fraction.class
* Java compiler version: 6 (50.0)
* JD-Core Version: 0.7.1
*/