1

我在 java-script 中使用原型创建了一个复杂函数的乘法

function ComplexNumber(real,imaginary) {
    this.real = real;
    this.imaginary = imaginary;
}
ComplexNumber.prototype = {
mult: function() {
         var multiplier = arguments[0];
         if(arguments.length != 1)
                multiplier = new ComplexNumber(arguments[0], arguments[1]);
         return new ComplexNumber(this.real * multiplier.real - this.imaginary * multiplier.imaginary,                    
         this.real * multiplier.imaginary + this.imaginary * multiplier.real);             
    },
 toString: function() {
       return this.real + " + " + this.imaginary + "j";
        }
 },
var complexA = new ComplexNumber(5, 8),
complexB = new ComplexNumber(3, 4),
complexSum = complexA.mult(complexB);
console.log(complexSum.toString());

我必须创建我尝试过的复数除法,但它不起作用......以下是我的 div 函数:

div : function(){
         var multiplier = arguments[0];
         if(arguments.length != 1)
                multiplier = new ComplexNumber(arguments[0], arguments[1]);

        return new ComplexNumber((this.real * multiplier.real - this.imaginary * multiplier.imaginary,                    
         this.real * multiplier.imaginary + this.imaginary * multiplier.real)/( ((this.real)*(this.real))) +((this.multiplier)*(this.multiplier)));
    },
4

1 回答 1

1

根据

(a+ib)/(c+id)=(ac+bd+i(bc-ad))/(c^2+d^2)

公式,你需要这个:

var det = multiplier.real*multiplier.real + multiplier.imaginary * multiplier.imaginary;
return new ComplexNumber(
  (this.real * multiplier.real + this.imaginary * multiplier.imaginary)/det,                    
  (this.imaginary * multiplier.real - this.real * multiplier.imaginary)/det
);
于 2012-06-23T07:08:28.653 回答