TheAlgorithms/Java · error · IllegalArgumentException

Orbiting mass and radius must be positive.

Error message

Orbiting mass and radius must be positive.

What it means

Thrown by CoulombsLaw.calculateCircularOrbitVelocity when orbitingMass <= 0 or radius <= 0. The orbital velocity formula v = sqrt(F_c * r / m) divides by mass and takes a square root, so a non-positive mass or radius would yield a division by zero, an imaginary result, or a physically meaningless value. This models a charged particle in stable circular orbit around a central charge.

Source

Thrown at src/main/java/com/thealgorithms/physics/CoulombsLaw.java:71

        double fy = forceMagnitude * (dy / distance);

        return new double[] {fx, fy};
    }

    /**
     * Calculates the speed required for a stable circular orbit of a charged particle
     * around a central charge (e.g., an electron orbiting a nucleus).
     *
     * @param centralCharge The charge of the central body (in Coulombs).
     * @param orbitingCharge The charge of the orbiting body (in Coulombs).
     * @param orbitingMass The mass of the orbiting body (in kg).
     * @param radius The radius of the orbit (in m).
     * @return The orbital speed (in m/s).
     * @throws IllegalArgumentException if mass or radius are not positive.
     */
    public static double calculateCircularOrbitVelocity(double centralCharge, double orbitingCharge, double orbitingMass, double radius) {
        if (orbitingMass <= 0 || radius <= 0) {
            throw new IllegalArgumentException("Orbiting mass and radius must be positive.");
        }

        // We only need the magnitude of the force, which is always positive.
        double forceMagnitude = Math.abs(COULOMBS_CONSTANT * centralCharge * orbitingCharge) / (radius * radius);

        // F_c = m * v^2 / r  =>  v = sqrt(F_c * r / m)
        return Math.sqrt(forceMagnitude * radius / orbitingMass);
    }
}

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Pass strictly positive values for orbitingMass (kg) and radius (m).
  2. Validate inputs at the boundary of your data layer (parse/load) and reject zero or negative mass/radius before reaching the physics call.
  3. If charges came from external data, double-check you are not confusing the charge array index with the mass/radius index.

Example fix

// before
double v = CoulombsLaw.calculateCircularOrbitVelocity(1.6e-19, 1.6e-19, 0, 5.29e-11);
// after
double v = CoulombsLaw.calculateCircularOrbitVelocity(1.6e-19, 1.6e-19, 9.11e-31, 5.29e-11);
Defensive patterns

Strategy: validation

Validate before calling

if (!(orbitingMass > 0 && radius > 0)) {
    throw new IllegalArgumentException("mass and radius must be > 0 before orbit calc");
}
double v = CoulombsLaw.calculateCircularOrbitVelocity(centralCharge, orbitingCharge, orbitingMass, radius);

Prevention

When it happens

Trigger: Calling calculateCircularOrbitVelocity(centralCharge, orbitingCharge, orbitingMass, radius) with orbitingMass = 0 or negative, or radius = 0 or negative. Note centralCharge and orbitingCharge are NOT validated here (they are squared/magnitude-handled), so they do not trigger this.

Common situations: Using uninitialised (0) mass/radius fields, a config typo specifying radius 0, unit-conversion mistakes producing 0, or importing data where a column was null and coerced to 0.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/f30c009f7f5ed762. Report an issue: GitHub.