-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigFraction.java
More file actions
169 lines (133 loc) · 5.43 KB
/
BigFraction.java
File metadata and controls
169 lines (133 loc) · 5.43 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
import java.math.BigInteger;
final class BigFraction extends Number implements Comparable<BigFraction> {
private BigInteger numerator;
private BigInteger denominator;
public final static BigFraction ZERO = new BigFraction(BigInteger.ZERO);
public BigFraction(String numerator, String denominator, boolean reduce) {
this(new BigInteger(numerator), new BigInteger(denominator));
if (reduce)
this.reduce();
}
public BigFraction(long numerator, long denominator) {
this(BigInteger.valueOf(numerator), BigInteger.valueOf(denominator));
}
public BigFraction(String numerator, String denominator) {
this(new BigInteger(numerator), new BigInteger(denominator));
}
public BigFraction(BigInteger numerator, BigInteger denominator) {
if (denominator.signum() == 0)
throw new IllegalArgumentException("Denominator can't be zero");
if (numerator.signum() == 0) {
denominator = BigInteger.ONE;
}
if (denominator.signum() == -1) {
numerator = numerator.multiply(BigInteger.valueOf(-1));
denominator = denominator.multiply(BigInteger.valueOf(-1));
}
this.numerator = numerator;
this.denominator = denominator;
}
public void reduce() {
if (!this.numerator.equals(BigInteger.ZERO)) {
BigInteger GCF = (GCF(this.numerator, this.denominator)).abs();
this.numerator = this.numerator.divide(GCF);
this.denominator = this.denominator.divide(GCF);
}
}
public BigFraction(BigInteger numerator) {
this.numerator = numerator;
this.denominator = BigInteger.ONE;
}
public static BigFraction valueOf(long numerator, long denominator) {
return new BigFraction(BigInteger.valueOf(numerator), BigInteger.valueOf(denominator));
}
public static BigFraction valueOf(long numerator) {
return new BigFraction(BigInteger.valueOf(numerator));
}
public BigFraction multiply(BigFraction frac) {
BigInteger newNumerator = this.getNumerator().multiply(frac.getNumerator());
BigInteger newDenominator = this.getDenominator().multiply(frac.getDenominator());
return new BigFraction(newNumerator, newDenominator);
}
public BigFraction divide(BigFraction frac) {
return this.multiply(new BigFraction(frac.getDenominator(), frac.getNumerator()));
}
public BigFraction add(BigFraction frac) {
BigInteger LCM = LCM(frac);
BigInteger fracNum = (LCM.divide(frac.getDenominator())).multiply(frac.getNumerator());
BigInteger thisNum = (LCM.divide(this.getDenominator())).multiply(this.getNumerator());
return new BigFraction(thisNum.add(fracNum), LCM);
}
public BigFraction subtract(BigFraction frac) {
BigInteger LCM = LCM(frac);
BigInteger fracNum = (LCM.divide(frac.getDenominator())).multiply(frac.getNumerator());
BigInteger thisNum = (LCM.divide(this.getDenominator())).multiply(this.getNumerator());
return new BigFraction(thisNum.subtract(fracNum), LCM);
}
public String toString() {
return this.getNumerator() + "/" + this.getDenominator();
}
public BigInteger getNumerator() {
return this.numerator;
}
public BigInteger getDenominator() {
return this.denominator;
}
@Override
public int intValue() {
return (int) this.doubleValue();
}
@Override
public long longValue() {
return (long) this.doubleValue();
}
@Override
public float floatValue() {
return (float) this.doubleValue();
}
@Override
public double doubleValue() {
return (this.numerator).doubleValue() / (this.denominator).doubleValue();
}
public boolean equals(BigFraction frac) {
return this.compareTo(frac) == 0;
}
@Override
public int compareTo(BigFraction frac) {
BigInteger t = this.getNumerator().multiply(frac.getDenominator());
BigInteger f = frac.getNumerator().multiply(this.getDenominator());
int result = 0;
if (t.max(f).equals(t))
return 1;
if (f.max(t).equals(f))
return -1;
return result;
}
public BigInteger LCM(BigFraction frac) {
return (frac.getDenominator().multiply(this.getDenominator()).abs()).divide(GCF(frac.getDenominator(), this.getDenominator()));
}
public boolean isReduced() {
BigInteger tempDenominator = numerator.min(denominator);
BigInteger tempNumerator = numerator.max(denominator);
BigInteger remainder = tempDenominator.mod(tempNumerator);
while (!remainder.equals(BigInteger.ZERO)) {
remainder = tempDenominator.mod(tempNumerator);
tempDenominator = tempNumerator;
tempNumerator = remainder;
}
return tempDenominator.equals(BigInteger.ONE);
}
private BigInteger GCF(BigInteger numerator, BigInteger denominator) {
BigInteger tempDenominator = numerator.min(denominator);
BigInteger tempNumerator = numerator.max(denominator);
numerator = tempNumerator;
denominator = tempDenominator;
BigInteger remainder = denominator.mod(numerator);
while (!remainder.equals(BigInteger.ZERO)) {
remainder = denominator.mod(numerator);
denominator = numerator;
numerator = remainder;
}
return denominator;
}
}