Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | import { IComplex, IComplexConstructor } from '../internal/complex'; import mask from '../internal/mask'; import getAbs from './getAbs'; import getArg from './getArg'; import getImag from './getImag'; import getReal from './getReal'; export default function div<T extends IComplex> (Complex: IComplexConstructor<T>, lhs: IComplex, r: IComplex | number, i = 0): T { const rhs = typeof r === 'number' ? new Complex(r, i, NaN, NaN, mask.HAS_CARTESIAN) : r; const _mask = lhs._mask & rhs._mask; let rhsAbs2: number; switch (_mask) { case mask.HAS_ALL: case mask.HAS_CARTESIAN | mask.HAS_ABS: rhsAbs2 = rhs._abs * rhs._abs; return new Complex( (lhs._real * rhs._real + lhs._imag * rhs._imag) / rhsAbs2, (lhs._imag * rhs._real - lhs._real * rhs._imag) / rhsAbs2, lhs._abs / rhs._abs, lhs._arg - rhs._arg, _mask ); case mask.HAS_CARTESIAN | mask.HAS_ARG: rhsAbs2 = rhs._real * rhs._real + rhs._imag * rhs._imag; return new Complex( (lhs._real * rhs._real + lhs._imag * rhs._imag) / rhsAbs2, (lhs._imag * rhs._real - lhs._real * rhs._imag) / rhsAbs2, NaN, lhs._arg - rhs._arg, _mask ); case mask.HAS_CARTESIAN: case mask.HAS_REAL: case mask.HAS_IMAG: rhsAbs2 = getReal(rhs) * rhs._real + getImag(rhs) * rhs._imag; return new Complex( (getReal(lhs) * rhs._real + getImag(lhs) * rhs._imag) / rhsAbs2, (lhs._imag * rhs._real - lhs._real * rhs._imag) / rhsAbs2, NaN, NaN, mask.HAS_CARTESIAN ); default: return new Complex( NaN, NaN, getAbs(lhs) / getAbs(rhs), getArg(lhs) - getArg(rhs), mask.HAS_POLAR ); } } |