-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrationalnumbertest_memory.js
369 lines (297 loc) · 13.7 KB
/
rationalnumbertest_memory.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
var rational = require('./build/Release/rationalnumber.node');
//var rational = require('./build/Debug/rationalnumber.node');
console.log("Inside rationalnumbertest, the result of the require command is:\n");
console.log(rational);
var RationalNumber = rational.RationalNumber;
console.log("The rational number constructor's prototype looks like:\n");
console.log(RationalNumber.prototype);
console.log("The add function looks like:\n", RationalNumber.prototype.add);
function testing() {
var rationalInstance = new RationalNumber();
console.log("The rationalInstance looks like:\n", rationalInstance);
console.log("Is the rationalInstance instanceof RationalNumber?", (rationalInstance instanceof RationalNumber) ? 'yes' : 'no');
var initFromConstructor = new RationalNumber('1/3');
console.log("The new rationalInstance from constructor looks like:\n", initFromConstructor);
rationalInstance.numerator = 1;
console.log("After setting the numerator, the instance looks like:\n", rationalInstance);
rationalInstance.denominator = 2;
console.log("After setting the denominator, the instance looks like:\n", rationalInstance);
console.log("And getting the new denominator yields:\n", rationalInstance.denominator);
var rationalInstance2 = new RationalNumber();
rationalInstance2.numerator = 3;
rationalInstance2.denominator = 4;
console.log("The second rational instance looks like:\n", rationalInstance2);
var rationalInstance3 = rationalInstance.add(rationalInstance2);
console.log("The sum is: ", rationalInstance3, "\n!!!1111\n");
console.log("After setting the numerator of the result to 6, the result now looks like:\n");
rationalInstance3.numerator = 6;
console.log(rationalInstance3);
var rationalInstance4 = rationalInstance.subtract(rationalInstance2);
console.log("The difference between 1/2 and 3/4 is:\n", rationalInstance4);
var rationalInstance5 = rationalInstance.multiply(rationalInstance2);
console.log("The product of 1/2 and 3/4 is:\n", rationalInstance5);
var rationalInstance6 = rationalInstance.divide(rationalInstance2);
console.log("The quotient of 1/2 and 3/4 is:\n", rationalInstance6);
var perfectSquare = new RationalNumber();
perfectSquare.numerator = 4;
perfectSquare.denominator = 9;
var oneHalf = new RationalNumber();
oneHalf.numerator = 1;
oneHalf.denominator = 2;
var perfectRoot = perfectSquare.power(oneHalf);
console.log("The result of (4/9)^(1/2) is:\n", perfectRoot);
try {
var imperfectRoot = new RationalNumber();
imperfectRoot.numerator = 2;
imperfectRoot.denominator = 3;
var result = imperfectRoot.power(oneHalf);
} catch(error) {
console.log("Got error object:\n", error.toString());
}
var perfectCube = new RationalNumber();
perfectCube.initializeFromString("-27/8");
var negativeOneThird = new RationalNumber();
negativeOneThird.initializeFromString("-1/3");
var perfectRoot = perfectCube.power(negativeOneThird);
console.log("The result of (-27/8)^(-1/3) is:\n", perfectRoot.numerator + "/" + perfectRoot.denominator);
var bigFraction = new RationalNumber();
bigFraction.initializeFromString("12/13");
console.log("The bigFraction's numerator is:\n", bigFraction.numerator);
console.log("The bigFraction's denominator is:\n", bigFraction.denominator);
console.log("About to construct a really, really big fraction.\n");
bigFraction.initializeFromString("11111111111111111111111111111111111111111111111111/2222222222222222222222222222222222222222222222222222");
console.log("The really, really big fraction's numerator is:\n", bigFraction.numerator);
console.log("The really, really big fraction's denominator is:\n", bigFraction.denominator);
var integerFraction = new RationalNumber();
integerFraction.initializeFromString("12");
console.log("The integer fraction's numerator is:\n", integerFraction.numerator);
console.log("The integer fraction's denominator is:\n", integerFraction.denominator);
bigFraction.initializeFromString("12/13");
var zero = new RationalNumber();
console.log("About to try dividing by zero.\n");
try {
var divideByZero = bigFraction.divide(zero);
console.log("12/13 / 0 is:\n", divideByZero.numerator + "/" + divideByZero.denominator);
} catch(error) {
console.log("The result of dividing 12/13 by 0 got error:\n", error.toString());
}
var selfFraction = new RationalNumber();
selfFraction.initializeFromString("12/13");
var otherFraction = new RationalNumber();
otherFraction.initializeFromString("12");
console.log("The result of testing whether 12/13 is equal to 12 is:\n");
console.log(selfFraction.isEqualTo(otherFraction));
otherFraction.initializeFromString("24/26");
console.log("The result of testing whether 12/13 is equal to 24/26 is:\n");
console.log(selfFraction.isEqualTo(otherFraction));
console.log("The result of testing whether 12/13 is less than 24/26 is:\n");
console.log(selfFraction.isLessThan(otherFraction));
otherFraction.initializeFromString("12");
console.log("The result of testing whether 12 is greater than 12/13 is:\n");
console.log(otherFraction.isGreaterThan(selfFraction));
otherFraction.initializeFromString("24/26");
console.log("The result of testing whether 12/13 is less than or equal to 24/26 is:\n");
console.log(selfFraction.isLessThanOrEqualTo(otherFraction));
otherFraction.initializeFromString("14/13");
console.log("The result of testing whether 12/13 is greater than or equal to 14/13 is:\n");
console.log(selfFraction.isGreaterThanOrEqualTo(otherFraction));
var smallInteger = new RationalNumber();
smallInteger.numerator = 3125;
var factorialResult = smallInteger.factorial();
console.log("The result of taking the factorial of 3125 is:\n", factorialResult.numerator, "/", factorialResult.denominator);
smallInteger.initializeFromString("1/2");
console.log("ABout to try and take the factorial of 1/2.\n");
try {
factorialResult = smallInteger.factorial();
} catch(error) {
console.log("Got error:\n", error.toString());
}
smallInteger.initializeFromString("-23");
console.log("About to try and take the factorial of -23.\n");
try {
factorialResult = smallInteger.factorial();
} catch(error) {
console.log("Got error:\n", error.toString());
}
console.log("About to try and take the factorial of:.\n", RationalNumber.prototype.getFactorialMax().numerator);
try {
factorialResult = RationalNumber.prototype.getFactorialMax().factorial();
} catch(error) {
console.log("Got error:\n", error.toString());
}
console.log("The result is:\n", factorialResult.numerator);
var oneMore = new RationalNumber();
oneMore.initializeFromString("1");
console.log("About to try and take the factorial of:\n", RationalNumber.prototype.getFactorialMax().add(oneMore).numerator);
try {
factorialResult = RationalNumber.prototype.getFactorialMax().add(oneMore).factorial();
} catch(error) {
console.log("Got error:\n", error.toString());
}
var five = new RationalNumber();
five.initializeFromString("5");
var three = new RationalNumber();
three.initializeFromString("3");
var r = five.modulus(three);
console.log("The result of 5%3 is:\n", r.numerator + "/" + r.denominator);
five.denominator = 2;
try {
five.modulus(three);
} catch(error) {
console.log("5/2 % 3Got error:\n", error.toString());
}
five.denominator = 1;
three.denominator = 2;
try {
five.modulus(three);
} catch(error) {
console.log("5 % 3/2 Got error:\n", error.toString());
}
five.initializeFromString("-5");
three.initializeFromString("3");
r = five.modulus(three);
console.log("The result of -5 % 3 is:\n", r.numerator + "/" + r.denominator);
five.initializeFromString("5");
three.initializeFromString("-3");
r = five.modulus(three);
console.log("The result of 5 % -3 is:\n", r.numerator + "/" + r.denominator);
five.initializeFromString("-5");
three.initializeFromString("-3");
r = five.modulus(three);
console.log("The result of -5 % -3 is:\n", r.numerator + "/" + r.denominator);
five.initializeFromString("5");
three.initializeFromString("3");
r = three.modulus(five);
console.log("The result of 3 % 5 is:\n", r.numerator + "/" + r.denominator);
three.initializeFromString("0");
try {
r = five.modulus(three);
console.log("The result of 5 % 0 is:\n", r.numerator + "/" + r.denominator);
} catch(error) {
console.log("5 % 0 resulted in error:\n", error.toString());
}
five.initializeFromString("5/3");
three.initializeFromString("3/2");
r = five.modulus(three);
console.log("The result of 5/3 % 3/2 is:\n", r.numerator + "/" + r.denominator);
var zero = new RationalNumber();
var one = zero.power(zero);
console.log("The result of 0^0 is:\n", one.numerator + "/" + one.denominator);
var negativeOne = new RationalNumber();
negativeOne.initializeFromString("-1");
try {
var inf = zero.power(negativeOne);
console.log("Taking zero to the power of negative one gave result:\n", inf.numerator + "/" + inf.denominator);
} catch(error) {
console.log("Taking zero to the power of negative one resulted in error:\n", error.toString());
}
var negative2 = new RationalNumber();
negative2.initializeFromString('-2');
oneFourth = negative2.power(negative2);
console.log("The result of (-2)^(-2) is:\n", oneFourth.numerator + '/' + oneFourth.denominator);
try {
var bad = (new RationalNumber('3/1')).add(4);
} catch(e) {
console.log("The result of passing a plain javascript number to add is:\n", e.toString());
}
try {
var bad = (new RationalNumber('3/1')).subtract(4);
} catch(e) {
console.log("The result of passing a plain javascript number to subtract is:\n", e.toString());
}
try {
var bad = (new RationalNumber('3/1')).multiply(4);
} catch(e) {
console.log("The result of passing a plain javascript number to multiply is:\n", e.toString());
}
try {
var bad = (new RationalNumber('3/1')).divide(4);
} catch(e) {
console.log("The result of passing a plain javascript number to divide is:\n", e.toString());
}
try {
var bad = (new RationalNumber('3/1')).power(4);
} catch(e) {
console.log("The result of passing a plain javascript number to power is:\n", e.toString());
}
try {
var bad = (new RationalNumber('3/1')).modulus(4);
} catch(e) {
console.log("The result of passing a plain javascript number to modulus is:\n", e.toString());
}
try {
var bad = (new RationalNumber('3/1')).isEqualTo(4);
} catch(e) {
console.log("The result of passing a plain javascript number to isEqualTo is:\n", e.toString());
}
try {
var bad = (new RationalNumber('3/1')).isLessThan(4);
} catch(e) {
console.log("The result of passing a plain javascript number to isLessThan is:\n", e.toString());
}
try {
var bad = (new RationalNumber('3/1')).isGreaterThan(4);
} catch(e) {
console.log("The result of passing a plain javascript number to isGreaterThan is:\n", e.toString());
}
try {
var bad = (new RationalNumber('3/1')).isGreaterThan(4);
} catch(e) {
console.log("The result of passing a plain javascript number to isGreaterThan is:\n", e.toString());
}
try {
var bad = (new RationalNumber('3/1')).isLessThanOrEqualTo(4);
} catch(e) {
console.log("The result of passing a plain javascript number to isLessThanOrEqualTo is:\n", e.toString());
}
try {
var bad = (new RationalNumber('3/1')).isGreaterThanOrEqualTo(4);
} catch(e) {
console.log("The result of passing a plain javascript number to isGreaterThanOrEqualTo is:\n", e.toString());
}
var gcdA = new RationalNumber('15');
var gcdB = new RationalNumber('18');
var gcdResult = RationalNumber.gcd(gcdA, gcdB);
console.log("The gcdResult is:\n", gcdResult);
var lcmA = new RationalNumber('15');
var lcmB = new RationalNumber('18');
var lcmResult = RationalNumber.lcm(lcmA, lcmB);
console.log("The lcmResult is:\n", lcmResult);
try {
var bad = RationalNumber.lcm(lcmA, 3);
} catch(e) {
console.log("The result of passing a plain javascript number to lcm is:\n", e.toString());
}
try {
var bad = RationalNumber.gcd(lcmA, 3);
} catch(e) {
console.log("The result of passing a plain javascript number to gcd is:\n", e.toString());
}
try {
var bad = new RationalNumber();
bad.initializeFromString("poop");
} catch(e) {
console.log("The result of initializing from poop is:\n", e.toString());
}
var zeroTest = RationalNumber.gcd(new RationalNumber('0'), new RationalNumber('0'));
console.log("The gcd of zero and zero is:\n", zeroTest);
zeroTest = RationalNumber.lcm(new RationalNumber('0'), new RationalNumber('0'));
console.log("The lcm of zero and zero is:\n", zeroTest);
var bigRat = new RationalNumber('134123421/134123420');
var floored = bigRat.floor();
console.log("The result of flooring the bigRat is:\n", floored);
}
for (var i = 0; i < 25; i++) {
testing();
}
var count = 0;
var logInterval = null;
function logger() {
console.log("Waiting for garbage to be collected.");
count++;
if (count >= 10) {
clearInterval(logInterval);
process.exit(0);
}
}
logInterval = setInterval(logger, 2000);