-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFraction.java
More file actions
149 lines (110 loc) · 4.12 KB
/
Fraction.java
File metadata and controls
149 lines (110 loc) · 4.12 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
package mathExt;
import java.math.BigInteger;
import java.util.Objects;
public class Fraction implements Comparable<Fraction> {
private final BigInteger numerator;
private final BigInteger denominator;
public static final Fraction ZERO = new Fraction(0, 1);
public static final Fraction ONE = new Fraction(1, 1);
public static final Fraction HALF = new Fraction(1, 2);
public static final Fraction QUARTER = new Fraction(1, 4);
private String toStringResult;
public Fraction(int numerator) {
this(numerator, 1);
}
public Fraction(int numerator, int denominator) {
this(new BigInteger(numerator + ""), new BigInteger(denominator + ""));
}
public Fraction(BigInteger numerator, BigInteger denominator) {
if (denominator.equals(BigInteger.ZERO))
throw new IllegalArgumentException("Denominator of a fraction can not be zero.");
if (numerator.equals(BigInteger.ZERO)) {
this.numerator = BigInteger.ZERO;
this.denominator = BigInteger.ONE;
return;
}
if (denominator.compareTo(BigInteger.ZERO) < 0) {
numerator = numerator.negate();
denominator = denominator.negate();
}
BigInteger gcd = numerator.gcd(denominator);
this.numerator = numerator.divide(gcd);
this.denominator = denominator.divide(gcd);
this.toStringResult = null;
}
public BigInteger getNumerator() {
return numerator;
}
public BigInteger getDenominator() {
return denominator;
}
@Override
public int compareTo(Fraction a) {
Fraction res = this.subtract(a);
return res.numerator.compareTo(BigInteger.ZERO);
}
@Override
public String toString() {
if (toStringResult == null)
toStringResult = this.numerator + (!this.denominator.equals(BigInteger.ONE) ? "/" + denominator : "");
return toStringResult;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Fraction fraction = (Fraction) o;
return numerator.equals(fraction.numerator) &&
denominator.equals(fraction.denominator);
}
@Override
public int hashCode() {
return Objects.hash(numerator, denominator);
}
public Fraction negate() {
return new Fraction(this.numerator.negate(), this.denominator);
}
public Fraction reciprocate() {
if (this.numerator.equals(BigInteger.ZERO))
throw new ArithmeticException("Can not reciprocate fraction with value " + this);
return new Fraction(this.denominator, this.numerator);
}
public Fraction add(Fraction b) {
BigInteger numerator = this.numerator.multiply(b.denominator).add(this.denominator.multiply(b.numerator));
BigInteger denominator = this.denominator.multiply(b.denominator);
return new Fraction(numerator, denominator);
}
public Fraction subtract(Fraction b) {
return this.add(b.negate());
}
public Fraction multiply(Fraction b) {
BigInteger numerator = this.numerator.multiply(b.numerator);
BigInteger denominator = this.denominator.multiply(b.denominator);
return new Fraction(numerator, denominator);
}
public Fraction divide(Fraction b) {
return this.multiply(b.reciprocate());
}
public BigInteger floor() {
BigInteger res = this.numerator.divide(this.denominator);
if (this.numerator.compareTo(BigInteger.ZERO) >= 0)
return res;
return res.subtract(BigInteger.ONE);
}
public BigInteger ceil() {
BigInteger[] res = this.numerator.divideAndRemainder(this.denominator);
if (res[1].compareTo(BigInteger.ZERO) > 0)
return res[0].add(BigInteger.ONE);
return res[0];
}
public Fraction max(Fraction b) {
if (this.compareTo(b) < 0)
return b;
return this;
}
public Fraction min(Fraction b) {
if(this.compareTo(b) < 0)
return b;
return this;
}
}