TheAlgorithms/Java · error · IllegalArgumentException
Speed must not exceed the speed of light
Error message
Speed must not exceed the speed of light
What it means
Thrown by Relativity.velocityAddition when Math.abs(v1) > SPEED_OF_LIGHT. The relativistic velocity-addition formula (v1 - v) / (1 - v1*v/c^2) only has physical meaning when the object's velocity in the lab frame does not exceed c; a superluminal v1 is invalid. This is the first of two velocity guards in velocityAddition, checked before the frame-velocity v guard.
Source
Thrown at src/main/java/com/thealgorithms/physics/Relativity.java:74
* @return The time that has passed in the laboratory frame (s).
*/
public static double timeDilation(double time, double v) {
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
- Ensure |v1| is in m/s and does not exceed 299792458 m/s.
- Verify the unit of v1 matches the SPEED_OF_LIGHT constant (m/s in this file).
- If v1 originates from a previous velocityAddition result, the prior call already enforces subluminal output, so a superluminal input here signals a unit error.
Example fix
// before double u = Relativity.velocityAddition(4e8, 1e8); // after double u = Relativity.velocityAddition(2e8, 1e8);
Defensive patterns
Strategy: validation
Validate before calling
if (Math.abs(v1) > SPEED_OF_LIGHT) {
throw new IllegalArgumentException("|v1| must be <= c, got " + v1);
}
double u = Relativity.velocityAddition(v1, v); Prevention
- Validate both v1 and v units (m/s) before composing velocities.
- Remember v1 may equal ±c but v may not — the two guards differ.
- Unit-test the composition with subluminal canonical values.
When it happens
Trigger: Calling velocityAddition(v1, v) with |v1| strictly greater than SPEED_OF_LIGHT. A v1 of exactly ±c is allowed by this guard (it checks > not >=), though the formula denominator may still misbehave.
Common situations: Unit mismatch (km/s vs m/s) inflating v1, or composing velocities from a Galilean-addition step that already exceeded c before being passed here.
Related errors
- Speed must be lower than the speed of light
- Frame speed must be lower than the speed of light
- Length must be non-negative
- Time must be non-negative
- Orbiting mass and radius must be positive.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/11f33523974b832b.
Report an issue: GitHub.