TheAlgorithms/Java · error · IllegalArgumentException
Length must be non-negative
Error message
Length must be non-negative
What it means
Thrown by Relativity.lengthContraction when length < 0. Length contraction L = L0 / gamma cannot produce a meaningful result for a negative proper length; the physical contractible quantity must be non-negative. Note this guard runs before the call to gamma(v), so a length validation failure surfaces even if the velocity is also invalid.
Source
Thrown at src/main/java/com/thealgorithms/physics/Relativity.java:46
* @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);
}
/**
* Calculates the time that has passed in the moving frame.
*
* @param length The time that has passed in the object's own frame (s).
* @param v The velocity of the object (m/s).
* @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);
}
View on GitHub (pinned to fdfb9a395b)
Solutions
- Pass a non-negative length in meters; if you computed it as a difference, wrap with Math.abs.
- Validate lengths at the data boundary and reject negatives before invoking lengthContraction.
- Check that the velocity is also subluminal, since gamma(v) is called next and will throw its own message otherwise.
Example fix
// before double L = Relativity.lengthContraction(x2 - x1, v); // after double L0 = Math.abs(x2 - x1); double L = Relativity.lengthContraction(L0, v);
Defensive patterns
Strategy: validation
Validate before calling
double L0 = Math.abs(length);
if (L0 < 0) { // always false after abs, but documents intent
throw new IllegalArgumentException("length must be non-negative");
}
double L = Relativity.lengthContraction(L0, v); Prevention
- Compute lengths as Math.abs(difference) to avoid sign errors.
- Validate lengths at the measurement boundary.
- Remember the velocity is validated separately in gamma — guard it too.
When it happens
Trigger: Calling lengthContraction(length, v) with a negative length. A length of exactly 0 is allowed (returns 0). The velocity v is validated separately inside gamma.
Common situations: A coordinate subtraction that yields a negative distance instead of its absolute value, a sign convention mismatch, or importing a measurement stored as a signed delta.
Related errors
- Speed must be lower than the speed of light
- Time must be non-negative
- Speed must not exceed the speed of light
- Frame speed must be lower than the speed of light
- Orbiting mass and radius must be positive.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/d5cbacd4aadf1e5e.
Report an issue: GitHub.