{"record":{"id":"fa33eb3002e8b90c","repo":"TheAlgorithms/Java","slug":"speed-must-be-lower-than-the-speed-of-light","errorCode":null,"errorMessage":"Speed must be lower than the speed of light","messagePattern":"Speed must be lower than the speed of light","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/physics/Relativity.java","lineNumber":32,"sourceCode":"    public static final double SPEED_OF_LIGHT = 299792458.0;\n\n    /**\n     * Private constructor to prevent instantiation of this utility class.\n     */\n    private Relativity() {\n    }\n\n    /**\n     * Calculates the gamma parameter that is of paramount importance in relativity\n     * theory. It is a dimensionless parameter that is equal to 1 for zero velocity\n     * but tends to infinity when velocity approaches the speed of light.\n     *\n     * @param v The velocity (m/s).\n     * @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","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/physics/Relativity.java#L14-L50","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\ndouble g = Relativity.gamma(3.5e8); // 350,000 km/s treated as m/s — exceeds c\n// after\ndouble g = Relativity.gamma(2.5e8); // 250,000 km/s in m/s, below c","handlingStrategy":"validation","validationCode":"if (!(Math.abs(v) < SPEED_OF_LIGHT)) {\n    throw new IllegalArgumentException(\"|v| must be < c (299792458 m/s), got \" + v);\n}\ndouble g = Relativity.gamma(v);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["java","physics","relativity","parameter-validation","unit-mismatch","illegal-argument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}