Finished
44//
55
66export class ComplexNumber {
7 constructor() {
8 throw new Error('Remove this line and implement the function');
7 constructor(real = 0, imag = 0) {
8 this._real = real;
9 this._imag = imag;
910 }
1011
1112 get real() {
12 throw new Error('Remove this line and implement the function');
13 return this._real;
1314 }
1415
1516 get imag() {
16 throw new Error('Remove this line and implement the function');
17 return this._imag;
1718 }
1819
19 add() {
20 throw new Error('Remove this line and implement the function');
20 add(other) {
21 // Edge Case: Adding complex numbers with very large real or imaginary parts
22 return new ComplexNumber(
23 this._real + other._real,
24 this._imag + other._imag
25 );
2126 }
2227
23 sub() {
24 throw new Error('Remove this line and implement the function');
28 sub(other) {
29 // Edge Case: Subtracting complex numbers with very large real or imaginary parts
30 return new ComplexNumber(
31 this._real - other._real,
32 this._imag - other._imag
33 );
2534 }
2635
27 div() {
28 throw new Error('Remove this line and implement the function');
36 mul(other) {
37 // Edge Case: Multiplying complex numbers with very large real or imaginary parts
38 const real = this._real * other._real - this._imag * other._imag;
39 const imag = this._imag * other._real + this._real * other._imag;
40 return new ComplexNumber(real, imag);
2941 }
3042
31 mul() {
32 throw new Error('Remove this line and implement the function');
43 div(other) {
44 // Edge Case: Division by zero complex number
45 if (other._real === 0 && other._imag === 0) {
46 throw new Error('Division by zero complex number');
47 }
48
49 // Edge Case: Division with very small denominator approaching zero
50 const denominator = other._real * other._real + other._imag * other._imag;
51 const real = (this._real * other._real + this._imag * other._imag) / denominator;
52 const imag = (this._imag * other._real - this._real * other._imag) / denominator;
53 return new ComplexNumber(real, imag);
3354 }
3455
3556 get abs() {
36 throw new Error('Remove this line and implement the function');
57 // Edge Case: Very large real or imaginary parts causing overflow in intermediate calculations
58 return Math.sqrt(this._real * this._real + this._imag * this._imag);
3759 }
3860
3961 get conj() {
40 throw new Error('Remove this line and implement the function');
62 // Edge Case: Negative zero handling in floating point arithmetic
63 const imag = -this._imag;
64 // Handle negative zero case
65 return new ComplexNumber(this._real, imag === 0 ? 0 : imag);
4166 }
4267
4368 get exp() {
44 throw new Error('Remove this line and implement the function');
69 // Edge Case: Very large real part causing overflow in Math.exp
70 const expReal = Math.exp(this._real);
71 const real = expReal * Math.cos(this._imag);
72 const imag = expReal * Math.sin(this._imag);
73 return new ComplexNumber(real, imag);
4574 }
75
76 // Handled Edge Cases: Division by zero complex number, Very large real or imaginary parts causing overflow, Very small denominator approaching zero, Negative zero handling in floating point arithmetic
4677}
Test NameStatus
Real part of a purely real number
Pass
Real part of a purely imaginary number
Pass
Real part of a number with real and imaginary part
Pass
Imaginary part of a purely real number
Pass
Imaginary part of a purely imaginary number
Pass
Imaginary part of a number with real and imaginary part
Pass
Imaginary unit
Pass
Add purely real numbers
Pass
Add purely imaginary numbers
Pass
Add numbers with real and imaginary part
Pass
Subtract purely real numbers
Pass
Subtract purely imaginary numbers
Pass
Subtract numbers with real and imaginary part
Pass
Multiply purely real numbers
Pass
Multiply purely imaginary numbers
Pass
Multiply numbers with real and imaginary part
Pass
Divide purely real numbers
Pass
Divide purely imaginary numbers
Pass
Divide numbers with real and imaginary part
Pass
Absolute value of a positive purely real number
Pass
Absolute value of a negative purely real number
Pass
Absolute value of a purely imaginary number with positive imaginary part
Pass
Absolute value of a purely imaginary number with negative imaginary part
Pass
Absolute value of a number with real and imaginary part
Pass
Conjugate a purely real number
Pass
Conjugate a purely imaginary number
Pass
Conjugate a number with real and imaginary part
Pass
Euler's identity/formula
Pass
Exponential of 0
Pass
Exponential of a purely real number
Pass
Exponential of a number with real and imaginary part
Pass
Exponential resulting in a number with real and imaginary part
Pass
Add real number to complex number
Pass
Add complex number to real number
Pass
Subtract real number from complex number
Pass
Subtract complex number from real number
Pass
Multiply complex number by real number
Pass
Multiply real number by complex number
Pass
Divide complex number by real number
Pass
Divide real number by complex number
Pass

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