Modeling Mercury Orbital Data (2024–2025) and NKTg Invariant in Scala – Precision & Design Questions

I am implementing an experimental validation workflow in Scala based on Mercury orbital data (2024–2025).

The dataset contains:

  • Position x (meters)

  • Velocity v (m/s)

  • Mass m (kg)

  • Momentum p = m × v

  • Invariant quantity NKTg1 = x × p

Typical magnitudes:

  • x ≈ 10^10

  • m ≈ 10^23

  • NKTg1 ≈ 8.90 × 10^38

Velocity is reconstructed algebraically:

v = NKTg1 / (x * m)

Observed average relative deviation vs 2025 measured values: ~1–2%.


Current Scala Implementation

case class MercuryData(
  date: String,
  position: BigDecimal,
  velocity: BigDecimal,
  mass: BigDecimal
) {

  val NKTg1: BigDecimal = BigDecimal("8.90E+38")

  def momentum: BigDecimal =
    mass * velocity

  def invariantValue: BigDecimal =
    position * momentum

  def simulatedVelocity: BigDecimal =
    NKTg1 / (position * mass)

  def relativeErrorPercent: BigDecimal =
    (simulatedVelocity - velocity) / velocity * 100
}

I chose BigDecimal because:

  • Double introduces drift at ~10^38 scale

  • Reproducibility is important

  • Deterministic results across runs matter


Questions

  1. For magnitudes near 10^38, is BigDecimal the correct approach in Scala, or is there a performant alternative that still guarantees deterministic precision?

  2. Are there recommended numeric contexts (MathContext) for scientific-scale workloads?

  3. If scaling to millions of rows:

    • Should this remain pure Scala collections?

    • Or move to a streaming / Spark-style pipeline?

    • Any recommended numeric libraries for high-precision invariant-style models?

  4. Are there known performance pitfalls when repeatedly computing:

    constant / (x * m)
    

    with BigDecimal in tight loops?


Goal

This is not about astrophysical theory.

It is about:

  • Deterministic numeric modeling

  • Precision at extreme magnitudes

  • Functional modeling of invariant-based computations

  • Performance trade-offs in Scala

I would appreciate guidance from the Scala community regarding best practices for high-magnitude, high-precision arithmetic workflows.

Have you looked at Spire? I’m thinking of the general purpose Number type.

Given the limits of observational accuracy, I would go for plain old BigDecimal to start with.

Out of mild curiosity, are you trying to confirm that Mercury’s angular momentum taken about the centre of mass of itself and the Sun is conserved?

You will also need some kind of geometric vector abstraction to compute the angular momentum; you could write it out longhand in component form, but that’s painful. I used CGAL in C++ way back when I used to do physics related stuff, there will be something like that for Scala, probably in Spire too.

(See what I did there. :zany_face:)

Hopefully any systematic departures from conservation you find will be down to the other planets perturbing Mercury and not a sudden onset of severe gravitational radiation!

1 Like

I don’t do a lot with numerics myself, but agreed – Spire is, AFAIK, the library that takes the topic most seriously.