SMALLTALK LAB
Newton roots: improve a guess with a tangent
Build a bounded Newton iteration for square roots, watch the residual shrink, and understand when a fast numerical method can fail.
Before you begin
Know arithmetic, blocks and whileTrue:. This lesson solves x squared = a for a nonnegative real target. Unlike bisection, Newton uses a derivative and does not maintain a bracketing interval.
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 - Measure how wrong the guess is
For target two, f(x)=x*x-2 measures the signed residual. A residual of zero means the equation is satisfied; it is not the same quantity as distance from the true root.
| x |
x := 2.0.
x*x-2Expected: 2.0

Try it: Try x=1.0: the residual changes sign.
02 - Use the slope at the current guess
The derivative of x squared minus a is 2*x. Dividing residual by slope estimates how far to move along the tangent to reach zero. Parentheses make the intended arithmetic explicit.
| x residual slope |
x := 2.0. residual := x*x-2. slope := 2*x.
x-(residual/slope)Expected: 1.5

Try it: Check that the tangent correction is 0.5.
03 - Simplify the update
For square roots, x-f(x)/f-prime(x) simplifies to (x+a/x)/2. Both forms agree here. Floating-point division is deliberate: exact Fractions would grow rapidly.
| x a |
x := 2.0. a := 2.
((x+(a/x))/2) = (x-((x*x-a)/(2*x)))Expected: true

Try it: Use a=9 and x=4.0 to obtain 3.125.
04 - Repeat a fixed number of improvements
Record the starting guess and three updates. This is a trace, not yet a stopping rule. The error falls quickly near this simple positive root.
| x values |
x := 2.0. values := OrderedCollection with: x.
3 timesRepeat: [x := (x+(2/x))/2. values add: x].
values collect: [:v | (v*1000000) rounded]Expected: {2000000. 1500000. 1416667. 1414216.} asOrderedCollection

Try it: The scaled integers make a stable readable trace; x still stores the full Float.
05 - Stop by residual, with a hard limit
The block validates its starting conditions, tests residual before each update, and caps work at fifty iterations. A limit is essential for numerical algorithms: do not assume every input converges.
| root |
root := [:target :start | | x next iterations |
target < 0 ifTrue: [Error signal: 'Nonnegative target required'].
start <= 0 ifTrue: [Error signal: 'Positive starting guess required'].
x := start asFloat. iterations := 0.
[(x*x-target) abs > 0.00000001 and: [iterations < 50]] whileTrue: [
next := (x+(target/x))/2.0.
x := next. iterations := iterations+1].
(x*x-target) abs > 0.00000001
ifTrue: [Error signal: 'Iteration limit reached'].
x].
((root value: 2 value: 2.0) squared-2) abs < 0.00000001Expected: true

Try it: Try targets 3, 9 and 25 with a positive guess.
06 - Compare against an independent square root
Use Squeak’s built-in sqrt as a check across positive targets. A residual threshold is our solver contract; a separate answer-distance check helps catch mistakes in the update.
| root |
root := [:target :start | | x next iterations |
target < 0 ifTrue: [Error signal: 'Nonnegative target required'].
start <= 0 ifTrue: [Error signal: 'Positive starting guess required'].
x := start asFloat. iterations := 0.
[(x*x-target) abs > 0.00000001 and: [iterations < 50]] whileTrue: [
next := (x+(target/x))/2.0.
x := next. iterations := iterations+1].
(x*x-target) abs > 0.00000001
ifTrue: [Error signal: 'Iteration limit reached'].
x].
#(1 2 3 9 25 100) allSatisfy: [:a |
((root value: a value: 1.0)-a sqrt) abs < 0.000001]Expected: true

Try it: Starting at one takes more iterations for a large target but still works for this test set.
07 - Handle the zero target carefully
Zero is a repeated root: the derivative also vanishes at the exact solution. A positive guess avoids division by zero; each update halves it. Residual tolerance therefore permits a small positive answer.
| root answer |
root := [:target :start | | x next iterations |
target < 0 ifTrue: [Error signal: 'Nonnegative target required'].
start <= 0 ifTrue: [Error signal: 'Positive starting guess required'].
x := start asFloat. iterations := 0.
[(x*x-target) abs > 0.00000001 and: [iterations < 50]] whileTrue: [
next := (x+(target/x))/2.0.
x := next. iterations := iterations+1].
(x*x-target) abs > 0.00000001
ifTrue: [Error signal: 'Iteration limit reached'].
x].
answer := root value: 0 value: 1.0.
{answer > 0. answer squared <= 0.00000001}Expected: {true. true.}

Try it: A tiny squared residual does not imply the same tiny answer error near zero.
08 - Reject a zero starting guess
An unguarded update would divide by zero. Test the precise validation message rather than accepting any exception as evidence of correct behavior.
| root |
root := [:target :start | | x next iterations |
target < 0 ifTrue: [Error signal: 'Nonnegative target required'].
start <= 0 ifTrue: [Error signal: 'Positive starting guess required'].
x := start asFloat. iterations := 0.
[(x*x-target) abs > 0.00000001 and: [iterations < 50]] whileTrue: [
next := (x+(target/x))/2.0.
x := next. iterations := iterations+1].
(x*x-target) abs > 0.00000001
ifTrue: [Error signal: 'Iteration limit reached'].
x].
[root value: 2 value: 0. false]
on: Error do: [:e | e messageText = 'Positive starting guess required']Expected: true

Try it: Negative targets are outside this real-valued solver’s contract.
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
This is a square-root-specific Newton solver, not a universal root finder. General Newton steps can diverge, oscillate or meet zero derivatives. Bisection is slower but retains a guarantee when a valid continuous sign-changing bracket is maintained.
The residual tolerance is absolute and chosen for small teaching inputs. For widely varying scales use suitable relative/absolute tolerances and finite-value checks. Very large Float inputs may overflow; no robust industrial numerical-library claim is made.
Quadratic local convergence applies near simple roots under suitable smoothness assumptions. The zero target has a multiple root and behaves differently. The fifty-step cap bounds work but cannot guarantee success for every magnitude.
Three practice challenges
Challenge 1
Why must the starting guess be positive here?
Show solution
It prevents the first division by zero and keeps this square-root iteration on the positive branch.
Challenge 2
What happens for target zero?
Show solution
The update becomes x/2, so the guess approaches zero linearly rather than the typical simple-root quadratic behavior.
Challenge 3
How would you expose convergence information?
Show solution
Return the answer together with iteration count and residual, or signal a clear failure when the work limit is exhausted.
Continue learning
Bisection | Newton Interpolation | Fractions
Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.