TheAlgorithms/Java · error · IllegalArgumentException
Time must be non-negative
Error message
Time must be non-negative
What it means
Thrown by Relativity.timeDilation when time < 0. Time dilation t = t0 * gamma multiplies a proper time by the Lorentz factor; a negative proper time is physically meaningless (proper time is a non-negative interval along a worldline). The velocity v is validated separately when timeDilation delegates to gamma(v).
Source
Thrown at src/main/java/com/thealgorithms/physics/Relativity.java:60
* @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);
}
/**
* 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");
}View on GitHub (pinned to fdfb9a395b)
Solutions
- Pass a non-negative time in seconds; reorder event subtraction as t2 - t1 only when t2 >= t1, or use Math.abs if magnitude is what matters.
- Validate proper-time inputs at the boundary and reject negative values before the call.
- Ensure the velocity is subluminal, because gamma(v) will throw separately otherwise.
Example fix
// before double t = Relativity.timeDilation(t1 - t2, v); // after double t0 = Math.max(0, t2 - t1); double t = Relativity.timeDilation(t0, v);
Defensive patterns
Strategy: validation
Validate before calling
double t0 = Math.max(0, time); double t = Relativity.timeDilation(t0, v);
Prevention
- Order time differences so the result is non-negative, or take the magnitude.
- Reject sentinel -1 values from optional fields before they reach physics calls.
- Validate the velocity separately since gamma will throw its own message.
When it happens
Trigger: Calling timeDilation(time, v) with a negative time value. A time of exactly 0 is allowed and returns 0.
Common situations: A clock-reading subtraction that produced a negative delta due to event ordering, a timestamp parsed with a sign error, or a default sentinel value of -1 leaking into the call.
Related errors
- Speed must be lower than the speed of light
- Length 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/228c484257a8bdac.
Report an issue: GitHub.