SMALLTALK LAB
Measure great-circle distance with haversine
Calculate the shortest surface distance on a sphere, including points near the date line.
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.
((90 * Float pi / 180) - (Float pi / 2)) abs < 1.0e-12Expected: true

Try it: Try 180 degrees and compare with Float pi.
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.
| theta |
theta := Float pi / 2.
(((theta / 2) sin squared) - 0.5) abs < 1.0e-12Expected: true

Try it: Try theta = Float pi; the term becomes 1.
03 - Check symmetry
Reversing the trip reverses the longitude difference. Squaring the sine cancels that sign. The latitude terms obey the same symmetry.
| a b |
a := (179 - (-179)) * Float pi / 180.
b := a negated.
((a / 2) sin squared - (b / 2) sin squared) abs < 1.0e-12Expected: true

Try it: Replace 179 and -179 with 30 and 70.
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.
| 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) roundedExpected: 10008

Try it: Use a radius of 1 and compare with Float pi / 2 before rounding.
05 - Cross the international date line
Although the numeric longitudes differ by 358°, the sine-squared term captures the short 2° arc.
| 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) roundedExpected: 222

Try it: Reverse the two points. The same rounded distance should return.
06 - Check the same and opposite points
Coincident points give zero; opposite points give half the circumference, π times the radius.
| 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)

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

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
Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.