-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRational.java
More file actions
234 lines (215 loc) · 5.87 KB
/
Rational.java
File metadata and controls
234 lines (215 loc) · 5.87 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
/* Original Licensing Copyright
*
* Represents a rational number.
* Copyright (C) 2021 DZ-FSDev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.dz_fs_dev.physics;
/**
* Represents a rational number. A rational number is one which can be
* represented as a fraction where both the numerator and denominator
* is a member of Integer.
*
* @author DZ-FSDev
* @since 17.0.2
* @version 0.0.21
*/
public class Rational extends Number implements Comparable<Rational>{
/**
* @since 0.0.20
*/
private static final long serialVersionUID = -8501455433949295842L;
private final long denominator;
private final long numerator;
/**
* Initializes a new instance of a Rational number equivalent in value.
*
* @param value The value this rational number should represent.
* @since 0.0.5
*/
public Rational(long value) {
this(value, 1);
}
/**
* Initializes a new instance of a Rational number given an integral
* numerator and denominator. Denominators will be normalized positive.
*
* @param numerator The numerator of this rational number.
* @param denominator The denominator of this rational number.
* @since 0.0.13
*/
public Rational(long numerator, long denominator) {
if(denominator == 0)throw new IllegalArgumentException("Denominator cannot be zero!");
if(denominator < 0) {
numerator *= -1;
denominator *= -1;
}
long gcd = numerator == 0 ? denominator : Math.abs(gcd(numerator, denominator));
this.denominator = denominator / gcd;
this.numerator = numerator / gcd;
}
/**
* Returns a new Rational which represents the result of addition between
* this Rational and the Augend.
*
* @param augend The other Rational to be added.
* @return A new Rational which represents the result of addition.
* @since 0.0.16
*/
public Rational add(Rational augend) {
return new Rational(
this.numerator * augend.denominator + augend.numerator * this.denominator,
this.denominator * augend.denominator);
}
/**
* Returns a new Rational which represents the result of subtraction from
* this Rational, the subtrahend.
*
* @param subtrahend The other Rational to be subtracted.
* @return A new Rational which represents the result of subtraction.
* @since 0.0.15
*/
public Rational subtract(Rational subtrahend) {
return new Rational(
this.numerator * subtrahend.denominator - subtrahend.numerator * this.denominator,
this.denominator * subtrahend.denominator);
}
/**
* Returns a new Rational which represents the result of multiplication
* between this Rational and the Multiplicand.
*
* @param multiplicand The other Rational to be multiplied.
* @return A new Rational which represents the result of multiplication.
* @since 0.0.17
*/
public Rational multiply(Rational multiplicand) {
return new Rational(
this.numerator * multiplicand.numerator,
this.denominator * multiplicand.denominator);
}
/**
* Returns a new Rational which represents the result of division
* between this Rational and the Divisor.
*
* @param division The other Rational to be divided.
* @return A new Rational which represents the result of division.
* @since 0.0.18
*/
public Rational divide(Rational divisor) {
return new Rational(
this.numerator * divisor.denominator,
this.denominator * divisor.numerator);
}
/**
* @since 0.0.8
*/
@Override
public int intValue() {
return (int)(numerator / denominator);
}
/**
* @since 0.0.8
*/
@Override
public long longValue() {
return numerator / denominator;
}
/**
* @since 0.0.8
*/
@Override
public float floatValue() {
return (float)numerator / denominator;
}
/**
* @since 0.0.8
*/
@Override
public double doubleValue() {
return (double)numerator / denominator;
}
/**
* Finds the lowest common multiple of two numbers.
*
* @param a The first number.
* @param b The second number.
* @return The lowest common multiple of the two numbers.
* @since 0.0.7
*/
private static long lcm(long a, long b) {
if (a == 0 || b == 0)
return 0;
else
return Math.abs(a * b) / gcd(a, b);
}
/**
* Recursively finds the greatest common divisor using Euclid's Algorithm
* for two numbers.
*
* @param a The first number.
* @param b The second number.
* @return The greatest common divisor of the two numbers.
* @since 0.0.6
*/
private static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
/**
* @since 0.0.19
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int) (denominator ^ (denominator >>> 32));
result = prime * result + (int) (numerator ^ (numerator >>> 32));
return result;
}
/**
* @since 0.0.20
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!(obj instanceof Rational))
return false;
Rational other = (Rational) obj;
if (denominator != other.denominator)
return false;
if (numerator != other.numerator)
return false;
return true;
}
@Override
public int compareTo(Rational o) {
// TODO Auto-generated method stub
return 0;
}
/**
* @since 0.0.21
*/
@Override
public Rational clone() {
return new Rational(numerator, denominator);
}
@Override
public String toString() {
return numerator + "/" + denominator;
}
}