TheAlgorithms/Java · error · IllegalArgumentException

Frame speed must be lower than the speed of light

Error message

Frame speed must be lower than the speed of light

What it means

Thrown by Relativity.velocityAddition when Math.abs(v) >= SPEED_OF_LIGHT. The frame velocity v appears in the denominator 1 - v1*v/c^2; if |v| reaches c the denominator can vanish (singularity) and beyond c the formula loses physical meaning. This is the second guard, checked after the v1 guard, so v1 must already be within ±c.

Source

Thrown at src/main/java/com/thealgorithms/physics/Relativity.java:77

        if (time < 0) {
            throw new IllegalArgumentException("Time must be non-negative");
        }
        return time * gamma(v);
    }

    /**
     * Calculates the velocity with respect to the moving frame.
     *
     * @param v1 The velocity of the object with respect to laboratory frame (m/s).
     * @param v The velocity of the moving frame (m/s).
     * @return The velocity with respect to the moving frame (m/s).
     */
    public static double velocityAddition(double v1, double v) {
        if (Math.abs(v1) > SPEED_OF_LIGHT) {
            throw new IllegalArgumentException("Speed must not exceed the speed of light");
        }
        if (Math.abs(v) >= SPEED_OF_LIGHT) {
            throw new IllegalArgumentException("Frame speed must be lower than the speed of light");
        }
        return (v1 - v) / (1 - v1 * v / (SPEED_OF_LIGHT * SPEED_OF_LIGHT));
    }
}

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Ensure |v| is in m/s and strictly less than 299792458 m/s (the frame must be subluminal, not equal to c).
  2. Confirm v uses the same m/s unit as SPEED_OF_LIGHT.
  3. Note the asymmetry: v1 may equal ±c but v may not — do not treat the two guards as interchangeable.

Example fix

// before
double u = Relativity.velocityAddition(2e8, 3e8);
// after
double u = Relativity.velocityAddition(2e8, 1.5e8);
Defensive patterns

Strategy: validation

Validate before calling

if (Math.abs(v) >= SPEED_OF_LIGHT) {
    throw new IllegalArgumentException("|v| (frame) must be < c, got " + v);
}
double u = Relativity.velocityAddition(v1, v);

Prevention

When it happens

Trigger: Calling velocityAddition(v1, v) with |v| >= SPEED_OF_LIGHT. Unlike the v1 check, this uses >= so exactly ±c for the frame velocity is rejected (the moving frame cannot travel at c).

Common situations: Unit mismatch inflating the frame velocity v, or reusing a particle velocity as a frame velocity without subluminal enforcement.

Related errors


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