@rayyamhk/complex
Version:
A lightweight and easy-to-use library for you to manipulate complex numbers
32 lines (27 loc) • 878 B
JavaScript
;
/**
* Calculates the quotient of two Complex Number.<br><br>
*
* Note that if the denominator is considered as 0,
* returns Complex.NaN instead of Infinity.
* @memberof Complex
* @static
* @param {Complex} num1 - The Complex Number on the left of '/' operator.
* @param {Complex} num2 - The Complex Number on the right of '/' operator.
* @returns {Complex} The quotient of two Complex Numbers
*/
function divide(num1, num2) {
if (!(num1 instanceof this) || !(num2 instanceof this)) {
return this.NaN;
}
var a = num1.re;
var b = num1.im;
var c = num2.re;
var d = num2.im;
if (Math.abs(c) < this.EPSILON && Math.abs(d) < this.EPSILON) {
return this.NaN;
}
var denominator = Math.pow(c, 2) + Math.pow(d, 2);
return new this((a * c + b * d) / denominator, (b * c - a * d) / denominator);
}
module.exports = divide;