Bearings

Great circle on a sphere, FM 3-25.26 and Bowditch · checked against GeographicLib's GeodSolve

What it is

A bearing is only meaningful with its north. A map has three: true north along the meridian, grid north along the map’s grid lines, and magnetic north where the compass points. Converting between them means adding or subtracting the angles between them, and the sign of each angle is where code goes wrong.

True north, grid north 5 degrees east of it, and magnetic north 20 degrees east of true, with grid convergence, the G-M angle and declination marked between them
  • Grid convergence is true to grid. It’s zero on a UTM zone’s centre line and grows toward the zone’s edges and the poles.
  • Declination is true to magnetic. It comes from the magnetic model.
  • The G-M angle is grid to magnetic: declination minus convergence. Military maps print it in the margin.

Operations

OperationGives
inverse The distance and initial bearing between two pointsIn: fromLatitudeInDegrees, fromLongitudeInDegrees, toLatitudeInDegrees, toLongitudeInDegrees
Out: distanceInMeters, bearingInDegrees
destination The point at a distance and bearing from anotherIn: fromLatitudeInDegrees, fromLongitudeInDegrees, distanceInMeters, bearingInDegrees
Out: toLatitudeInDegrees, toLongitudeInDegrees
backAzimuth The reverse of a bearingIn: bearingInDegrees
Out: backAzimuthInDegrees
turn The shortest turn from one bearing to anotherIn: fromBearingInDegrees, toBearingInDegrees
Out: turnInDegrees, direction
convertNorth A bearing converted between true, magnetic and grid northIn: bearingInDegrees, fromNorth, toNorth, magneticDeclinationInDegrees, convergenceInDegrees
Out: convertedBearingInDegrees
formatBearing A bearing written in whole degrees or milsIn: bearingInDegrees, angleUnit
Out: bearingText
compassPoint The nearest of 4, 8, 16 or 32 compass pointsIn: bearingInDegrees, pointCount
Out: compassPointText

Fields

FieldMeaning
fromLatitudeInDegreesThe start, −90 to 90
fromLongitudeInDegreesThe start, −180 to 180
toLatitudeInDegreesThe end, −90 to 90
toLongitudeInDegreesThe end, −180 to 180
distanceInMetersAlong the great circle, on the sphere; 0 or more
bearingInDegrees0 to 360, clockwise from north. inverse leaves it out where it's undefined: the same point, antipodes, a pole, or due north, where 0 and 360 are both right.
backAzimuthInDegreesThe bearing plus 180°, in 0 up to 360, so the back azimuth of 180° is 0°
fromBearingInDegreesThe heading now, 0 to 360
toBearingInDegreesThe heading wanted, 0 to 360
turnInDegreesThe smaller turn, 0 to 180
directionright, left, or none for no turn. A turn of exactly 180° is right.
fromNorthtrue, magnetic or grid
toNorthtrue, magnetic or grid
magneticDeclinationInDegreesTrue to magnetic, east positive, from the magnetic model: true = magnetic + declination
convergenceInDegreesTrue to grid, east positive, from UTM: true = grid + convergence. The G-M angle is declination minus convergence.
convertedBearingInDegrees0 up to 360
angleUnitdegrees, or mils at 6400 to the circle
bearingTextDegrees rounded half up to a whole degree, three digits and °, 000° to 359°; or mils rounded half up, four digits, 0000 to 6399. Round first, then wrap: 359.6° is 000°.
pointCount4, 8, 16 or 32
compassPointTextBowditch's abbreviation for the nearest point, like N, NNE or NE by N. Half-way between two points goes clockwise.

Edge cases

CaseWhat it tests
edge-inverse-across-antimeridianinverse179.5° to −179.5° is 110 km east, not most of the way round.
edge-inverse-due-northinverseBearing left out: 0 and 360 are both right.
edge-inverse-same-pointinverseDistance 0; the bearing is undefined.
edge-inverse-antipodesinverseHalf the circumference; every bearing is right, so none is compared.
edge-inverse-from-north-poleinverseFrom a pole the bearing depends on the convention, so only distance is compared.
edge-inverse-one-metreinverseShort distances keep their precision.
edge-destination-over-the-poledestinationDue north past the pole comes down the other side: longitude −170.
fm-3-25-26-figure-6-10convertNorthFM 3-25.26: magnetic 210° with a 12° east G-M angle is grid 222°.
fm-3-25-26-figure-6-12convertNorthFM 3-25.26: grid 2° with a 16° east G-M angle wraps to magnetic 346°.
fm-3-25-26-figure-6-15convertNorthFM 3-25.26: magnetic 5° with a 12° west G-M angle wraps to grid 353°.
hand-convert-north-1convertNorthTrue = magnetic + declination.
hand-convert-north-15convertNorthTrue = grid + convergence.
hand-convert-north-rounds-to-360convertNorthA hair under 360° rounds to 360°, which is 0°: answers run 0 up to 360.
bowditch-points-4-359-9compassPointJust west of north is still N.
bowditch-points-16-11-25compassPointHalf-way between N and NNE goes clockwise: NNE.
bowditch-points-32-11-25compassPointBowditch's abbreviation: N by E.
hand-back-azimuth-180backAzimuthThe back azimuth of 180° is 0°, not 360°.
hand-turn-350-to-10turnAcross north: 20° right, not 340° left.
hand-turn-0-to-180turnExactly 180° is right, by this file's rule.
hand-turn-90-to-90turnNo turn: direction none.
hand-format-degrees-0-5formatBearingHalf up: 0.5° is 001°.
hand-format-degrees-359-6formatBearingRound, then wrap: 359.6° is 000°, never 360°.
hand-format-mils-359-98formatBearing6399.64 mils rounds to 6400, which wraps to 0000.
invalid-format-bearing-nanformatBearingNaN has no bearing to write: outOfRange, not a crash or NaN°.

Their inputs and expected values are in vectors.json; search for the case's id.

For agents

Measure on a sphere of radius 6,371,008.8 m, not the ellipsoid, so distances match Turf and the map: use the haversine or spherical law of cosines for distance, and the standard spherical formulas for initial bearing and destination. Normalize longitudes to −180 to 180 and bearings to 0 to 360. In convertNorth, true = magnetic + declination and true = grid + convergence, both east positive. In formatBearing, round half up first and wrap after, so 359.6° writes as 000°. For compassPoint, use Bowditch’s abbreviations, with ties going clockwise. Pass every case in vectors.json; text must match exactly.

Test data

vectors.json holds 824 cases, published 2026-11-04. Earth model: Sphere, radius 6,371,008.8 m, the mean radius Turf uses. How to read it: Test data format.

OperationCases
inverse 313311 reference · 11 edge · 2 invalid
destination 306304 reference · 4 edge · 2 invalid
backAzimuth 87 hand · 3 edge · 1 invalid
turn 109 hand · 8 edge · 1 invalid
convertNorth 346 published · 26 hand · 2 edge · 2 invalid
formatBearing 2320 hand · 6 edge · 3 invalid
compassPoint 130128 hand · 68 edge · 2 invalid

Tags say where each value came from: what they mean.

FieldTolerance
distanceInMeters± 0.001
bearingInDegrees± 0.000001
toLatitudeInDegrees± 0.0000001
toLongitudeInDegrees± 0.0000001
backAzimuthInDegrees± 0.000001
turnInDegrees± 0.000001
convertedBearingInDegrees± 0.000001

Every other field is compared exactly.

Implementations

ImplementationStatus
gshaw/algorithms-swiftSwift Passes
gshaw/algorithms-typescriptTypeScript Passes

Each implementation's CI grades it against this page's test data on every push, and again when the data changes. The code isn't public yet. See how implementations work.

Source

Reference programGeodSolve, GeographicLib 2.7, on a sphere (-e 6371008.8 0)Charles Karney. Every call and its output is in /bearings/sources/geodsolve.txt · MIT
Mils, back azimuths and the G-M angleFM 3-25.26, Map Reading and Land Navigation (2001)Headquarters, Department of the Army. Approved for public release. Excerpts and figures 6-10 to 6-15 in /bearings/sources/ · Public domain
Compass pointsThe American Practical Navigator (Bowditch), Pub. 9, Volume II, Appendix BNational Geospatial-Intelligence Agency. The table is in /bearings/sources/bowditch-appendix-b.txt · Public domain