Floryne Pierson
← All posts

Building a precise astronomy engine for production: 0.29° down to 0.003°

How I measured, diagnosed and fixed two coordinate-system bugs — using Swiss Ephemeris as a reference before touching a single line of code.

Magical Chart generates natal astrology reports: planetary positions, house cusps, aspects. The accuracy of that output depends entirely on the astronomy engine underneath. If the Moon's north node is off by a degree, every interpretation built on top of it is wrong. The question is: how do you know when it's wrong, and how do you know when you've actually fixed it?

That's what this article is about. Not the glamorous part of building an AI product — but the measurement discipline that makes the glamorous part trustworthy.

The discipline: measure the broken state first

When I started auditing the astronomy engine, I had a hunch that the coordinate handling wasn't right. The temptation is to fix the code you suspect is wrong, then verify it looks better. That's the wrong order.

The right order: measure the broken state against an authoritative external reference, then fix, then re-measure. This gives you before/after statistics instead of "it looked wrong and now it looks right." For a production system that tells real users where their Saturn was when they were born, the difference matters.

I used Swiss Ephemeris as the external reference — the same library used by professional astrology software, accurate to sub-arcsecond precision. I ran 25 test dates through both engines and recorded the deltas.

Bug 1: Lunar nodes — computing an event instead of a position

The first bug was conceptual, not just numerical. The original implementation called SearchMoonNode() — a function that searches for the moment in time when the Moon last crossed the ecliptic plane. That's an event. What we needed was the current direction of that crossing — a position on the ecliptic, expressed as an angle.

These are related but different things. The event tells you when the Moon was last at the node. The position tells you where the node currently is in the sky. For a natal chart, you need the position.

Measured error in the broken state against Swiss Ephemeris: mean 0.29°, max 1.07° across 25 dates.

The correct implementation computes the instantaneous ascending node using the Moon's orbital angular momentum vector — the cross product of position and velocity, projected onto the ecliptic plane:

// Broken: event search
const node = Astronomy.SearchMoonNode(date);

// Fixed: instantaneous position via orbital mechanics
const moonState = Astronomy.GeoVector(Body.Moon, date, true);
const h = cross(moonState.position, moonState.velocity);
const ascendingNodeLongitude = Math.atan2(-h.x, h.y) * 180 / Math.PI;

After the fix: mean error 0.003°, max 0.007°. That's a 97× improvement.

Bug 2: Planets — equatorial coordinates treated as ecliptic

The second bug was a coordinate system mismatch. Planetary positions were being computed in equatorial coordinates (RA/Dec, referenced to Earth's equator) but then read as if they were ecliptic coordinates (longitude/latitude, referenced to the orbital plane). These are two different reference frames, tilted relative to each other by about 23.4°.

The practical error: for most planets near the ecliptic, this produced position errors of 1–2°. For planets near the solstice points (where the two frames diverge most), errors were higher.

The fix was one line — using the astronomy library's built-in ecliptic conversion instead of reading the raw equatorial components:

// Broken: reading equatorial RA as ecliptic longitude
const lon = geoVector.x; // wrong reference frame

// Fixed: proper ecliptic conversion
const ecliptic = Astronomy.Ecliptic(geoVector);
const lon = ecliptic.elon;

After the fix: mean error below 0.001° for all tested planets.

ComponentBefore (mean error)After (mean error)
Lunar nodes0.29°0.003°
Planetary longitudes1–2°<0.001°

Why 1° matters in astrology

One degree of longitude on the ecliptic corresponds to roughly 111 km on Earth's surface when projected onto an astrocartography map. At 1° of error, a planet line that should run through Paris might be drawn through Brussels. For a natal chart interpretation, a 1° error can shift a planet across a house cusp — changing the entire interpretation of that placement.

Precision isn't a vanity metric here. It's the difference between telling a user their rising sign correctly or incorrectly.

The provider pattern: keeping the engine replaceable

The astronomy engine is wrapped in a provider interface. No code outside astronomy-core calls the underlying library directly — everything goes through AstronomyProvider:

// All callers use the provider interface
const provider = new AstronomyEngineProvider();
const positions = await provider.getPlanetaryPositions(date, lat, lon);

// The underlying library is an implementation detail
// — replaceable without touching astrology-core or the AI layer

This matters for a few reasons. The underlying library (Astronomy Engine, running in Node without native bindings) is accurate but not the gold standard — Swiss Ephemeris is more precise but requires Python bindings that don't fit the serverless architecture. If I need to swap in a more accurate backend later, the provider interface means none of the downstream code changes.

It also means tests can inject a mock provider that returns known positions, making the astrology interpretation layer testable independently of the actual ephemeris calculations.

The Chiron edge case: building a custom ephemeris

Chiron — a minor planet often included in modern astrology charts — wasn't in the astronomy library at all. No ephemeris, no function to call.

I fetched Chiron's orbital elements from JPL Horizons (NASA's planetary data API) and implemented a Keplerian orbital solver. To verify it, I pulled JPL's own perturbed position vectors at four reference dates and compared. The errors were within acceptable range for astrology purposes — not Swiss-Ephemeris-class accuracy, but documented and bounded, not silently wrong.

The key principle carried through: don't claim precision you haven't measured. The Chiron implementation is documented as "Keplerian approximation, JPL-cross-checked at four points" — not as equivalent to the true node work where I have full error statistics.

The lesson: "it looks right" is not evidence. Measure the broken state, fix one thing at a time, re-measure. If you can't put a number on how wrong it was before, you can't put a number on how right it is now. For any system where correctness is load-bearing — financial calculations, medical data, astronomical positions — documented error bars are not optional.

What the validation process prevented

During Phase 2 of the validation, I discovered that a core library function my implementation depended on simply didn't exist in the version I was running — Astronomy.LunarNode() wasn't there. The code would have crashed at runtime for every natal chart request.

Because the validation layer existed before the astrology interpretation layer was built on top of it, the crash was caught in tests, not in production. That's the other reason to validate before building the dependent layer: a broken foundation costs more to fix the longer you build on top of it.