TheAlgorithms/Java · error · IllegalArgumentException
Speed must be lower than the speed of light
Error message
Speed must be lower than the speed of light
What it means
Thrown by Relativity.gamma when Math.abs(v) >= SPEED_OF_LIGHT. The Lorentz factor gamma = 1 / sqrt(1 - v^2/c^2) diverges as v approaches c and becomes imaginary for v > c; the speed of light is the upper bound on any massive object's velocity, so |v| >= c is physically invalid. gamma is the shared helper used by lengthContraction, timeDilation, and velocityAddition.
Source
Thrown at src/main/java/com/thealgorithms/physics/Relativity.java:32
public static final double SPEED_OF_LIGHT = 299792458.0;
/**
* Private constructor to prevent instantiation of this utility class.
*/
private Relativity() {
}
/**
* Calculates the gamma parameter that is of paramount importance in relativity
* theory. It is a dimensionless parameter that is equal to 1 for zero velocity
* but tends to infinity when velocity approaches the speed of light.
*
* @param v The velocity (m/s).
* @return The value of gamma parameter.
*/
public static double gamma(double v) {
if (Math.abs(v) >= SPEED_OF_LIGHT) {
throw new IllegalArgumentException("Speed must be lower than the speed of light");
}
return 1.0 / Math.sqrt(1 - v * v / (SPEED_OF_LIGHT * SPEED_OF_LIGHT));
}
/**
* Calculates the length of an object in the moving frame.
*
* @param length The length of an object in its own frame (m).
* @param v The velocity of the object (m/s).
* @return The length of an object in the laboratory frame (m).
*/
public static double lengthContraction(double length, double v) {
if (length < 0) {
throw new IllegalArgumentException("Length must be non-negative");
}
return length / gamma(v);
}
View on GitHub (pinned to fdfb9a395b)
Solutions
- Ensure |v| is in m/s and strictly less than 299792458 m/s.
- Confirm the unit of SPEED_OF_LIGHT used internally (this file uses m/s) matches the unit of your input velocity.
- For inputs near c, check for floating-point overshoot: clamp with Math.min(Math.abs(v), SPEED_OF_LIGHT * (1 - 1e-12)) only if physically justified.
Example fix
// before double g = Relativity.gamma(3.5e8); // 350,000 km/s treated as m/s — exceeds c // after double g = Relativity.gamma(2.5e8); // 250,000 km/s in m/s, below c
Defensive patterns
Strategy: validation
Validate before calling
if (!(Math.abs(v) < SPEED_OF_LIGHT)) {
throw new IllegalArgumentException("|v| must be < c (299792458 m/s), got " + v);
}
double g = Relativity.gamma(v); Prevention
- Confirm velocity units (m/s) match SPEED_OF_LIGHT before every relativistic call.
- Wrap relativistic calls in a helper that validates units once.
- Test with canonical values (0, 0.5c, 0.99c) to catch unit errors early.
When it happens
Trigger: Calling gamma(v) with v == SPEED_OF_LIGHT, v == -SPEED_OF_LIGHT, or |v| > SPEED_OF_LIGHT. Indirectly triggered when lengthContraction or timeDilation receive such a velocity, because they delegate to gamma.
Common situations: Passing a velocity in km/s or mph without converting to m/s (so 300000 instead of 3e8, but the reverse — passing m/s where km/s was assumed — can overshoot c), or a unit mismatch where SPEED_OF_LIGHT is taken as ~3e8 m/s but the input is already in m/s and slightly exceeds it due to rounding.
Related errors
- Speed must not exceed 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/fa33eb3002e8b90c.
Report an issue: GitHub.