sicp.io
2.1.4 · 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 is the interval enclosure of those possible products.

SICP code461 of 1,048,576 UTF-8 bytes
Examples
Result
Output
Value
Diagnostic
Execution trace0 / 0 events
    Programs run in the browser with their result and execution trace.
    Expected result

    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. The traces record the exact selected intervals.

    Try it yourself

    Change the program and compare the result.

    Change the second interval from (4, 5) to (-5, -4). Predict all four endpoint products and the resulting lower and upper bounds.

    Show hint

    Do not assume the first product is the lower bound. Compare all four values after multiplying the endpoints.

    Complete this lesson

    0 of 20 lessons complete in this chapter0%