/
RationalNumber-math.js
409 lines (337 loc) · 11.1 KB
/
RationalNumber-math.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
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
//
// Programmer: Craig Stuart Sapp <craig@ccrma.stanford.edu>
// Creation Date: Fri Dec 26 09:26:18 PST 2014
// Last Modified: Fri Jan 2 14:42:25 PST 2015
// Filename: RationalNumber-math.js
// Syntax: JavaScript 1.8.5/ECMAScript 5.1
// vim: ts=3
//
// Description: Mathematical operations for the RationalNumber object.
//
'use strict';
// If using node, load the constructor for RationalNumber:
if (typeof module !== 'undefined' && module.hasOwnProperty('exports')) {
var RationalNumber = require('./RationalNumber-base.js');
}
//////////////////////////////
//
// RationalNumber.prototype.abs -- Return a new RationalNumber which is
// the non-negative version of the calling object.
//
RationalNumber.prototype.abs = function () {
return this.clone().setSign('+');
}
//////////////////////////////
//
// RationalNumber.prototype.invert -- Switch the numerator and denominator
// in the calling object.
//
RationalNumber.prototype.invert = function () {
var temp = this._n;
this._n = this._d;
this._d = temp;
return this;
}
//////////////////////////////
//
// RationalNumber.prototype.inversion -- Alias for getInversion().
// RationalNumber.prototype.getInversion -- Return the reciproical of
// the RationalNumber object. Returns a new RationalNumber and does
// not modify the calling object.
//
RationalNumber.prototype.getInversion = function () {
var n = this.getNumerator();
var d = this.getDenominator();
return new RationalNumber(d, n);
}
RationalNumber.prototype.inversion = RationalNumber.prototype.getInversion;
//////////////////////////////
//
// RationalNumber.prototype.negate -- Make positive values negative and
// negative values positive.
//
RationalNumber.prototype.negate = function () {
this._s = -this._s;
return this;
}
//////////////////////////////
//
// RationalNumber.prototype.negation -- Alias of getNegation().
// RationalNumber.prototype.getNegation -- Return a copy of the object, with
// the sign reversed.
//
RationalNumber.prototype.getNegation = function () {
return this.clone().negate();
}
//////////////////////////////
//
// RationalNumber.prototype.addTo -- To this RationalNumber, add additional
// RationalNumbers or integers. If an input parameter is a string, then
// parse as an integer, or as a fraction if '/' is found in the string.
// The value of this object will be changed to the sum.
RationalNumber.prototype.addTo = function () {
var a, ai, ao;
var b, bi, bo;
if ((arguments.length === 1) && (arguments[0] instanceof Array)) {
this.addTo.apply(this, arguments[0]);
return this;
}
for (var i=0; i<arguments.length; i++) {
a = this.getNumerator();
b = this.getDenominator();
if (typeof arguments[i] === 'string') {
var matches = arguments[i].match(/([+-]?\d+)[-+ _](\d+)\/(\d+)/);
if (matches) {
ai = parseInt(matches[2]);
bi = parseInt(matches[3]);
ai += bi * parseInt(matches[1]);
} else {
matches = arguments[i].match(/(-?\d+)\/(\d+)/);
if (matches) {
ai = parseInt(matches[1]);
bi = parseInt(matches[2]);
} else {
ai = parseInt(arguments[i]) || 0;
bi = 1;
}
}
} else if (typeof arguments[i] === 'number') {
ai = arguments[i] ? parseInt(arguments[i]) : 0;
bi = 1;
} else if ((typeof arguments[i] === 'object') &&
(this instanceof RationalNumber)) {
ai = arguments[i].getNumerator();
bi = arguments[i].getDenominator();
} else {
// Don't know what the object is, so skip it.
continue;
}
if ((b === 0) && (bi === 0) && (a < 0) && (ai > 0)) {
return this.setValue(0,0);
}
if ((b === 0) && (bi === 0) && (a > 0) && (ai < 0)) {
return this.setValue(0,0);
}
if ((b === 0) && (a === 0)) { return this.setValue(0, 0) }
if ((bi === 0) && (ai === 0)) { return this.setValue(0, 0) }
if ((b === 0) || (bi === 0)) { return this.setValue(1, 0) }
// deal with infnities and NaNs here.
ao = a * bi + b * ai;
bo = b * bi;
this.setValue(ao, bo);
}
return this;
}
//////////////////////////////
//
// RationalNumber.prototype.add -- Similar to addTo(), but will not
// alter the calling object and instead returns a new RationalNumber
// which is the sum of the rational numbers.
RationalNumber.prototype.add = function () {
return RationalNumber.prototype.addTo.apply(this.clone(), arguments);
}
//////////////////////////////
//
// RationalNumber.prototype.subtractTo -- To this RationalNumber, subtract
// a RationalNumber or integer. If an input parameter is a string, then
// parse as an integer, or as a fraction if '/' is found in the string.
// The value of this object will be changed to the difference value.
// All arguments will be negated and the added to the initial value
// of the object.
RationalNumber.prototype.subtractTo = function () {
var a, ai, ao;
var b, bi, bo;
if ((arguments.length === 1) && (arguments[0] instanceof Array)) {
this.subtractTo.apply(this, arguments[0]);
return this;
}
for (var i=0; i<arguments.length; i++) {
a = this.getNumerator();
b = this.getDenominator();
if (typeof arguments[i] === 'string') {
var matches = arguments[i].match(/([+-]?\d+)[-+ _](\d+)\/(\d+)/);
if (matches) {
ai = parseInt(matches[2]);
bi = parseInt(matches[3]);
ai += bi * parseInt(matches[1]);
} else {
matches = arguments[i].match(/(-?\d+)\/(\d+)/);
if (matches) {
ai = parseInt(matches[1]);
bi = parseInt(matches[2]);
} else {
ai = parseInt(arguments[i]) || 0;
bi = 1;
}
}
} else if (typeof arguments[i] === 'number') {
ai = arguments[i] ? parseInt(arguments[i]) : 0;
bi = 1;
} else if ((typeof arguments[i] === 'object') &&
(this instanceof RationalNumber)) {
ai = arguments[i].getNumerator();
bi = arguments[i].getDenominator();
} else {
// Don't know what the object is, so skip it.
continue;
}
if ((b === 0) && (bi === 0) && (a < 0) && (ai < 0)) {
return this.setValue(0,0);
}
if ((b === 0) && (bi === 0) && (a > 0) && (ai > 0)) {
return this.setValue(0,0);
}
if ((b === 0) && (a === 0)) { return this.setValue(0, 0) }
if ((bi === 0) && (ai === 0)) { return this.setValue(0, 0) }
if ((b === 0) || (bi === 0)) { return this.setValue(1, 0) }
ao = a * bi - b * ai;
bo = b * bi;
this.setValue(ao, bo);
}
return this;
}
//////////////////////////////
//
// RationalNumber.prototype.subtract -- Similar to subtractTo(), but will not
// alter the calling object and instead returns a new RationalNumber
// which is the sum of the first numbers and the negation of the other
// numbers.
RationalNumber.prototype.subtract = function () {
return RationalNumber.prototype.subtractTo.apply(this.clone(), arguments);
}
//////////////////////////////
//
// RationalNumber.prototype.multiplyTo -- To this RationalNumber, multiply
// additional RationalNumbers or integers. If an input parameter is a
// string, then parse as an integer, or as a fraction if '/' is found
// in the string. The value of this object will be changed to the sum.
RationalNumber.prototype.multiplyTo = function () {
var a, ai, ao;
var b, bi, bo;
if ((arguments.length === 1) && (arguments[0] instanceof Array)) {
this.multiplyTo.apply(this, arguments[0]);
return this;
}
for (var i=0; i<arguments.length; i++) {
a = this.getNumerator();
b = this.getDenominator();
if (typeof arguments[i] === 'string') {
var matches = arguments[i].match(/([+-]?\d+)[-+ _](\d+)\/(\d+)/);
if (matches) {
ai = parseInt(matches[2]);
bi = parseInt(matches[3]);
ai += bi * parseInt(matches[1]);
} else {
matches = arguments[i].match(/(-?\d+)\/(\d+)/);
if (matches) {
ai = parseInt(matches[1]);
bi = parseInt(matches[2]);
} else {
ai = parseInt(arguments[i]) || 0;
bi = 1;
}
}
} else if (typeof arguments[i] === 'number') {
ai = arguments[i] ? parseInt(arguments[i]) : 0;
bi = 1;
} else if ((typeof arguments[i] === 'object') &&
(this instanceof RationalNumber)) {
ai = arguments[i].getNumerator();
bi = arguments[i].getDenominator();
} else {
// Don't know what the object is, so skip it.
continue;
}
if (((a === 0) || (ai === 0)) && ((b === 0) || (bi === 0))) {
return this.setValue(0,0);
} else if ((b === 0) || (bi === 0)) {
if ((a < 0) && (ai < 0)) {
return this.setValue(1, 0)
} else if ((a >= 0) && (ai >= 0)) {
return this.setValue(1, 0)
} else {
return this.setValue(-1, 0)
}
}
ao = a * ai;
bo = b * bi;
this.setValue(ao, bo);
}
return this;
}
//////////////////////////////
//
// RationalNumber.prototype.multiply -- Similar to multiplyTo(), but will not
// alter the calling object and instead returns a new RationalNumber
// which is the product of the rational numbers.
RationalNumber.prototype.multiply = function () {
return RationalNumber.prototype.multiplyTo.apply(this.clone(), arguments);
}
//////////////////////////////
//
// RationalNumber.prototype.divideTo -- To this RationalNumber, divide
// additional RationalNumbers or integers. If an input parameter is a
// string, then parse as an integer, or as a fraction if '/' is found
// in the string. The value of this object will be changed.
RationalNumber.prototype.divideTo = function () {
var a, ai, ao;
var b, bi, bo;
if ((arguments.length === 1) && (arguments[0] instanceof Array)) {
this.divideTo.apply(this, arguments[0]);
return this;
}
for (var i=0; i<arguments.length; i++) {
a = this.getNumerator();
b = this.getDenominator();
if (typeof arguments[i] === 'string') {
var matches = arguments[i].match(/([+-]?\d+)[-+ _](\d+)\/(\d+)/);
if (matches) {
ai = parseInt(matches[2]);
bi = parseInt(matches[3]);
ai += bi * parseInt(matches[1]);
} else {
matches = arguments[i].match(/(-?\d+)\/(\d+)/);
if (matches) {
ai = parseInt(matches[1]);
bi = parseInt(matches[2]);
} else {
ai = parseInt(arguments[i]) || 0;
bi = 1;
}
}
} else if (typeof arguments[i] === 'number') {
ai = arguments[i] ? parseInt(arguments[i]) : 0;
bi = 1;
} else if ((typeof arguments[i] === 'object') &&
(this instanceof RationalNumber)) {
ai = arguments[i].getNumerator();
bi = arguments[i].getDenominator();
} else {
// Don't know what the object is, so skip it.
continue;
}
if ((b === 0) && (a === 0)) { return this.setValue(0, 0) }
if ((bi === 0) && (ai === 0)) { return this.setValue(0, 0) }
if ((b === 0) && (ai === 0)) { return this.setValue(0, 0) }
if ((a === 0) && (bi === 0)) { return this.setValue(0, 0) }
ao = a * bi;
bo = b * ai;
this.setValue(ao, bo);
}
return this;
}
//////////////////////////////
//
// RationalNumber.prototype.divide -- Similar to divideTo(), but will not
// alter the calling object and instead returns a new RationalNumber
// which is the division of the rational numbers in the argument list.
RationalNumber.prototype.divide = function () {
return RationalNumber.prototype.divideTo.apply(this.clone(), arguments);
}
///////////////////////////////////////////////////////////////////////////
//
// Export RationalNumber constructor if running in node:
//
if (typeof module !== 'undefined' && module.hasOwnProperty('exports')) {
module.exports = RationalNumber;
}