Lab · c01.s01.L1
Contract checker
A returned value can keep every line of a four-line contract without being exact.
Question: What may a caller rely on when a procedure returns, and what must the caller never assume?
Frame 1 of 14: Four linesA contract is a promise on four lines: what you may pass in, what comes back, what stays the same, and what is not promised. The recipe is the square-root update x_next = (x + n / x) / 2, stopped when |x*x - n| < tolerance. The fourth line is part of the promise, not an apology for it.
Four lines. A contract is a promise on four lines: what you may pass in, what comes back, what stays the same, and what is not promised. The recipe is the square-root update x_next = (x + n / x) / 2, stopped when |x*x - n| < tolerance. The fourth line is part of the promise, not an apology for it.
Keyboard: Space plays or pauses, Left and Right step, Home goes to the first frame, End goes to the last frame.
Text description of this lab
A row of guesses for the square root of nine climbs from 1.0000 to 5.0000, then settles through 3.4000 and 3.0235 to 3.0001. Beside it, four contract lines are checked in turn: the input is accepted, the square of the estimate lies within the tolerance 0.001, the input is unchanged, and exactness is not promised. A looser tolerance stops earlier. The fourth line never becomes a promise: rely on the tolerance, never on equality.
Return to the section that hosts this lab
Caption status: re-simulated. Every lab declares one of published, re-simulated, schematic, synthetic or interpolated.