{"record":{"id":"94625c1d63fb54d7","repo":"TheAlgorithms/Python","slug":"speed-must-not-exceed-light-speed-299-792-458-m-s","errorCode":null,"errorMessage":"Speed must not exceed light speed 299,792,458 [m/s]!","messagePattern":"Speed must not exceed light speed 299,792,458 \\[m/s\\]!","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"physics/lorentz_transformation_four_vector.py","lineNumber":56,"sourceCode":"\n\n# Vehicle's speed divided by speed of light (no units)\ndef beta(velocity: float) -> float:\n    \"\"\"\n    Calculates β = v/c, the given velocity as a fraction of c\n    >>> beta(c)\n    1.0\n    >>> beta(199792458)\n    0.666435904801848\n    >>> beta(1e5)\n    0.00033356409519815205\n    >>> beta(0.2)\n    Traceback (most recent call last):\n      ...\n    ValueError: Speed must be greater than or equal to 1!\n    \"\"\"\n    if velocity > c:\n        raise ValueError(\"Speed must not exceed light speed 299,792,458 [m/s]!\")\n    elif velocity < 1:\n        # Usually the speed should be much higher than 1 (c order of magnitude)\n        raise ValueError(\"Speed must be greater than or equal to 1!\")\n\n    return velocity / c\n\n\ndef gamma(velocity: float) -> float:\n    \"\"\"\n    Calculate the Lorentz factor y = 1 / √(1 - v²/c²) for a given velocity\n    >>> gamma(4)\n    1.0000000000000002\n    >>> gamma(1e5)\n    1.0000000556325075\n    >>> gamma(3e7)\n    1.005044845777813\n    >>> gamma(2.8e8)\n    2.7985595722318277","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/physics/lorentz_transformation_four_vector.py#L38-L74","documentation":"Raised by beta(velocity) in lorentz_transformation_four_vector when velocity exceeds c = 299,792,458 m/s. beta computes v/c for the Lorentz transformation, and superluminal speeds make the Lorentz factor imaginary, so they are rejected outright. Inputs are assumed to be in m/s.","triggerScenarios":"beta(3e8) or any velocity > 299792458; passing velocity in km/s or units of c (e.g. beta(0.9) is fine because it is < 1, but beta(2) interpreted as 2c in natural units is 2 m/s here and hits the other branch).","commonSituations":"Unit mismatch — velocity supplied in km/s (values look small) or in fractions of c multiplied by the wrong constant; sci-fi or simulation scenarios with FTL speeds; accumulating numerical error pushing v slightly above c near the limit.","solutions":["Pass velocity in m/s and clamp to at most c: min(v, 299792458.0) when computing limits.","Fix unit conversion (km/s -> m/s means multiply by 1000, so 0.9c is 269813212.2 m/s).","Catch ValueError and reject FTL inputs at your API boundary."],"exampleFix":"# before\nbeta(3e8)  # ValueError: exceeds c\n\n# after\nC = 299_792_458.0\nbeta(min(3e8, C))  # clamped, beta(c) == 1.0","handlingStrategy":"validation","validationCode":"C = 299_792_458.0  # m/s\nif not (1.0 <= velocity <= C):\n    raise ValueError(f'velocity must be in [1, {C}] m/s, got {velocity}')\nbeta(velocity)","typeGuard":null,"tryCatchPattern":"try:\n    beta(v)\nexcept ValueError as e:\n    if 'exceed' in str(e):\n        v = min(v, 299_792_458.0)  # clamp FTL input\n    else:\n        raise","preventionTips":["Standardize on m/s everywhere in your codebase.","Clamp near-c numeric results with min(v, c) before calling.","Convert fractions of c by multiplying by 299792458."],"tags":["physics","relativity","units","range-check","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}