operator / method

Interval operator /(
  1. Interval i
)

Performs an interval division.

[a, b] * [c, d] = [a, b] * (1/[c, d]) = [a, b] * [1/d, 1/c]

Note: Does not handle division by zero and throws an ArgumentError instead.

Implementation

Interval operator /(Interval i) {
  if (this.isEmpty() || i.isEmpty()) return Interval.empty();

  if (i.containsZero()) {
    // Fuck. Somebody is dividing by zero, the world is going to end.
    // Just kidding - we actually can handle this situation here.

    // Case 1: This interval is strictly negative.
    if (!this.isPositive()) {
      if (i.min == 0 && i.max == 0) {
        // Result = empty set
        return Interval.empty();
      }

      if (i.min < i.max && i.max == 0) {
        // round down new min
        return Interval(this.max / i.min, double.infinity);
      }

      if (i.min < i.max && i.min == 0) {
        // round up new max
        return Interval(double.negativeInfinity, this.max / i.max);
      }
    }

    // Case 2: This interval contains zero.
    if (this.containsZero()) {
      return Interval(double.negativeInfinity, double.infinity);
    }

    // Case 3: This interval is strictly positive.
    if (this.max > 0) {
      if (i.min == 0 && i.max == 0) {
        // Result = empty set
        return Interval.empty();
      }

      if (i.min < i.max && i.max == 0) {
        // round up new max
        return Interval(double.negativeInfinity, this.min / i.min);
      }

      if (i.min < i.max && i.min == 0) {
        // round down new min
        return Interval(this.min / i.max, double.infinity);
      }
    }
    throw ArgumentError('Can not divide by 0');
  }

  return this * Interval(1.0 / i.max, 1.0 / i.min);
}