Selectors separate interval representation from calculation.
An uncertain quantity can carry lower and upper bounds behind an interface, while arithmetic constructs a new interval that contains every endpoint result.
How can arithmetic use a range without depending on how its two bounds are stored?
- Construct an interval from lower and upper bounds
- Keep pair representation behind bound selectors
- Propagate bounds through interval addition
- Compare all endpoint products when signs differ
make-interval stores two bounds in a pair, but add-interval and mul-interval never inspect that pair directly. They ask lower-bound and upper-bound for components, preserving the same abstraction barrier used for rational numbers earlier in the chapter.
Addition combines the two lower bounds and the two upper bounds. Multiplication must consider all four endpoint products because the smallest or largest result can come from a different corner when an interval crosses zero. The result encloses possible products within these independent bounds; it is not a probability model.
(begin
(define (make-interval lower upper) (cons lower upper))
(define (lower-bound interval) (car interval))
(define (upper-bound interval) (cdr interval))
(define (add-interval left right)
(make-interval
(+ (lower-bound left) (lower-bound right))
(+ (upper-bound left) (upper-bound right))))
(define total
(add-interval (make-interval 1 2)
(make-interval 3 5)))
(list (lower-bound total) (upper-bound total)))- Output
- —
- Value
- —
- Diagnostic
- —
The first program returns (4 7). The second returns (-10 15).
Follow each selector call through the representation barrier. In multiplication, locate the four products -8, -10, 12, and 15 before min4 and max4 choose the returned bounds. These traces describe only the selected intervals.
Change the program before you read the hint.
Change the second interval from (4, 5) to (-5, -4). Predict all four endpoint products and the resulting lower and upper bounds.
Show one hint
Do not assume the first product is the lower bound. Compare all four values after multiplying the endpoints.