{"record":{"id":"d5cbacd4aadf1e5e","repo":"TheAlgorithms/Java","slug":"length-must-be-non-negative-d5cbac","errorCode":null,"errorMessage":"Length must be non-negative","messagePattern":"Length must be non-negative","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/physics/Relativity.java","lineNumber":46,"sourceCode":"     * @return The value of gamma parameter.\n     */\n    public static double gamma(double v) {\n        if (Math.abs(v) >= SPEED_OF_LIGHT) {\n            throw new IllegalArgumentException(\"Speed must be lower than the speed of light\");\n        }\n        return 1.0 / Math.sqrt(1 - v * v / (SPEED_OF_LIGHT * SPEED_OF_LIGHT));\n    }\n\n    /**\n     * Calculates the length of an object in the moving frame.\n     *\n     * @param length The length of an object in its own frame (m).\n     * @param v The velocity of the object (m/s).\n     * @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","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/physics/Relativity.java#L28-L64","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\ndouble L = Relativity.lengthContraction(x2 - x1, v);\n// after\ndouble L0 = Math.abs(x2 - x1);\ndouble L = Relativity.lengthContraction(L0, v);","handlingStrategy":"validation","validationCode":"double L0 = Math.abs(length);\nif (L0 < 0) { // always false after abs, but documents intent\n    throw new IllegalArgumentException(\"length must be non-negative\");\n}\ndouble L = Relativity.lengthContraction(L0, v);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"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"}