| 4 | 4 | // |
| 5 | 5 | |
| 6 | 6 | export 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; |
| 9 | 10 | } |
| 10 | 11 | |
| 11 | 12 | get real() { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 13 | return this._real; |
| 13 | 14 | } |
| 14 | 15 | |
| 15 | 16 | get imag() { |
| 16 | | throw new Error('Remove this line and implement the function'); |
| 17 | return this._imag; |
| 17 | 18 | } |
| 18 | 19 | |
| 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 | ); |
| 21 | 26 | } |
| 22 | 27 | |
| 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 | ); |
| 25 | 34 | } |
| 26 | 35 | |
| 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); |
| 29 | 41 | } |
| 30 | 42 | |
| 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); |
| 33 | 54 | } |
| 34 | 55 | |
| 35 | 56 | 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); |
| 37 | 59 | } |
| 38 | 60 | |
| 39 | 61 | 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); |
| 41 | 66 | } |
| 42 | 67 | |
| 43 | 68 | 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); |
| 45 | 74 | } |
| 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 |
| 46 | 77 | } |