TheAlgorithms/Python · error · ValueError

Orbital radii must be greater than zero.

Error message

Orbital radii must be greater than zero.

What it means

Raised by orbital_transfer_work(mass_central, mass_object, r_initial, r_final) when r_initial <= 0 or r_final <= 0. Orbital radii are distances from the central body and must be strictly positive; zero or negative radii make the 1/r terms in the work formula singular or unphysical, so they are rejected before computing W = G*M*m/2 * (1/r_initial - 1/r_final).

Source

Thrown at physics/orbital_transfer_work.py:39

        mass_object (float): Mass of the object being moved (kg)
        r_initial (float): Initial orbital radius (m)
        r_final (float): Final orbital radius (m)

    Returns:
        str: Work done in Joules as a string in scientific notation (3 decimals)

    Examples:
        >>> orbital_transfer_work(5.972e24, 1000, 6.371e6, 7e6)
        '2.811e+09'
        >>> orbital_transfer_work(5.972e24, 500, 7e6, 6.371e6)
        '-1.405e+09'
        >>> orbital_transfer_work(1.989e30, 1000, 1.5e11, 2.28e11)
        '1.514e+11'
    """
    gravitational_constant = 6.67430e-11

    if r_initial <= 0 or r_final <= 0:
        raise ValueError("Orbital radii must be greater than zero.")

    work = (gravitational_constant * mass_central * mass_object / 2) * (
        1 / r_initial - 1 / r_final
    )
    return f"{work:.3e}"


if __name__ == "__main__":
    import doctest

    doctest.testmod()
    print("Orbital transfer work calculator\n")

    try:
        M = float(input("Enter mass of central body (kg): ").strip())
        if M <= 0:
            r1 = float(input("Enter initial orbit radius (m): ").strip())
        if r1 <= 0:

View on GitHub (pinned to f5988cc097)

Solutions

  1. Pass orbital radius, not altitude: r = altitude + central_body_radius (e.g. 6.371e6 for Earth)
  2. Check the argument order matches (mass_central, mass_object, r_initial, r_final)
  3. Clamp or reject simulation states with r <= 0 before calling the function

Example fix

# before
orbital_transfer_work(5.972e24, 1000, 400e3, 7e6)  # altitude, not radius
# ValueError: Orbital radii must be greater than zero.

# after
orbital_transfer_work(5.972e24, 1000, 400e3 + 6.371e6, 7e6)
Defensive patterns

Strategy: validation

Validate before calling

def radius_from_altitude(altitude, body_radius):
    r = altitude + body_radius
    if r <= 0:
        raise ValueError(f"radius {r} must be > 0 (altitude={altitude})")
    return r

orbital_transfer_work(5.972e24, 1000, radius_from_altitude(alt1, 6.371e6), radius_from_altitude(alt2, 6.371e6))

Try / catch

try:
    w = orbital_transfer_work(M, m, r1, r2)
except ValueError as e:
    if "Orbital radii" in str(e):
        raise ValueError(f"bad radii: r1={r1}, r2={r2}") from e
    raise

Prevention

When it happens

Trigger: orbital_transfer_work(5.972e24, 1000, 0, 7e6); orbital_transfer_work(5.972e24, 1000, -6.371e6, 7e6); radii derived from altitude minus Earth radius that came out <= 0 because the altitude was below ground or zero.

Common situations: Passing altitude (height above surface) instead of radius (altitude + body radius), which yields tiny or negative values; swapping argument order so a mass lands in an r slot; simulation edge cases where an orbit decays to r=0.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/17ffc74b5680d1b6. Report an issue: GitHub.