SMALLTALK LAB
Bisection: trap a root in a shrinking interval
Find approximate roots in Squeak Smalltalk by preserving a sign-changing interval, inspect each halving, and test error bounds instead of guessing decimal digits.
Before you begin
Use a fresh Workspace in native Squeak 6.1. Select ONE complete checkpoint and choose Print it. Every checkpoint is independent, so no variables carry across steps. Know block arguments, comparisons and whileTrue:. We solve x squared minus 2 equals zero on 1..2. Unlike the binary-search lesson, there is no sorted collection or exact item to find: the state is a numeric interval containing a root of a continuous function. Inputs here are exact Integers/Fractions; only the final display is converted to Float. Continuity is a mathematical promise supplied by the caller, not something these few lines can prove.
These are native Squeak 6.1 examples. Each checkpoint is independent: open the download as text, copy one complete expression into Workspace and choose Print it. Do not file in the checkpoint collection as a class definition.
01 - Describe a question as a function
A root is an input whose output is zero. Writing the question as a block separates evaluating a candidate from choosing the next candidate. At 1 the square is too small; at 2 it is too large. These are exact Integer calculations.
| f |
f := [:x | (x * x) - 2].
{f value: 1. f value: 2. f value: 3}Expected: #(-1 2 7)

Try it: Change the final input to 0. The answer becomes -2, not a root.
02 - Establish the bracket
A continuous function with opposite signs at two endpoints has at least one zero between them. Multiply the endpoint outputs to check for opposite signs. A zero product also admits a root exactly at an endpoint. This does not imply that there is only one root.
| f lo hi |
f := [:x | (x * x) - 2].
lo := 1. hi := 2.
{lo < hi. (f value: lo) * (f value: hi) < 0}Expected: #(true true)

Try it: Try 2..3: both outputs are positive, so this interval cannot be accepted as a bracket by this method.
03 - Ask about the midpoint
The midpoint is halfway between the two endpoints. In Squeak, 3 / 2 is an exact Fraction. Its function value is 1 / 4, which has the same positive sign as the old upper endpoint. The negative endpoint must remain.
| f lo hi mid |
f := [:x | (x * x) - 2].
lo := 1. hi := 2. mid := (lo + hi) / 2.
{mid. f value: mid. (hi - lo) / 2}Expected: {3/2. 1/4. 1/2}

Try it: The new bracket is 1..3/2. Do not discard the endpoint merely because its absolute output is larger.
04 - Preserve opposite signs after one update
Compare the lower endpoint with the midpoint. If their signs differ, keep that left half. Otherwise keep the right half. Here we keep the left half. For this checkpoint the midpoint is known not to be an exact root; the reusable solver handles that case separately.
| f lo hi mid |
f := [:x | (x * x) - 2].
lo := 1. hi := 2. mid := (lo + hi) / 2.
(f value: lo) * (f value: mid) < 0
ifTrue: [hi := mid]
ifFalse: [lo := mid].
{lo. hi. hi - lo. (f value: lo) * (f value: hi) < 0}Expected: {1. 3/2. 1/2. true}

Try it: The width halved and the sign-changing property survived. Those are two different checks.
05 - Record the sequence rather than hide the loop
The trace stores a fresh three-element Array for each update: lower bound, upper bound and width. Six halvings reduce a width of 1 to 1/64. The resulting interval encloses the positive square root of 2; it is not a claim that its midpoint is exactly that irrational number.
| f lo hi trace |
f := [:x | (x * x) - 2]. lo := 1. hi := 2.
trace := OrderedCollection new.
6 timesRepeat: [
| mid |
mid := (lo + hi) / 2.
(f value: lo) * (f value: mid) < 0
ifTrue: [hi := mid] ifFalse: [lo := mid].
trace add: {lo. hi. hi - lo}].
{trace size. trace last}Expected: {6. {45/32. 91/64. 1/64}}

Try it: Inspect trace to see all six rows. Keep Arrays separate: reusing one mutable row would overwrite your history.
06 - Stop for an input error bound
If a root stays inside the bracket, the midpoint is no farther than half the interval width from some enclosed root. We stop when that bound is at most 1/1000. This is an input-position bound, not a bound on the function output. The loop below is specialized to our nonzero-midpoint square-root example.
| f lo hi tolerance count |
f := [:x | (x * x) - 2]. lo := 1. hi := 2.
tolerance := 1/1000. count := 0.
[(hi - lo) / 2 > tolerance] whileTrue: [
| mid |
mid := (lo + hi) / 2.
(f value: lo) * (f value: mid) < 0
ifTrue: [hi := mid] ifFalse: [lo := mid].
count := count + 1].
{count. (hi - lo) / 2. ((lo + hi) / 2) asFloat > 1.414}Expected: {9. 1/1024. true}

Try it: With tolerance 1/10000 the loop needs more halvings. Never use tolerance zero for a root that cannot be represented exactly.
07 - Package a checked solver block
This block accepts a function, ordered bounds and a positive tolerance. It returns the approximation and the number of midpoint evaluations. Endpoint roots need zero midpoint evaluations. Exact midpoint roots terminate immediately; unbracketed inputs and a 128-evaluation limit signal errors. This is a teaching solver for exact real arithmetic, not a Float/NaN/infinity library.
| solve result |
solve := [:function :left :right :tolerance |
| lo hi flo fhi mid fm count answer |
(left < right and: [tolerance > 0])
ifFalse: [Error signal: 'Need ordered bounds and positive tolerance'].
lo := left. hi := right. count := 0. answer := nil.
flo := function value: lo. fhi := function value: hi.
flo = 0 ifTrue: [answer := lo].
fhi = 0 ifTrue: [answer := hi].
(answer isNil and: [flo * fhi > 0])
ifTrue: [Error signal: 'Endpoints must bracket a root'].
[answer isNil and: [count < 128]] whileTrue: [
mid := (lo + hi) / 2. fm := function value: mid.
count := count + 1.
(fm = 0 or: [(hi - lo) / 2 <= tolerance])
ifTrue: [answer := mid]
ifFalse: [
flo * fm < 0
ifTrue: [hi := mid]
ifFalse: [lo := mid. flo := fm]]].
answer isNil ifTrue: [Error signal: 'Iteration limit reached'].
{answer. count}].
result := solve value: [:x | (x * x) - 2] value: 1 value: 2 value: 1/1000.
{result first. result second. (result first squared - 2) abs < (1/100)}Expected: {1449/1024. 10. true}

Try it: The count is 10, not 9: this solver counts midpoint evaluations, including the final accepted midpoint; checkpoint 6 counted interval updates.
08 - Test contracts and edge cases
Test endpoint roots, an exact midpoint root, a decreasing function, an invalid bracket and invalid tolerance. The square-root checks bound the actual approximation using independently known decimal bounds, not the same sign-selection branch. The assertion counter increments only after a condition succeeds; the final answer is true if all nine assertions pass.
| solve assert checks result rejected |
solve := [:function :left :right :tolerance |
| lo hi flo fhi mid fm count answer |
(left < right and: [tolerance > 0])
ifFalse: [Error signal: 'Need ordered bounds and positive tolerance'].
lo := left. hi := right. count := 0. answer := nil.
flo := function value: lo. fhi := function value: hi.
flo = 0 ifTrue: [answer := lo].
fhi = 0 ifTrue: [answer := hi].
(answer isNil and: [flo * fhi > 0])
ifTrue: [Error signal: 'Endpoints must bracket a root'].
[answer isNil and: [count < 128]] whileTrue: [
mid := (lo + hi) / 2. fm := function value: mid.
count := count + 1.
(fm = 0 or: [(hi - lo) / 2 <= tolerance])
ifTrue: [answer := mid]
ifFalse: [
flo * fm < 0
ifTrue: [hi := mid]
ifFalse: [lo := mid. flo := fm]]].
answer isNil ifTrue: [Error signal: 'Iteration limit reached'].
{answer. count}].
checks := 0.
assert := [:ok | ok ifFalse: [Error signal: 'Bisection check failed']. checks := checks + 1].
assert value: ((solve value: [:x | x - 1] value: 1 value: 2 value: 1/1000) = #(1 0)).
assert value: ((solve value: [:x | x - 2] value: 1 value: 2 value: 1/1000) = #(2 0)).
assert value: ((solve value: [:x | x - 3] value: 2 value: 4 value: 1/1000) = #(3 1)).
assert value: ((solve value: [:x | 3 - x] value: 2 value: 4 value: 1/1000) = #(3 1)).
result := solve value: [:x | x squared - 2] value: 1 value: 2 value: 1/10000.
assert value: (result first > (1414/1000)).
assert value: (result first < (1415/1000)).
rejected := [solve value: [:x | x squared + 1] value: 1 value: 2 value: 1/100. false]
on: Error do: [:error | error messageText = 'Endpoints must bracket a root'].
assert value: rejected.
rejected := [solve value: [:x | x] value: -1 value: 1 value: 0. false]
on: Error do: [:error | error messageText = 'Need ordered bounds and positive tolerance'].
assert value: rejected.
rejected := [solve value: [:x | x] value: 2 value: 1 value: 1/100. false]
on: Error do: [:error | error messageText = 'Need ordered bounds and positive tolerance'].
assert value: rejected.
checks = 9Expected: true

Try it: Add a tiny tolerance such as 1/(2 raisedTo: 200). The square-root example must signal Iteration limit reached rather than claim an accurate answer.
Watch the computed states

Download the visual experiment. Select the entire expression and Do it. Change stage := 0. to another supported stage, close the previous window, and run again. The GIF replays those native states. This example needs native Squeak; no new browser playground compatibility is claimed.
Reason about correctness and limits
Opposite signs are not enough without continuity. A jump from -1 to 1 can change sign without ever producing zero. Do not pass a discontinuous function and claim a root was proved.
An even-multiplicity root can touch zero without changing sign. For (x - 1) squared on 0..2 both endpoints are positive. Rejecting that bracket does not mean the function has no root.
A small function residual is not the same as a small input error. Scaling a function by a million changes its residual but not its root. This lesson stops by bracket width.
Exact Fractions avoid Float stagnation here, but their numerators and denominators can grow. For a production Float solver, add finite-number checks and detect mid equal to an endpoint; never silently promise an unachievable tolerance.
The solver assumes a deterministic function with no mutation between evaluations. It evaluates its inputs repeatedly. A function reading changing state can invalidate the bracket.
Do not compare results with exact equality to 2 sqrt. Use a tolerance or an independently justified enclosure. The approximation is deliberately a rational number.
Three practice challenges
A different square root
Change the function to x squared - 3 and use the bracket 1..2. Does the returned value lie between 1.731 and 1.733?
Show solution
Use checkpoint 7 with [:x | x squared - 3] and tolerance 1/1000. Keep the same bracket. Test result first > (1731/1000) and result first < (1733/1000); do not require exact equality to a rounded decimal.
A root on the negative side
Solve x squared - 2 on -2..-1. Explain why the same sign test still works.
Show solution
The root is negative. The test compares signs, not a rule that the left value must be negative. With tolerance 1/1000 the result is -1449/1024 and 10 midpoint evaluations.
Build a native interval display
Draw a number line from 1 to 2 and replay the six bracket rows from checkpoint 5. What should stay fixed?
Show solution
Keep the mapping fixed: screenX := 40 + ((value - 1) * 600). Draw the original line once, then map each row first and second to its bracket bar. Show the row third as width. Do not autoscale every row, which would make the shrinking interval appear constant in size.
Continue learning
Binary Search | Fractions | Blocks | Testing
Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Windows.