* Internal division that works on plain Intervals.
(a: Interval, b: Interval)
| 125 | * Internal division that works on plain Intervals. |
| 126 | */ |
| 127 | function _div(a: Interval, b: Interval): IntervalResult { |
| 128 | // Case 1: Divisor entirely positive or negative - safe division |
| 129 | if (b.lo > 0 || b.hi < 0) { |
| 130 | return ok(_mul(a, { lo: 1 / b.hi, hi: 1 / b.lo })); |
| 131 | } |
| 132 | |
| 133 | // Case 2: Divisor strictly contains zero - singularity |
| 134 | // For plotting, we signal this and let the algorithm subdivide |
| 135 | if (b.lo < 0 && b.hi > 0) { |
| 136 | return { kind: 'singular' }; |
| 137 | } |
| 138 | |
| 139 | // Case 3: Divisor is exactly [0, c] (touches zero at lower bound) |
| 140 | if (b.lo === 0 && b.hi > 0) { |
| 141 | // Dividing by [0+, c]: approaches +Infinity or -Infinity from one side |
| 142 | if (a.lo >= 0) { |
| 143 | // Positive / [0+, c] = [a.lo/c, +Infinity) |
| 144 | return { |
| 145 | kind: 'partial', |
| 146 | value: { lo: a.lo / b.hi, hi: Infinity }, |
| 147 | domainClipped: 'hi', |
| 148 | }; |
| 149 | } else if (a.hi <= 0) { |
| 150 | // Negative / [0+, c] = (-Infinity, a.hi/c] |
| 151 | return { |
| 152 | kind: 'partial', |
| 153 | value: { lo: -Infinity, hi: a.hi / b.hi }, |
| 154 | domainClipped: 'lo', |
| 155 | }; |
| 156 | } else { |
| 157 | // Mixed sign numerator - result is all reals |
| 158 | return { kind: 'entire' }; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // Case 4: Divisor is exactly [c, 0] (touches zero at upper bound) |
| 163 | if (b.hi === 0 && b.lo < 0) { |
| 164 | // Dividing by [c, 0-]: similar logic, opposite signs |
| 165 | if (a.lo >= 0) { |
| 166 | return { |
| 167 | kind: 'partial', |
| 168 | value: { lo: -Infinity, hi: a.lo / b.lo }, |
| 169 | domainClipped: 'lo', |
| 170 | }; |
| 171 | } else if (a.hi <= 0) { |
| 172 | return { |
| 173 | kind: 'partial', |
| 174 | value: { lo: a.hi / b.lo, hi: Infinity }, |
| 175 | domainClipped: 'hi', |
| 176 | }; |
| 177 | } else { |
| 178 | return { kind: 'entire' }; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | // Case 5: Divisor is exactly [0, 0] - division by zero |
| 183 | return { kind: 'empty' }; |
| 184 | } |