Arithmetic/Complex: Difference between revisions

Content added Content deleted
m (→‎{{header|C}}: bug fix)
(→‎{{header|JavaScript}}: support complex conjugate and fix bug in toString())
Line 1,351: Line 1,351:
var denom = Math.pow(z.r,2) + Math.pow(z.i,2);
var denom = Math.pow(z.r,2) + Math.pow(z.i,2);
return new Complex(z.r/denom, -1*z.i/denom);
return new Complex(z.r/denom, -1*z.i/denom);
}

Complex.conjugate = function(z) {
return new Complex(z.r, -1*z.i);
}
}


Line 1,357: Line 1,361:


Complex.prototype.toString = function() {
Complex.prototype.toString = function() {
return (this.r !== 0 ? this.r : "")
return this.r === 0 && this.i === 0
+ (this.r !== 0 && this.i !== 0
? "0"
? (this.i > 0 ? " + " : " - ")
: (this.r !== 0 ? this.r : "")
+ ((this.r !== 0 || this.i < 0) && this.i !== 0
? (this.i > 0 ? "+" : "-")
: "" ) + ( this.i !== 0 ? Math.abs(this.i) + "i" : "" );
: "" ) + ( this.i !== 0 ? Math.abs(this.i) + "i" : "" );
}
}