operator / method

  1. @override
Cartesian operator /(
  1. Object divisor
)
override

Returns a Complex whose value is (this / divisor). Implements the definitional formula

  a + bi       ac + bd + (bc - ad)i
---------- = ------------------------
  c + di           c^2 + d^2

but uses prescaling of operands to limit the effects of overflows and underflows in the computation.

Infinite and NaN values are handled according to the following rules, applied in the order presented:

  • If either this or divisor has a NaN value in either part, nan is returned.
  • If divisor equals zero, nan is returned.
  • If this and divisor are both infinite, nan is returned.
  • If this is finite (i.e., has no Infinite or NAN parts) and divisor is infinite (one or both parts infinite), zero is returned.
  • If this is infinite and divisor is finite, NAN values are returned in the parts of the result if the double rules applied to the definitional formula force NaN results.

Implementation

@override
Cartesian operator /(Object divisor) {
  if (divisor is Complex) {
    if (isNaN || divisor.isNaN) {
      return Complex.nan;
    }

    final c = divisor.real;
    final d = divisor.imaginary;
    if (c == 0.0 && d == 0.0) {
      return Complex.nan;
    }

    if (divisor.isInfinite && !isInfinite) {
      return Complex.zero;
    }

    if (c.abs() < d.abs()) {
      final q = c / d;
      final denominator = c * q + d;
      return Cartesian(
        (real * q + imaginary) / denominator,
        (imaginary * q - real) / denominator,
      );
    } else {
      final q = d / c;
      final denominator = d * q + c;
      return Cartesian(
        (imaginary * q + real) / denominator,
        (imaginary - real * q) / denominator,
      );
    }
  } else if (divisor is num) {
    if (isNaN || divisor.isNaN) {
      return Complex.nan;
    }
    if (divisor == 0) {
      return Complex.nan;
    }
    if (divisor.isInfinite) {
      return isFinite ? Complex.zero : Complex.nan;
    }
    return Cartesian(real / divisor, imaginary / divisor);
  } else {
    throw ArgumentError('factor must be a num or a Complex');
  }
}