s LearnSqueak.com

SMALLTALK LAB

Continued fractions: turn a ratio into a short recipe

Extract integer parts and reciprocals, rebuild exact ratios, and examine convergents using Squeak’s exact Fraction arithmetic.

8 tested checkpointsNative Squeak screenshotsComputed visual replay

Before you begin

Know fractions, loops and blocks. A finite simple continued fraction [a0; a1, a2, ...] means a0 + 1/(a1 + 1/(a2 + ...)). We use nonnegative exact rational values.

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 - Separate the whole part

The floor is the first term. Subtract it to leave a fractional remainder. Here 43/19 begins with two and leaves 5/19.

Independent Workspace expression - select all, then Print it
| x a |
x := 43/19. a := x floor.
{a. x-a}

Expected: {2. 5/19.}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 1: Separate the whole part
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: The remainder is at least zero and below one.

Download this checkpoint

02 - Invert the remainder

Taking the reciprocal moves the remainder into the next whole-part extraction. The reciprocal of 5/19 is 19/5, whose floor is three.

Independent Workspace expression - select all, then Print it
| x |
x := (5/19) reciprocal.
{x. x floor. x-x floor}

Expected: {19/5. 3. 4/5.}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 2: Invert the remainder
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Never invert zero; zero means the expansion is complete.

Download this checkpoint

03 - Extract every term

Repeat whole-part extraction until the remainder becomes zero. The loop condition also performs the extraction, so even an integer receives a one-term result. Exact rational arithmetic guarantees finite termination for these inputs.

Independent Workspace expression - select all, then Print it
| terms |
terms := [:value | | x out a |
 (value isInteger or: [value isKindOf: Fraction])
  ifFalse: [Error signal: 'Use an exact rational'].
 value < 0 ifTrue: [Error signal: 'Use a nonnegative rational'].
 x := value. out := OrderedCollection new.
 [a := x floor. out add: a. x := x-a. x = 0]
  whileFalse: [x := x reciprocal].
 out asArray].
terms value: 43/19

Expected: {2. 3. 1. 4.}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 3: Extract every term
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Follow the remainders manually: 5/19, 4/5, 1/4, then zero.

Download this checkpoint

04 - Handle integers and zero

A whole number has no fractional remainder. Zero is also a valid one-term continued fraction. These cases test the termination check before any reciprocal operation.

Independent Workspace expression - select all, then Print it
| terms |
terms := [:value | | x out a |
 (value isInteger or: [value isKindOf: Fraction])
  ifFalse: [Error signal: 'Use an exact rational'].
 value < 0 ifTrue: [Error signal: 'Use a nonnegative rational'].
 x := value. out := OrderedCollection new.
 [a := x floor. out add: a. x := x-a. x = 0]
  whileFalse: [x := x reciprocal].
 out asArray].
{terms value: 7. terms value: 0}

Expected: {#(7). #(0)}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 4: Handle integers and zero
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: No reciprocal of zero occurs in either expansion.

Download this checkpoint

05 - Rebuild from the right

Start at the final term. Walking backward, take the reciprocal of the current result and add the preceding term. The array here comes from the extractor and has valid positive tail terms.

Independent Workspace expression - select all, then Print it
| rebuild |
rebuild := [:sequence | | result |
 sequence isEmpty ifTrue: [Error signal: 'Supply at least one term'].
 result := sequence last.
 sequence size-1 to: 1 by: -1 do: [:i |
  result := (sequence at: i)+result reciprocal].
 result].
rebuild value: #(2 3 1 4)

Expected: 43/19

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 5: Rebuild from the right
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Rebuilding arbitrary unvalidated arrays can divide by zero; validate a public input format.

Download this checkpoint

06 - Compare successive convergents

A prefix of the terms gives a convergent. These prefixes approach the target but need not approach from only one side. The last prefix equals the original rational exactly.

Independent Workspace expression - select all, then Print it
| rebuild items |
rebuild := [:sequence | | result |
 sequence isEmpty ifTrue: [Error signal: 'Supply at least one term'].
 result := sequence last.
 sequence size-1 to: 1 by: -1 do: [:i |
  result := (sequence at: i)+result reciprocal].
 result].
items := #(2 3 1 4).
(1 to: items size) collect: [:n | rebuild value: (items copyFrom: 1 to: n)]

Expected: {2. 7/3. 9/4. 43/19.}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 6: Compare successive convergents
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Subtract 43/19 from each result to see the alternating error signs in this example.

Download this checkpoint

07 - Round-trip many exact ratios

Compare extraction followed by rebuilding across a grid of numerators and denominators. Squeak automatically reduces fractions, so mathematically equal ratios compare equal.

Independent Workspace expression - select all, then Print it
| terms rebuild |
terms := [:value | | x out a |
 (value isInteger or: [value isKindOf: Fraction])
  ifFalse: [Error signal: 'Use an exact rational'].
 value < 0 ifTrue: [Error signal: 'Use a nonnegative rational'].
 x := value. out := OrderedCollection new.
 [a := x floor. out add: a. x := x-a. x = 0]
  whileFalse: [x := x reciprocal].
 out asArray].
rebuild := [:sequence | | result |
 sequence isEmpty ifTrue: [Error signal: 'Supply at least one term'].
 result := sequence last.
 sequence size-1 to: 1 by: -1 do: [:i |
  result := (sequence at: i)+result reciprocal].
 result].
(0 to: 20) allSatisfy: [:n |
 (1 to: 12) allSatisfy: [:d |
  (rebuild value: (terms value: n/d)) = (n/d)]]

Expected: true

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 7: Round-trip many exact ratios
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Include larger coprime numerator and denominator values as additional checks.

Download this checkpoint

08 - Reject floating-point input

A Float is an approximation and is deliberately outside this extractor's contract. Without a precision policy, a decimal approximation may create a long expansion that looks more meaningful than it is.

Independent Workspace expression - select all, then Print it
| terms |
terms := [:value | | x out a |
 (value isInteger or: [value isKindOf: Fraction])
  ifFalse: [Error signal: 'Use an exact rational'].
 value < 0 ifTrue: [Error signal: 'Use a nonnegative rational'].
 x := value. out := OrderedCollection new.
 [a := x floor. out add: a. x := x-a. x = 0]
  whileFalse: [x := x reciprocal].
 out asArray].
[terms value: 2.5. false]
 on: Error do: [:e | e messageText = 'Use an exact rational']

Expected: true

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 8: Reject floating-point input
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Use the exact fraction 5/2 instead; its terms are #(2 2).

Download this checkpoint

Watch the computed states

NATIVE SQUEAK CAPTURE
Continued fractions: turn a ratio into a short recipe native visual trace
Replay of actual Morphic window captures at successive computed stages; not mouse footage.

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

The algorithm is closely related to Euclid’s repeated division, but records the quotients to describe a rational rather than only compute a gcd. Its number of terms depends on the ratio; large integer arithmetic contributes additional cost.

This lesson does not approximate irrational numbers or promise an error bound for Float input. An irrational expansion is generally infinite and needs an explicit stopping rule.

A rational may have two finite simple expansions, such as [2; 2] and [2; 1, 1]. This extraction ends naturally on a final integer; compare reconstructed values rather than assuming every possible notation is unique.

Three practice challenges

Challenge 1

What is the continued fraction of 5/2?

Show solution

The whole part is two; the remaining half becomes two after inversion. The result is #(2 2).

Challenge 2

Why rebuild from the right?

Show solution

The final reciprocal is nested deepest. Evaluating from that inside value outward follows the mathematical parentheses.

Challenge 3

Can a final term of zero be accepted blindly?

Show solution

No. It can cause a reciprocal of zero during reconstruction. Validate arbitrary term arrays; the tested extractor produces valid tails.

Continue learning

Fractions | Euclidean Gcd | Bisection

Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.