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.
Source
operator/(Interval i) {
if (this.isEmpty() || i.isEmpty()) return new 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 new Interval.empty();
}
if (i.min < i.max && i.max == 0) {
// round down new min
return new Interval(this.max / i.min, double.INFINITY);
}
if (i.min < i.max && i.min == 0) {
// round up new max
return new Interval(double.NEGATIVE_INFINITY, this.max / i.max);
}
}
// Case 2: This interval contains zero.
if (this.containsZero()) {
return new Interval(double.NEGATIVE_INFINITY, double.INFINITY);
}
// Case 3: This interval is strictly positive.
if (this.max > 0) {
if (i.min == 0 && i.max == 0) {
// Result = empty set
return new Interval.empty();
}
if (i.min < i.max && i.max == 0) {
// round up new max
return new Interval(double.NEGATIVE_INFINITY, this.min / i.min);
}
if (i.min < i.max && i.min == 0) {
// round down new min
return new Interval(this.min / i.max, double.INFINITY);
}
}
throw new ArgumentError('Can not divide by 0');
}
return this * new Interval(1.0/i.max, 1.0/i.min);
}