s LearnSqueak.com

SMALLTALK LAB

Measure great-circle distance with haversine

Calculate the shortest surface distance on a sphere, including points near the date line.

6 tested checkpointsNative Squeak screenshotsNative evaluation replay

Before you begin

Open a Workspace in native Squeak. Select one complete checkpoint and choose Print it.

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.

Expected values are Smalltalk expressions. The printer may display equivalent Smalltalk literals with different spaces or delimiters.

01 - Convert degrees to radians

Squeak trigonometric methods expect radians. Ninety degrees is π/2 radians.

Independent Workspace expression - select all, then Print it
((90 * Float pi / 180) - (Float pi / 2)) abs < 1.0e-12

Expected: true

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 1: Convert degrees to radians
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Try 180 degrees and compare with Float pi.

Download this checkpoint

02 - Use the half-angle term

For a quarter circle, squaring the sine of half the central angle gives roughly 0.5. Use a tolerance rather than an exact Float equality.

Independent Workspace expression - select all, then Print it
| theta |
theta := Float pi / 2.
(((theta / 2) sin squared) - 0.5) abs < 1.0e-12

Expected: true

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 2: Use the half-angle term
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Try theta = Float pi; the term becomes 1.

Download this checkpoint

03 - Check symmetry

Reversing the trip reverses the longitude difference. Squaring the sine cancels that sign. The latitude terms obey the same symmetry.

Independent Workspace expression - select all, then Print it
| a b |
a := (179 - (-179)) * Float pi / 180.
b := a negated.
((a / 2) sin squared - (b / 2) sin squared) abs < 1.0e-12

Expected: true

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 3: Check symmetry
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Replace 179 and -179 with 30 and 70.

Download this checkpoint

04 - Measure a quarter of the equator

At the equator the longitude separation here is a quarter turn. Multiply the recovered angle by 6,371 km and round only for display.

Independent Workspace expression - select all, then Print it
| distance |
distance := [:u :v :radius |
 | rad phi1 phi2 dphi dlambda a |
 rad := Float pi / 180.
 phi1 := u x * rad. phi2 := v x * rad.
 dphi := (v x - u x) * rad.
 dlambda := (v y - u y) * rad.
 a := ((dphi / 2) sin squared)
      + (phi1 cos * phi2 cos * (dlambda / 2) sin squared).
 a := (a max: 0.0) min: 1.0.
 2 * radius * a sqrt arcSin].
(distance value: (0@0) value: (0@90) value: 6371) rounded

Expected: 10008

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 4: Measure a quarter of the equator
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Use a radius of 1 and compare with Float pi / 2 before rounding.

Download this checkpoint

05 - Cross the international date line

Although the numeric longitudes differ by 358°, the sine-squared term captures the short 2° arc.

Independent Workspace expression - select all, then Print it
| distance |
distance := [:u :v :radius |
 | rad phi1 phi2 dphi dlambda a |
 rad := Float pi / 180.
 phi1 := u x * rad. phi2 := v x * rad.
 dphi := (v x - u x) * rad.
 dlambda := (v y - u y) * rad.
 a := ((dphi / 2) sin squared)
      + (phi1 cos * phi2 cos * (dlambda / 2) sin squared).
 a := (a max: 0.0) min: 1.0.
 2 * radius * a sqrt arcSin].
(distance value: (0@179) value: (0@(-179)) value: 6371) rounded

Expected: 222

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 5: Cross the international date line
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Reverse the two points. The same rounded distance should return.

Download this checkpoint

06 - Check the same and opposite points

Coincident points give zero; opposite points give half the circumference, π times the radius.

Independent Workspace expression - select all, then Print it
| distance |
distance := [:u :v :radius |
 | rad phi1 phi2 dphi dlambda a |
 rad := Float pi / 180.
 phi1 := u x * rad. phi2 := v x * rad.
 dphi := (v x - u x) * rad.
 dlambda := (v y - u y) * rad.
 a := ((dphi / 2) sin squared)
      + (phi1 cos * phi2 cos * (dlambda / 2) sin squared).
 a := (a max: 0.0) min: 1.0.
 2 * radius * a sqrt arcSin].
{(distance value: (0@0) value: (0@0) value: 6371) rounded.
 (distance value: (0@0) value: (0@180) value: 6371) rounded}

Expected: #(0 20015)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 6: Check the same and opposite points
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Change the last longitude to 90; the second result becomes 10008.

Download this checkpoint

Replay the checkpoints

NATIVE SQUEAK CAPTURE
Measure great-circle distance with haversine native Workspace replay
Replay of successive native Squeak Workspace evaluations, not an interaction recording. The static checkpoints appear above.

Reason about correctness and limits

Coordinates are latitude first, longitude second, in degrees. Use valid latitudes and a positive radius in the units you want returned.

This is a spherical model, not a road distance or an ellipsoid calculation. The clamp avoids roundoff outside the valid inverse-sine domain.

Three practice challenges

Predict one intermediate value

Try theta = Float pi; the term becomes 1.

Show solution

The half-angle is π/2, whose sine squared is 1.

Work one small variation

Use a radius of 1 and compare with Float pi / 2 before rounding.

Show solution

The distance before rounding is π/2 in the same units as the radius.

Check the edge of the model

Why is the answer not a driving distance?

Show solution

The model measures the shortest arc on a sphere. It does not model roads, terrain, or an ellipsoid.

Continue learning

First Point | First Rounding

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