TheAlgorithms/Python · error · ValueError

Speed must be greater than or equal to 1!

Error message

Speed must be greater than or equal to 1!

What it means

Raised by beta(velocity) when velocity < 1 (m/s). This is not a physical limit but a practical floor: the comment notes speeds should be of order c for the transformation to be meaningful, so sub-1 m/s inputs are treated as likely unit errors. Combined with the upper guard, valid input is the window [1, 299792458] m/s.

Source

Thrown at physics/lorentz_transformation_four_vector.py:59

def beta(velocity: float) -> float:
    """
    Calculates β = v/c, the given velocity as a fraction of c
    >>> beta(c)
    1.0
    >>> beta(199792458)
    0.666435904801848
    >>> beta(1e5)
    0.00033356409519815205
    >>> beta(0.2)
    Traceback (most recent call last):
      ...
    ValueError: Speed must be greater than or equal to 1!
    """
    if velocity > c:
        raise ValueError("Speed must not exceed light speed 299,792,458 [m/s]!")
    elif velocity < 1:
        # Usually the speed should be much higher than 1 (c order of magnitude)
        raise ValueError("Speed must be greater than or equal to 1!")

    return velocity / c


def gamma(velocity: float) -> float:
    """
    Calculate the Lorentz factor y = 1 / √(1 - v²/c²) for a given velocity
    >>> gamma(4)
    1.0000000000000002
    >>> gamma(1e5)
    1.0000000556325075
    >>> gamma(3e7)
    1.005044845777813
    >>> gamma(2.8e8)
    2.7985595722318277
    >>> gamma(299792451)
    4627.49902669495
    >>> gamma(0.3)

View on GitHub (pinned to f5988cc097)

Solutions

  1. Convert to m/s: multiply fractions-of-c by 299792458, km/s by 1000, mph by 0.44704.
  2. If you genuinely need v < 1 m/s, compute beta manually as v/299792458 instead of using this helper.
  3. Validate 1 <= velocity <= 299792458 before calling.

Example fix

# before
beta(0.2)  # ValueError: speed is 0.2 m/s, not 0.2c

# after
beta(0.2 * 299_792_458)  # 0.2c expressed in m/s
Defensive patterns

Strategy: validation

Validate before calling

C = 299_792_458.0
v_mps = velocity_in_c_units * C  # if input is in units of c
assert 1.0 <= v_mps <= C, f'v must be within [1, {C}] m/s'
beta(v_mps)

Prevention

When it happens

Trigger: beta(0.2) from the doctest — almost always a velocity passed in units of c (0.2c) or km/s instead of m/s. beta(0) for a rest frame is also rejected.

Common situations: Natural-unit confusion (passing beta instead of v); passing velocities in km/h or mph (all < 3e5-ish, most < 1 only for very slow objects — walking speed 1.4 m/s passes, 0.5 m/s fails); testing with 0.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/0eede68e7b46fb33. Report an issue: GitHub.