(x: Interval | IntervalResult)
| 61 | * Need to handle sign change at 0 since x^2 is not monotonic. |
| 62 | */ |
| 63 | export function square(x: Interval | IntervalResult): IntervalResult { |
| 64 | const unwrapped = unwrapOrPropagate(x); |
| 65 | if (!Array.isArray(unwrapped)) return unwrapped; |
| 66 | const [xVal] = unwrapped; |
| 67 | if (xVal.lo >= 0) { |
| 68 | // Entirely non-negative: monotonically increasing |
| 69 | return ok({ lo: xVal.lo * xVal.lo, hi: xVal.hi * xVal.hi }); |
| 70 | } else if (xVal.hi <= 0) { |
| 71 | // Entirely non-positive: monotonically decreasing, flip bounds |
| 72 | return ok({ lo: xVal.hi * xVal.hi, hi: xVal.lo * xVal.lo }); |
| 73 | } else { |
| 74 | // Interval contains 0 - minimum is 0 |
| 75 | return ok({ lo: 0, hi: Math.max(xVal.lo * xVal.lo, xVal.hi * xVal.hi) }); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Integer power helper for non-negative integer exponents. |
no test coverage detected