(Lispex)sicp.io
2.8 · Interval arithmetic

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.

Guiding question

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.

Lispex · SICP sourceScheme-compatible SICP syntax executed by the Lispex SICP profile.
(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)))
Lispex learning runtimeLispex SICP profile 1.0.0
Loading Lispex SICP runtime
Lispex · SICP source461 / 1,048,576 UTF-8 bytes
Examples
Result
Output
Value
Diagnostic
Visible execution0 / 0 trace events
    This browser result is not a Lispex Vouch record or authority.wasm —
    Expected observation

    The first program returns (4 7). The second returns (-10 15).

    Trace focus

    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.

    Try it yourself

    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.