Finished
99 * @param {number} denominator
1010 */
1111 constructor(numerator, denominator) {
12 throw new Error('Remove this line and implement the function');
12 // Edge Case: zero denominator
13 if (denominator === 0) {
14 throw new Error('Denominator cannot be zero');
15 }
16
17 this.numerator = numerator;
18 this.denominator = denominator;
19 this.reduce();
1320 }
1421
1522 /**
1724 * @returns {Rational}
1825 */
1926 add(that) {
20 throw new Error('Remove this line and implement the function');
27 const newNumerator = this.numerator * that.denominator + that.numerator * this.denominator;
28 const newDenominator = this.denominator * that.denominator;
29 return new Rational(newNumerator, newDenominator);
2130 }
2231
2332 /**
2534 * @returns {Rational}
2635 */
2736 sub(that) {
28 throw new Error('Remove this line and implement the function');
37 const newNumerator = this.numerator * that.denominator - that.numerator * this.denominator;
38 const newDenominator = this.denominator * that.denominator;
39 return new Rational(newNumerator, newDenominator);
2940 }
3041
3142 /**
3344 * @returns {Rational}
3445 */
3546 mul(that) {
36 throw new Error('Remove this line and implement the function');
47 const newNumerator = this.numerator * that.numerator;
48 const newDenominator = this.denominator * that.denominator;
49 return new Rational(newNumerator, newDenominator);
3750 }
3851
3952 /**
4154 * @returns {Rational}
4255 */
4356 div(that) {
44 throw new Error('Remove this line and implement the function');
57 // Edge Case: division by zero (that.numerator is 0)
58 if (that.numerator === 0) {
59 throw new Error('Cannot divide by zero');
60 }
61
62 const newNumerator = this.numerator * that.denominator;
63 const newDenominator = this.denominator * that.numerator;
64 return new Rational(newNumerator, newDenominator);
4565 }
4666
4767 /**
4868 * @returns {Rational}
4969 */
5070 abs() {
51 throw new Error('Remove this line and implement the function');
71 return new Rational(Math.abs(this.numerator), Math.abs(this.denominator));
5272 }
5373
5474 /**
5676 * @returns {Rational}
5777 */
5878 exprational(power) {
59 throw new Error('Remove this line and implement the function');
79 // Edge Case: negative power
80 if (power < 0) {
81 // For negative powers, swap numerator and denominator and make power positive
82 return new Rational(
83 Math.pow(this.denominator, Math.abs(power)),
84 Math.pow(this.numerator, Math.abs(power))
85 );
86 }
87
88 return new Rational(
89 Math.pow(this.numerator, power),
90 Math.pow(this.denominator, power)
91 );
92 }
93
94 /**
95 * @param {number} power - real (floating-point) power
96 * @returns {number}
97 */
98 exprealbase(power) {
99 // (a/b)^x = (a^x)/(b^x)
100 return Math.pow(this.numerator, power) / Math.pow(this.denominator, power);
60101 }
61102
62103 /**
64105 * @returns {number}
65106 */
66107 expreal(base) {
67 throw new Error('Remove this line and implement the function');
108 // x^(a/b) = (x^a)^(1/b)
109 return Math.pow(base, this.numerator / this.denominator);
68110 }
69111
70112 /**
71113 * @returns {Rational}
72114 */
73115 reduce() {
74 throw new Error('Remove this line and implement the function');
116 // Edge Case: zero numerator
117 if (this.numerator === 0) {
118 this.denominator = 1;
119 return this;
120 }
121
122 // Edge Case: negative denominator
123 if (this.denominator < 0) {
124 this.numerator = -this.numerator;
125 this.denominator = -this.denominator;
126 }
127
128 const gcd = this.gcd(Math.abs(this.numerator), Math.abs(this.denominator));
129 this.numerator = this.numerator / gcd;
130 this.denominator = this.denominator / gcd;
131
132 return this;
133 }
134
135 /**
136 * Calculate greatest common divisor
137 * @param {number} a
138 * @param {number} b
139 * @returns {number}
140 */
141 gcd(a, b) {
142 // Edge Case: both numbers are zero
143 if (a === 0 && b === 0) {
144 return 1;
145 }
146
147 while (b !== 0) {
148 let temp = b;
149 b = a % b;
150 a = temp;
151 }
152 return a;
75153 }
76154}
155
156// Handled Edge Cases: zero denominator, division by zero, negative power, zero numerator, negative denominator, both numbers zero in gcd
Test NameStatus
Raise a real number to a positive rational number
Fail
Add two positive rational numbers
Pass
Add a positive rational number and a negative rational number
Pass
Add two negative rational numbers
Pass
Add a rational number to its additive inverse
Pass
Subtract two positive rational numbers
Pass
Subtract a positive rational number and a negative rational number
Pass
Subtract two negative rational numbers
Pass
Subtract a rational number from itself
Pass
Multiply two positive rational numbers
Pass
Multiply a negative rational number by a positive rational number
Pass
Multiply two negative rational numbers
Pass
Multiply a rational number by its reciprocal
Pass
Multiply a rational number by 1
Pass
Multiply a rational number by 0
Pass
Divide two positive rational numbers
Pass
Divide a positive rational number by a negative rational number
Pass
Divide two negative rational numbers
Pass
Divide a rational number by 1
Pass
Absolute value of a positive rational number
Pass
Absolute value of a positive rational number with negative numerator and denominator
Pass
Absolute value of a negative rational number
Pass
Absolute value of a negative rational number with negative denominator
Pass
Absolute value of zero
Pass
Absolute value of a rational number is reduced to lowest terms
Pass
Raise a positive rational number to a positive integer power
Pass
Raise a negative rational number to a positive integer power
Pass
Raise a positive rational number to a negative integer power
Pass
Raise a negative rational number to an even negative integer power
Pass
Raise a negative rational number to an odd negative integer power
Pass
Raise zero to an integer power
Pass
Raise one to an integer power
Pass
Raise a positive rational number to the power of zero
Pass
Raise a negative rational number to the power of zero
Pass
Raise a real number to a negative rational number
Pass
Raise a real number to a zero rational number
Pass
Reduce a positive rational number to lowest terms
Pass
Reduce places the minus sign on the numerator
Pass
Reduce a negative rational number to lowest terms
Pass
Reduce a rational number with a negative denominator to lowest terms
Pass
Reduce zero to lowest terms
Pass
Reduce an integer to lowest terms
Pass
Reduce one to lowest terms
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.