-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfractions.js
263 lines (219 loc) · 5.68 KB
/
fractions.js
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
const gcd = require('num-integer').gcd;
const { Integer, Num, NumAssign, One, Signed, Zero } = require('num-traits');
function const_abs(a) {
if (a < Zero::zero()) {
return -a;
} else {
return a;
}
}
function gcd_recur(m, n) {
if (n == Zero::zero()) {
return const_abs(m);
} else {
return gcd_recur(n, m % n);
}
}
function const_gcd(m, n) {
if (m == Zero::zero()) {
return const_abs(n);
} else {
return gcd_recur(m, n);
}
}
class Fraction {
constructor(numer, denom) {
this.numer = numer;
this.denom = denom;
}
static new_raw(numer, denom) {
return new Fraction(numer, denom);
}
numer() {
return this.numer;
}
denom() {
return this.denom;
}
static zero() {
return new Fraction(Zero::zero(), One::one());
}
is_zero() {
return this.numer.is_zero() && !this.denom.is_zero();
}
set_zero() {
this.numer.set_zero();
this.denom.set_one();
}
is_normal() {
return !(this.numer.is_zero() || this.denom.is_zero());
}
is_infinite() {
return !this.numer.is_zero() && this.denom.is_zero();
}
is_nan() {
return this.numer.is_zero() && this.denom.is_zero();
}
static one() {
return new Fraction(One::one(), One::one());
}
is_one() {
return this.numer == this.denom;
}
set_one() {
this.numer.set_one();
this.denom.set_one();
}
normalize() {
this.keep_denom_positive();
return this.reduce();
}
abs() {
if (this.is_negative()) {
return -this;
} else {
return this;
}
}
signum() {
if (this.is_positive()) {
return Fraction.one();
} else if (this.is_zero()) {
return Fraction.zero();
} else {
return -Fraction.one();
}
}
is_positive() {
return this.numer.is_positive();
}
is_negative() {
return this.numer.is_negative();
}
reduce() {
const common = gcd(this.numer, this.denom);
if (common != One::one() && common != Zero::zero()) {
this.numer /= common;
this.denom /= common;
}
return common;
}
keep_denom_positive() {
if (this.denom < Zero::zero()) {
this.numer = -this.numer;
this.denom = -this.denom;
}
}
reciprocal() {
[this.numer, this.denom] = [this.denom, this.numer];
this.keep_denom_positive();
}
cross(rhs) {
return this.numer * rhs.denom - this.denom * rhs.numer;
}
neg() {
return new Fraction(-this.numer, this.denom);
}
inv() {
let res = this;
res.reciprocal();
return res;
}
eq(other) {
return this.denom == One::one() && this.numer == other;
}
partial_cmp(other) {
if (this.denom == One::one() || other == Zero::zero()) {
return this.numer.partial_cmp(other);
}
let lhs = new Fraction(this.numer, other);
let rhs = this.denom;
lhs.reduce();
return lhs.numer.partial_cmp(lhs.denom * rhs);
}
cmp(other) {
if (this.denom == other.denom) {
return this.numer.cmp(other.numer);
}
let lhs = new Fraction(this.numer, other.numer);
let rhs = new Fraction(this.denom, other.denom);
lhs.reduce();
rhs.reduce();
return (lhs.numer * rhs.denom).cmp(lhs.denom * rhs.numer);
}
add_assign(other) {
if (this.denom == other.denom) {
this.numer += other.numer;
this.reduce();
return;
}
let rhs = other;
[this.denom, rhs.numer] = [rhs.numer, this.denom];
let common_n = this.reduce();
let common_d = rhs.reduce();
[this.denom, rhs.numer] = [rhs.numer, this.denom];
this.numer = this.numer * rhs.denom + this.denom * rhs.numer;
this.denom *= rhs.denom;
[this.denom, common_d] = [common_d, this.denom];
this.reduce();
this.numer *= common_n;
this.denom *= common_d;
this.reduce();
}
sub_assign(other) {
if (this.denom == other.denom) {
this.numer -= other.numer;
this.reduce();
return;
}
let rhs = other;
[this.denom, rhs.numer] = [rhs.numer, this.denom];
let common_n = this.reduce();
let common_d = rhs.reduce();
[this.denom, rhs.numer] = [rhs.numer, this.denom];
this.numer = this.cross(rhs);
this.denom *= rhs.denom;
[this.denom, common_d] = [common_d, this.denom];
this.reduce();
this.numer *= common_n;
this.denom *= common_d;
this.reduce();
}
mul_assign(other) {
let rhs = other;
[this.numer, rhs.numer] = [rhs.numer, this.numer];
this.reduce();
rhs.reduce();
this.numer *= rhs.numer;
this.denom *= rhs.denom;
}
div_assign(other) {
let rhs = other;
[this.denom, rhs.numer] = [rhs.numer, this.denom];
this.normalize();
rhs.reduce();
this.numer *= rhs.denom;
this.denom *= rhs.numer;
}
add(other) {
let res = this;
res.add_assign(other);
return res;
}
sub(other) {
let res = this;
res.sub_assign(other);
return res;
}
mul(other) {
let res = this;
res.mul_assign(other);
return res;
}
div(other) {
let res = this;
res.div_assign(other);
return res;
}
}
module.exports = Fraction;