{"record":{"id":"228c484257a8bdac","repo":"TheAlgorithms/Java","slug":"time-must-be-non-negative","errorCode":null,"errorMessage":"Time must be non-negative","messagePattern":"Time must be non-negative","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/physics/Relativity.java","lineNumber":60,"sourceCode":"     * @return The length of an object in the laboratory frame (m).\n     */\n    public static double lengthContraction(double length, double v) {\n        if (length < 0) {\n            throw new IllegalArgumentException(\"Length must be non-negative\");\n        }\n        return length / gamma(v);\n    }\n\n    /**\n     * Calculates the time that has passed in the moving frame.\n     *\n     * @param length The time that has passed in the object's own frame (s).\n     * @param v The velocity of the object (m/s).\n     * @return The time that has passed in the laboratory frame (s).\n     */\n    public static double timeDilation(double time, double v) {\n        if (time < 0) {\n            throw new IllegalArgumentException(\"Time must be non-negative\");\n        }\n        return time * gamma(v);\n    }\n\n    /**\n     * Calculates the velocity with respect to the moving frame.\n     *\n     * @param v1 The velocity of the object with respect to laboratory frame (m/s).\n     * @param v The velocity of the moving frame (m/s).\n     * @return The velocity with respect to the moving frame (m/s).\n     */\n    public static double velocityAddition(double v1, double v) {\n        if (Math.abs(v1) > SPEED_OF_LIGHT) {\n            throw new IllegalArgumentException(\"Speed must not exceed the speed of light\");\n        }\n        if (Math.abs(v) >= SPEED_OF_LIGHT) {\n            throw new IllegalArgumentException(\"Frame speed must be lower than the speed of light\");\n        }","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/physics/Relativity.java#L42-L78","documentation":"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).","triggerScenarios":"Calling timeDilation(time, v) with a negative time value. A time of exactly 0 is allowed and returns 0.","commonSituations":"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.","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."],"exampleFix":"// before\ndouble t = Relativity.timeDilation(t1 - t2, v);\n// after\ndouble t0 = Math.max(0, t2 - t1);\ndouble t = Relativity.timeDilation(t0, v);","handlingStrategy":"validation","validationCode":"double t0 = Math.max(0, time);\ndouble t = Relativity.timeDilation(t0, v);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["java","physics","relativity","parameter-validation","illegal-argument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}